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 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
public class AnnotationFinder { |
20 | |
|
21 | |
private final Class<?> annotatedClass; |
22 | |
|
23 | 44 | public AnnotationFinder(Class<?> annotatedClass) { |
24 | 44 | this.annotatedClass = annotatedClass; |
25 | 44 | } |
26 | |
|
27 | |
public <A extends Annotation> boolean isAnnotationPresent(Class<A> annotationClass) { |
28 | 87 | return getAnnotation(annotationClass) != null; |
29 | |
} |
30 | |
|
31 | |
public <A extends Annotation> boolean isAnnotationValuePresent(Class<A> annotationClass, String memberName) { |
32 | 38 | Annotation annotation = getAnnotation(annotationClass); |
33 | 38 | 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 | 272 | Annotation annotation = getAnnotation(annotationClass); |
40 | 272 | if (annotation != null) { |
41 | 271 | 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 | 50 | Set<T> set = new HashSet<T>(); |
50 | 50 | if (!isAnnotationPresent(annotationClass)) { |
51 | 15 | return new ArrayList<T>(set); |
52 | |
} |
53 | 35 | Object[] values = getAnnotatedValue(annotationClass, Object[].class, memberName); |
54 | 64 | for (Object value : values) { |
55 | 29 | set.add((T) value); |
56 | |
} |
57 | 35 | boolean inheritValues = true; |
58 | 35 | String inheritMemberName = createInheritMemberName(memberName); |
59 | 35 | if (isAnnotationValuePresent(annotationClass, inheritMemberName)) { |
60 | 26 | inheritValues = getAnnotatedValue(annotationClass, boolean.class, inheritMemberName); |
61 | |
} |
62 | 35 | if (inheritValues) { |
63 | 33 | Class<?> superClass = annotatedClass.getSuperclass(); |
64 | 33 | if (superClass != null && superClass != Object.class) { |
65 | 20 | set.addAll(new AnnotationFinder(superClass).getAnnotatedValues(annotationClass, type, memberName)); |
66 | |
} |
67 | |
} |
68 | 35 | return new ArrayList<T>(set); |
69 | |
} |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
protected String createInheritMemberName(String memberName) { |
79 | 35 | 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 | 18 | return (List<Class<T>>) getAnnotatedValues(annotationClass, type.getClass(), memberName); |
86 | |
} |
87 | |
|
88 | |
protected <A extends Annotation> Annotation getAnnotation(Class<A> annotationClass) { |
89 | 397 | return annotatedClass.getAnnotation(annotationClass); |
90 | |
} |
91 | |
|
92 | |
protected Object getAnnotationValue(Annotation annotation, String attributeName) { |
93 | |
try { |
94 | 308 | Method method = annotation.annotationType().getDeclaredMethod(attributeName, new Class[0]); |
95 | 298 | return method.invoke(annotation); |
96 | 10 | } catch (Exception e) { |
97 | 10 | return null; |
98 | |
} |
99 | |
} |
100 | |
} |