AnnotationFinder.java

  1. package org.jbehave.core.configuration;

  2. import java.lang.annotation.Annotation;
  3. import java.lang.reflect.Method;
  4. import java.util.ArrayList;
  5. import java.util.LinkedHashSet;
  6. import java.util.List;
  7. import java.util.Set;

  8. import org.apache.commons.lang3.StringUtils;

  9. /**
  10.  * Helper class to find and retrieve annotated values
  11.  *
  12.  * @author Cristiano GaviĆ£o
  13.  * @author Brian Repko
  14.  * @author Mauro Talevi
  15.  */
  16. public class AnnotationFinder {

  17.     private final Class<?> annotatedClass;

  18.     public AnnotationFinder(Class<?> annotatedClass) {
  19.         this.annotatedClass = annotatedClass;
  20.     }

  21.     public <A extends Annotation> boolean isAnnotationPresent(Class<A> annotationClass) {
  22.         return getAnnotation(annotationClass) != null;
  23.     }

  24.     public <A extends Annotation> boolean isAnnotationValuePresent(Class<A> annotationClass, String memberName) {
  25.         Annotation annotation = getAnnotation(annotationClass);
  26.         return annotation != null && getAnnotationValue(annotation, memberName) != null;
  27.     }

  28.     @SuppressWarnings("unchecked")
  29.     public <T, A extends Annotation> T getAnnotatedValue(Class<A> annotationClass, Class<T> memberType,
  30.             String memberName) {
  31.         Annotation annotation = getAnnotation(annotationClass);
  32.         if (annotation != null) {
  33.             return (T) getAnnotationValue(annotation, memberName);
  34.         }
  35.         throw new AnnotationRequired(annotatedClass, annotationClass);
  36.     }

  37.     @SuppressWarnings("unchecked")
  38.     public <T, A extends Annotation> List<T> getAnnotatedValues(Class<A> annotationClass, Class<T> type,
  39.             String memberName) {
  40.         Set<T> set = new LinkedHashSet<>();
  41.         if (!isAnnotationPresent(annotationClass)) {
  42.             return new ArrayList<>(set);
  43.         }
  44.         Object[] values = getAnnotatedValue(annotationClass, Object[].class, memberName);
  45.         for (Object value : values) {
  46.             set.add((T) value);
  47.         }
  48.         boolean inheritValues = true;
  49.         String inheritMemberName = createInheritMemberName(memberName);
  50.         if (isAnnotationValuePresent(annotationClass, inheritMemberName)) {
  51.             inheritValues = getAnnotatedValue(annotationClass, boolean.class, inheritMemberName);
  52.         }
  53.         if (inheritValues) {
  54.             Class<?> superClass = annotatedClass.getSuperclass();
  55.             if (superClass != null && superClass != Object.class) {
  56.                 set.addAll(new AnnotationFinder(superClass).getAnnotatedValues(annotationClass, type, memberName));
  57.             }
  58.         }
  59.         return new ArrayList<>(set);
  60.     }

  61.     /**
  62.      * Creates the inherit member name by prefixing "inherit" to the capitalized member name.
  63.      *
  64.      * @param memberName the initial member name
  65.      * @return The inherit member name
  66.      */
  67.     protected String createInheritMemberName(String memberName) {
  68.         return "inherit" + StringUtils.capitalize(memberName);
  69.     }

  70.     @SuppressWarnings("unchecked")
  71.     public <T, A extends Annotation> List<Class<T>> getAnnotatedClasses(Class<A> annotationClass, Class<T> type,
  72.             String memberName) {
  73.         return (List<Class<T>>) getAnnotatedValues(annotationClass, type.getClass(), memberName);
  74.     }

  75.     protected <A extends Annotation> Annotation getAnnotation(Class<A> annotationClass) {
  76.         return annotatedClass.getAnnotation(annotationClass);
  77.     }

  78.     protected Object getAnnotationValue(Annotation annotation, String attributeName) {
  79.         try {
  80.             Method method = annotation.annotationType().getDeclaredMethod(attributeName);
  81.             return method.invoke(annotation);
  82.         } catch (Exception e) {
  83.             return null;
  84.         }
  85.     }
  86. }