Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package org.jarbframework.utils.bean;
import static org.jarbframework.utils.Asserts.state;
import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import org.springframework.beans.BeanUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.ReflectionUtils;
/**
* Searches for annotations in class and property declarations.
*
* @author Jeroen van Schagen
* @date Aug 29, 2011
*/
public class AnnotationScanner {
private AnnotationScanner() {
}
/**
* Find a specific annotation on the class declaration.
*
* @param beanClass bean that should contain the annotation
* @param annotationType type of annotation that should be looked for
* @return desired annotation, as declared on the class, if any
*/
public static T findAnnotation(Class beanClass, Class annotationType) {
return AnnotationUtils.findAnnotation(beanClass, annotationType);
}
/**
* Check if a specific annotation is declared on the class.
*
* @param beanClass bean that should contain the annotation
* @param annotationType type of annotations that should be looked for
* @return {@code true} if an annotation could be found, else {@code false}
*/
public static boolean hasAnnotation(Class beanClass, Class annotationType) {
return findAnnotation(beanClass, annotationType) != null;
}
/**
* Find a specific annotation on the property declaration. Note that we only
* expect one matching annotation to be found, otherwise an exception is thrown.
*
* @param propertyReference property that should contain the annotation
* @param annotationType type of annotation that should be looked for
* @return desired annotation, as declared on the property, if any
*/
public static T findAnnotation(PropertyReference propertyReference, Class annotationType) {
Collection annotations = getAnnotations(propertyReference, annotationType);
if (annotations.isEmpty()) {
return null;
} else {
state(annotations.size() == 1, "Found more than one matching annotation.");
return annotations.iterator().next();
}
}
/**
* Find a specific annotation on the property declaration.
* @param propertyReference property that should contain the annotation
* @param annotationType type of annotation that should be looked for
* @return desired annotation, as declared on the property, if any
*/
public static Collection getAnnotations(PropertyReference propertyReference, Class annotationType) {
PropertyReference finalReference = BeanProperties.getFinalProperty(propertyReference);
Set annotations = new HashSet();
addIfNotNull(annotations, getFieldAnnotation(finalReference, annotationType));
addIfNotNull(annotations, getGetterAnnotation(finalReference, annotationType));
return annotations;
}
private static T getGetterAnnotation(PropertyReference propertyReference, Class annotationType) {
PropertyDescriptor propertyDescriptor = BeanUtils.getPropertyDescriptor(propertyReference.getBeanClass(), propertyReference.getName());
T annotation = null;
if (propertyDescriptor != null && propertyDescriptor.getReadMethod() != null) {
annotation = AnnotationUtils.findAnnotation(propertyDescriptor.getReadMethod(), annotationType);
}
return annotation;
}
private static T getFieldAnnotation(PropertyReference propertyReference, Class annotationType) {
Field field = ReflectionUtils.findField(propertyReference.getBeanClass(), propertyReference.getName());
T annotation = null;
if (field != null) {
annotation = field.getAnnotation(annotationType);
}
return annotation;
}
private static void addIfNotNull(Collection collection, T element) {
if (element != null) {
collection.add(element);
}
}
/**
* Check if a specific property is declared on the property.
*
* @param propertyReference property that should contain the annotation
* @param annotationType type of annotations that should be looked for
* @return {@code true} if an annotation could be found, else {@code false}
*/
public static boolean hasAnnotation(PropertyReference propertyReference, Class annotationType) {
return findAnnotation(propertyReference, annotationType) != null;
}
}