All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tngtech.configbuilder.util.AnnotationHelper Maven / Gradle / Ivy

Go to download

The Config Builder creates fully configured instances of config classes, using values from various sources like properties files, command line arguments etc.

The newest version!
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 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[] annotationOrder) {
        List result = Lists.newArrayList();
        for (Class annotationClass : annotationOrder) {
            if (field.isAnnotationPresent(annotationClass)) {
                result.add(field.getAnnotation(annotationClass));
            }
        }
        return result;
    }

    public Set getFieldsAnnotatedWith(Class clazz, Class annotationClass) {
        return getAllFields(clazz, withAnnotation(annotationClass));
    }

    public Set getMethodsAnnotatedWith(Class clazz, Class annotationClass) {
        return getAllMethods(clazz, withAnnotation(annotationClass));
    }

    public boolean fieldHasAnnotationAnnotatedWith(Field field, Class annotationClass) {
        Annotation[] annotations = field.getDeclaredAnnotations();
        return !getAnnotationsAnnotatedWith(annotations, annotationClass).isEmpty();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy