com.tngtech.configbuilder.util.AnnotationHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of config-builder Show documentation
Show all versions of config-builder Show documentation
The Config Builder creates fully configured instances of config classes, using values from various sources like properties files, command line arguments etc.
package com.tngtech.configbuilder.util;
import com.google.common.collect.Lists;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Set;
import static org.reflections.ReflectionUtils.*;
public class AnnotationHelper {
public List getAnnotationsAnnotatedWith(Annotation[] annotations, Class extends Annotation> annotationClass) {
List result = Lists.newArrayList();
for (Annotation annotation : annotations) {
if (annotation.annotationType().isAnnotationPresent(annotationClass)) {
result.add(annotation);
}
}
return result;
}
public List getAnnotationsInOrder(Field field, Class extends Annotation>[] annotationOrder) {
List result = Lists.newArrayList();
for (Class extends Annotation> annotationClass : annotationOrder) {
if (field.isAnnotationPresent(annotationClass)) {
result.add(field.getAnnotation(annotationClass));
}
}
return result;
}
public Set getFieldsAnnotatedWith(Class clazz, Class extends Annotation> annotationClass) {
return getAllFields(clazz, withAnnotation(annotationClass));
}
public Set getMethodsAnnotatedWith(Class clazz, Class extends Annotation> annotationClass) {
return getAllMethods(clazz, withAnnotation(annotationClass));
}
public boolean fieldHasAnnotationAnnotatedWith(Field field, Class extends Annotation> annotationClass) {
Annotation[] annotations = field.getDeclaredAnnotations();
return !getAnnotationsAnnotatedWith(annotations, annotationClass).isEmpty();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy