Coverage Report - org.jbehave.core.configuration.AnnotationFinder
 
Classes in this File Line Coverage Branch Coverage Complexity
AnnotationFinder
100%
32/32
90%
18/20
2.444
 
 1  
 package org.jbehave.core.configuration;
 2  
 
 3  
 import java.lang.annotation.Annotation;
 4  
 import java.lang.reflect.Method;
 5  
 import java.util.ArrayList;
 6  
 import java.util.HashSet;
 7  
 import java.util.List;
 8  
 import java.util.Set;
 9  
 
 10  
 import org.apache.commons.lang.StringUtils;
 11  
 
 12  
 /**
 13  
  * Helper class to find and retrieve annotated values
 14  
  * 
 15  
  * @author Cristiano GaviĆ£o
 16  
  * @author Brian Repko
 17  
  * @author Mauro Talevi
 18  
  */
 19  
 public class AnnotationFinder {
 20  
 
 21  
     private final Class<?> annotatedClass;
 22  
 
 23  34
     public AnnotationFinder(Class<?> annotatedClass) {
 24  34
         this.annotatedClass = annotatedClass;
 25  34
     }
 26  
 
 27  
     public <A extends Annotation> boolean isAnnotationPresent(Class<A> annotationClass) {
 28  65
         return getAnnotation(annotationClass) != null;
 29  
     }
 30  
 
 31  
     public <A extends Annotation> boolean isAnnotationValuePresent(Class<A> annotationClass, String memberName) {
 32  28
         Annotation annotation = getAnnotation(annotationClass);
 33  28
         return annotation != null && getAnnotationValue(annotation, memberName) != null;
 34  
     }
 35  
 
 36  
     @SuppressWarnings("unchecked")
 37  
     public <T, A extends Annotation> T getAnnotatedValue(Class<A> annotationClass, Class<T> memberType,
 38  
             String memberName) {
 39  222
         Annotation annotation = getAnnotation(annotationClass);
 40  222
         if (annotation != null) {
 41  221
             return (T) getAnnotationValue(annotation, memberName);
 42  
         }
 43  1
         throw new AnnotationRequired(annotatedClass, annotationClass);
 44  
     }
 45  
 
 46  
     @SuppressWarnings("unchecked")
 47  
     public <T, A extends Annotation> List<T> getAnnotatedValues(Class<A> annotationClass, Class<T> type,
 48  
             String memberName) {
 49  33
         Set<T> set = new HashSet<T>();
 50  33
         if (!isAnnotationPresent(annotationClass)) {
 51  8
             return new ArrayList(set);
 52  
         }
 53  25
         Object[] values = getAnnotatedValue(annotationClass, Object[].class, memberName);
 54  50
         for (Object value : values) {
 55  25
             set.add((T) value);
 56  
         }
 57  25
         boolean inheritValues = true;
 58  25
         String inheritMemberName = createInheritMemberName(memberName);
 59  25
         if (isAnnotationValuePresent(annotationClass, inheritMemberName)) {
 60  25
             inheritValues = getAnnotatedValue(annotationClass, boolean.class, inheritMemberName);
 61  
         }
 62  25
         if (inheritValues) {
 63  23
             Class<?> superClass = annotatedClass.getSuperclass();
 64  23
             if (superClass != null && superClass != Object.class) {
 65  13
                 set.addAll(new AnnotationFinder(superClass).getAnnotatedValues(annotationClass, type, memberName));
 66  
             }
 67  
         }
 68  25
         return new ArrayList(set);
 69  
     }
 70  
 
 71  
     /**
 72  
      * Creates the inherit member name by prefixing "inherit" to the capitalized
 73  
      * member name.
 74  
      * 
 75  
      * @param memberName
 76  
      * @return The inherit member name
 77  
      */
 78  
     protected String createInheritMemberName(String memberName) {
 79  25
         return "inherit" + StringUtils.capitalize(memberName);
 80  
     }
 81  
 
 82  
     @SuppressWarnings("unchecked")
 83  
     public <T, A extends Annotation> List<Class<T>> getAnnotatedClasses(Class<A> annotationClass, Class<T> type,
 84  
             String memberName) {
 85  17
         return (List<Class<T>>) getAnnotatedValues(annotationClass, type.getClass(), memberName);
 86  
     }
 87  
 
 88  
     protected <A extends Annotation> Annotation getAnnotation(Class<A> annotationClass) {
 89  315
         return annotatedClass.getAnnotation(annotationClass);
 90  
     }
 91  
 
 92  
     protected Object getAnnotationValue(Annotation annotation, String attributeName) {
 93  
         try {
 94  248
             Method method = annotation.annotationType().getDeclaredMethod(attributeName, new Class[0]);
 95  247
             return method.invoke(annotation);
 96  1
         } catch (Exception e) {
 97  1
             return null;
 98  
         }
 99  
     }
 100  
 }