Java Utililty Methods Reflection Annotation

List of utility methods to do Reflection Annotation

Description

The list of methods to do Reflection Annotation are organized into topic(s).

Method

ListgetAnnotationOuterIndecies(Class annotationClass, Annotation[][] annotations)
get Annotation Outer Indecies
List<Integer> result = new ArrayList<Integer>();
for (int i = 0; i < annotations.length; i++) {
    for (int k = 0; k < annotations[i].length; k++) {
        if (annotationClass.isInstance(annotations[i][k]))
            result.add(i);
return result;
...
ObjectgetAnnotationPropertyValue(Annotation a, String annotationPropertyName)
Given an Annotation and a property name, this method finds the property method, invokes it, and returns the result
try {
    Method[] methods = a.annotationType().getMethods();
    for (int i = 0; i < methods.length; i++) {
        if (methods[i].getName().equals(annotationPropertyName)) {
            return methods[i].invoke(a, new Object[] {});
} catch (Exception e) {
...
TgetAnnotationRecursive(Class cls, Class annotationClass)
get Annotation Recursive
T annotation = cls.getAnnotation(annotationClass);
if (annotation != null)
    return annotation;
if (Object.class.equals(cls))
    return null;
return getAnnotationRecursive(cls.getSuperclass(), annotationClass);
CollectiongetAnnotations(AnnotatedElement ae, Class annotationType)
get Annotations
Collection<T> anns = new ArrayList<>(2);
T ann = ae.getAnnotation(annotationType);
if (ann != null) {
    anns.add(ann);
for (Annotation metaAnn : ae.getAnnotations()) {
    ann = metaAnn.annotationType().getAnnotation(annotationType);
    if (ann != null) {
...
Annotation[]getAnnotations(AnnotatedElement annotated)
Returns all annotations present on this element.
List<Annotation> results = new LinkedList<Annotation>();
results.addAll(Arrays.asList(annotated.getAnnotations()));
if (annotated instanceof Class && !Object.class.equals(annotated)) {
    results.addAll(Arrays.asList(getAnnotations(((Class<?>) annotated).getSuperclass())));
    for (Class<?> i : ((Class<?>) annotated).getInterfaces()) {
        results.addAll(Arrays.asList(getAnnotations(i)));
} else if (annotated instanceof Method && !Object.class.equals(((Method) annotated).getDeclaringClass())) {
...
ListgetAnnotations(Annotation[][] annotations)
get Annotations
final List<Annotation> result = new LinkedList<>();
for (int i = 0; i < annotations.length; i++) {
    if (annotations[i].length > 1) {
        throw new RuntimeException("you can only assign one annotation");
    Annotation annotation = annotations[i][0];
    result.add(annotation);
return result;
SetgetAnnotations(Class cls)
get Annotations
Set<Annotation> ret = new HashSet<Annotation>();
ret.addAll(Arrays.asList(cls.getAnnotations()));
if (cls.getSuperclass() != null) {
    ret.addAll(getAnnotations(cls.getSuperclass()));
for (Class intrface : cls.getInterfaces()) {
    ret.addAll(getAnnotations(intrface));
return ret;
ListgetAnnotations(Class annotationClass, Annotation[] annotations)
get Annotations
List<Annotation> result = null;
for (Annotation annotation : annotations) {
    if (annotation.annotationType() == annotationClass) {
        if (result == null) {
            result = new ArrayList<Annotation>();
        result.add(annotation);
if (result == null) {
    return Collections.emptyList();
return result;
ListgetAnnotations(Class c, Class annotationClass)
Finds the annotation objects of annotated fields.
Field[] fields = c.getDeclaredFields();
List<T> annotations = new LinkedList<>();
for (Field field : fields) {
    T annotation = field.getAnnotation(annotationClass);
    if (annotation != null) {
        annotations.add(annotation);
return annotations;
A[]getAnnotations(Class annotation, Collection annotations)
get Annotations
final Collection<A> found = new ArrayList<>();
for (Annotation a : annotations) {
    if (annotation.isAssignableFrom(annotation)) {
        found.add(annotation.cast(a));
return found.toArray((A[]) Array.newInstance(annotation, 0));