1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.uruma.util; |
17 | |
|
18 | |
import java.lang.annotation.Annotation; |
19 | |
import java.lang.reflect.Field; |
20 | |
import java.lang.reflect.Method; |
21 | |
import java.util.ArrayList; |
22 | |
import java.util.HashMap; |
23 | |
import java.util.List; |
24 | |
import java.util.Map; |
25 | |
|
26 | |
import org.seasar.framework.beans.BeanDesc; |
27 | |
import org.seasar.framework.beans.PropertyDesc; |
28 | |
import org.seasar.framework.beans.factory.BeanDescFactory; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
public class AnnotationUtil { |
36 | 4 | private static Map<ClassEntry, List<Field>> fieldCache = new HashMap<ClassEntry, List<Field>>(); |
37 | |
|
38 | 4 | private static Map<ClassEntry, List<Method>> methodCache = new HashMap<ClassEntry, List<Method>>(); |
39 | |
|
40 | 4 | private static Map<ClassEntry, List<PropertyDesc>> pdCache = new HashMap<ClassEntry, List<PropertyDesc>>(); |
41 | |
|
42 | 0 | protected AnnotationUtil() { |
43 | 0 | } |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
public static List<Field> getAnnotatedFields(final Class<?> clazz, |
60 | |
final Class<? extends Annotation> annotationClass) { |
61 | 8 | ClassEntry entry = new ClassEntry(clazz, annotationClass); |
62 | 8 | List<Field> result = fieldCache.get(entry); |
63 | 8 | if (result != null) { |
64 | 4 | return result; |
65 | |
} else { |
66 | 4 | result = new ArrayList<Field>(); |
67 | 4 | BeanDesc desc = BeanDescFactory.getBeanDesc(clazz); |
68 | 4 | int fieldSize = desc.getFieldSize(); |
69 | 28 | for (int i = 0; i < fieldSize; i++) { |
70 | 24 | Field field = desc.getField(i); |
71 | 24 | if (field.getAnnotation(annotationClass) != null) { |
72 | 12 | result.add(field); |
73 | 12 | fieldCache.put(entry, result); |
74 | |
} |
75 | |
} |
76 | 4 | return result; |
77 | |
} |
78 | |
} |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
public static List<Method> getAnnotatedMethods(final Class<?> clazz, |
95 | |
final Class<? extends Annotation> annotationClass) { |
96 | 0 | ClassEntry entry = new ClassEntry(clazz, annotationClass); |
97 | 0 | List<Method> result = methodCache.get(entry); |
98 | 0 | if (result != null) { |
99 | 0 | return result; |
100 | |
} else { |
101 | 0 | result = new ArrayList<Method>(); |
102 | 0 | BeanDesc desc = BeanDescFactory.getBeanDesc(clazz); |
103 | |
|
104 | 0 | String[] methodNames = desc.getMethodNames(); |
105 | 0 | for (int i = 0; i < methodNames.length; i++) { |
106 | 0 | Method[] methods = desc.getMethods(methodNames[i]); |
107 | |
|
108 | 0 | for (int j = 0; j < methods.length; j++) { |
109 | 0 | if (methods[j].isAnnotationPresent(annotationClass)) { |
110 | 0 | result.add(methods[j]); |
111 | |
} |
112 | |
} |
113 | |
} |
114 | 0 | methodCache.put(entry, result); |
115 | |
|
116 | 0 | return result; |
117 | |
} |
118 | |
} |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
public static List<PropertyDesc> getAnnotatedPropertyDescs( |
135 | |
final Class<?> clazz, |
136 | |
final Class<? extends Annotation> annotationClass) { |
137 | 140 | ClassEntry entry = new ClassEntry(clazz, annotationClass); |
138 | 140 | List<PropertyDesc> result = pdCache.get(entry); |
139 | 140 | if (result != null) { |
140 | 108 | return result; |
141 | |
} else { |
142 | 32 | result = new ArrayList<PropertyDesc>(); |
143 | 32 | BeanDesc desc = BeanDescFactory.getBeanDesc(clazz); |
144 | 32 | int fieldSize = desc.getFieldSize(); |
145 | 268 | for (int i = 0; i < fieldSize; i++) { |
146 | 236 | Field field = desc.getField(i); |
147 | 236 | if (field.getAnnotation(annotationClass) != null) { |
148 | 128 | String propertyName = field.getName(); |
149 | 128 | if (desc.hasPropertyDesc(propertyName)) { |
150 | 128 | result.add(desc.getPropertyDesc(propertyName)); |
151 | 128 | pdCache.put(entry, result); |
152 | |
} |
153 | |
} |
154 | |
} |
155 | 32 | return result; |
156 | |
} |
157 | |
} |
158 | |
|
159 | |
private static class ClassEntry { |
160 | |
private Class<?> clazz; |
161 | |
|
162 | |
private Class<? extends Annotation> annotationClass; |
163 | |
|
164 | |
ClassEntry(final Class<?> clazz, |
165 | 148 | final Class<? extends Annotation> annotationClass) { |
166 | 148 | this.clazz = clazz; |
167 | 148 | this.annotationClass = annotationClass; |
168 | 148 | } |
169 | |
|
170 | |
@Override |
171 | |
public int hashCode() { |
172 | 288 | final int PRIME = 31; |
173 | 288 | int result = 1; |
174 | 288 | result = PRIME |
175 | |
* result |
176 | |
+ ((this.annotationClass == null) ? 0 |
177 | |
: this.annotationClass.hashCode()); |
178 | 288 | result = PRIME * result |
179 | |
+ ((this.clazz == null) ? 0 : this.clazz.hashCode()); |
180 | 288 | return result; |
181 | |
} |
182 | |
|
183 | |
@Override |
184 | |
public boolean equals(final Object obj) { |
185 | 112 | if (this == obj) |
186 | 0 | return true; |
187 | 112 | if (obj == null) |
188 | 0 | return false; |
189 | 112 | if (getClass() != obj.getClass()) |
190 | 0 | return false; |
191 | 112 | final ClassEntry other = (ClassEntry) obj; |
192 | 112 | if (this.annotationClass == null) { |
193 | 0 | if (other.annotationClass != null) |
194 | 0 | return false; |
195 | 112 | } else if (!this.annotationClass.equals(other.annotationClass)) |
196 | 0 | return false; |
197 | 112 | if (this.clazz == null) { |
198 | 0 | if (other.clazz != null) |
199 | 0 | return false; |
200 | 112 | } else if (!this.clazz.equals(other.clazz)) |
201 | 0 | return false; |
202 | 112 | return true; |
203 | |
} |
204 | |
} |
205 | |
} |