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

io.castled.utils.ReflectionUtils Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package io.castled.utils;

import com.google.api.client.util.Lists;
import org.apache.commons.collections.CollectionUtils;

import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ReflectionUtils {

    public static  List getAnnotationsFromType(Class classType, final Class annotationClass) {

        List annotations = Lists.newArrayList();
        while (!classType.getName().equals(Object.class.getName())) {

            annotations.addAll(Arrays.stream(classType.getAnnotationsByType(annotationClass))
                    .collect(Collectors.toList()));
            classType = classType.getSuperclass();
        }
        return annotations;
    }

    public static  A getAnnotation(Class classType, final Class annotationClass) {

        List annotations = getAnnotationsFromType(classType, annotationClass);
        if (CollectionUtils.isEmpty(annotations)) {
            return null;
        }
        return annotations.get(0);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy