Java Reflection Annotation getAnnotations(Enum targetEnum, Class targetAnnotationClass)

Here you can find the source of getAnnotations(Enum targetEnum, Class targetAnnotationClass)

Description

get Annotations

License

Apache License

Declaration

private static List<Annotation> getAnnotations(Enum<?> targetEnum,
        Class<? extends Annotation> targetAnnotationClass) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;

public class Main {

    private static List<Annotation> getAnnotations(Enum<?> targetEnum,
            Class<? extends Annotation> targetAnnotationClass) {

        Annotation[] allAnnotations;
        try {//from w  ww . j a va 2  s .  c  o  m
            allAnnotations = targetEnum.getClass().getField(targetEnum.name()).getAnnotations();
        } catch (Exception e) {
            throw new IllegalStateException("It failed to get annotations. targetEnum = [" + targetEnum + "]", e);
        }

        List<Annotation> annotations = new ArrayList<Annotation>();
        for (Annotation annotation : allAnnotations) {
            if (annotation.annotationType().getAnnotation(targetAnnotationClass) != null) {
                annotations.add(annotation);
            }
        }
        return annotations;
    }
}

Related

  1. getAnnotations(Class cls)
  2. getAnnotations(Class annotationClass, Annotation[] annotations)
  3. getAnnotations(Class c, Class annotationClass)
  4. getAnnotations(Class annotation, Collection annotations)
  5. getAnnotations(Class ann, Object o, Method m, int param)
  6. getAnnotations(Field field)
  7. getAnnotations(final Class c, final Class annClass)
  8. getAnnotations(final Class clazz, final Class annotation)
  9. getAnnotations(final Class clazz, final String fieldName)