##### Coverage Report - org.jbehave.core.configuration.AnnotationFinder

Classes in this File | Line Coverage | Branch Coverage | Complexity
AnnotationFinder |
100% | 32/32
95% | 19/20
| 2.4444444444444446;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.LinkedHashSet;
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 | 54 |
         public AnnotationFinder(Class<?> annotatedClass) {
24 | 54 |
             this.annotatedClass = annotatedClass;
25 | 54 |
         }
26 |  |
27 |  |
         public <A extends Annotation> boolean isAnnotationPresent(Class<A> annotationClass) {
28 | 108 |
             return getAnnotation(annotationClass) != null;
29 |  |
         }
30 |  |
31 |  |
         public <A extends Annotation> boolean isAnnotationValuePresent(Class<A> annotationClass, String memberName) {
32 | 45 |
             Annotation annotation = getAnnotation(annotationClass);
33 | 45 |
             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 | 412 |
             Annotation annotation = getAnnotation(annotationClass);
40 | 412 |
             if (annotation != null) {
41 | 411 |
                 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 | 64 |
             Set<T> set = new LinkedHashSet<T>();
50 | 64 |
             if (!isAnnotationPresent(annotationClass)) {
51 | 22 |
                 return new ArrayList<T>(set);
52 |  |
             }
53 | 42 |
             Object[] values = getAnnotatedValue(annotationClass, Object[].class, memberName);
54 | 75 |
             for (Object value : values) {
55 | 33 |
                 set.add((T) value);
56 |  |
             }
57 | 42 |
             boolean inheritValues = true;
58 | 42 |
             String inheritMemberName = createInheritMemberName(memberName);
59 | 42 |
             if (isAnnotationValuePresent(annotationClass, inheritMemberName)) {
60 | 31 |
                 inheritValues = getAnnotatedValue(annotationClass, boolean.class, inheritMemberName);
61 |  |
             }
62 | 42 |
             if (inheritValues) {
63 | 40 |
                 Class<?> superClass = annotatedClass.getSuperclass();
64 | 40 |
                 if (superClass != null && superClass != Object.class) {
65 | 27 |
                     set.addAll(new AnnotationFinder(superClass).getAnnotatedValues(annotationClass, type, memberName));
66 |  |
                 }
67 |  |
             }
68 | 42 |
             return new ArrayList<T>(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 | 42 |
             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 | 23 |
             return (List<Class<T>>) getAnnotatedValues(annotationClass, type.getClass(), memberName);
86 |  |
         }
87 |  |
88 |  |
         protected <A extends Annotation> Annotation getAnnotation(Class<A> annotationClass) {
89 | 565 |
             return annotatedClass.getAnnotation(annotationClass);
90 |  |
         }
91 |  |
92 |  |
         protected Object getAnnotationValue(Annotation annotation, String attributeName) {
93 |  |
             try {
94 | 455 |
                 Method method = annotation.annotationType().getDeclaredMethod(attributeName, new Class[0]);
95 | 443 |
                 return method.invoke(annotation);
96 | 12 |
             } catch (Exception e) {
97 | 12 |
                 return null;
98 |  |
             }
99 |  |
         }
100 |  |
     }