io.castled.utils.ReflectionUtils Maven / Gradle / Ivy
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);
}
}