1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jexl3.internal.introspection;
18
19 import org.apache.commons.jexl3.introspection.JexlPermissions;
20 import org.apache.commons.logging.Log;
21
22 import java.lang.reflect.Field;
23 import java.lang.reflect.Method;
24 import java.lang.reflect.Modifier;
25 import java.util.AbstractMap;
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.Comparator;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Set;
33 import java.util.concurrent.ConcurrentHashMap;
34
35
36
37
38
39
40
41
42
43
44
45
46 final class ClassMap {
47
48
49
50
51
52
53 public static Method cacheMiss() {
54 try {
55 return ClassMap.class.getMethod("cacheMiss");
56 } catch (final Exception xio) {
57
58 return null;
59 }
60 }
61
62
63
64
65 static final Method CACHE_MISS = cacheMiss();
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 private final Map<MethodKey, Method> byKey ;
81
82
83
84 private final Map<String, Method[]> byName;
85
86
87
88 private final Map<String, Field> fieldCache;
89
90
91
92
93 private static final ClassMap EMPTY = new ClassMap();
94
95
96
97
98 static ClassMap empty() {
99 return EMPTY;
100 }
101
102
103
104
105 private ClassMap() {
106 this.byKey = Collections.unmodifiableMap(new AbstractMap<MethodKey, Method>() {
107 @Override
108 public String toString() {
109 return "emptyClassMap{}";
110 }
111 @Override
112 public Set<Entry<MethodKey, Method>> entrySet() {
113 return Collections.emptySet();
114 }
115 @Override public Method get(final Object name) {
116 return CACHE_MISS;
117 }
118 });
119 this.byName = Collections.emptyMap();
120 this.fieldCache = Collections.emptyMap();
121 }
122
123
124
125
126
127
128
129
130 @SuppressWarnings("LeakingThisInConstructor")
131 ClassMap(final Class<?> aClass, final JexlPermissions permissions, final Log log) {
132 this.byKey = new ConcurrentHashMap<>();
133 this.byName = new HashMap<>();
134
135 create(this, permissions, aClass, log);
136
137 final Field[] fields = aClass.getFields();
138 if (fields.length > 0) {
139 final Map<String, Field> cache = new HashMap<>();
140 for (final Field field : fields) {
141 if (permissions.allow(field)) {
142 cache.put(field.getName(), field);
143 }
144 }
145 fieldCache = cache;
146 } else {
147 fieldCache = Collections.emptyMap();
148 }
149 }
150
151
152
153
154
155
156
157 Field getField(final String fname) {
158 return fieldCache.get(fname);
159 }
160
161
162
163
164
165
166 String[] getFieldNames() {
167 return fieldCache.keySet().toArray(new String[0]);
168 }
169
170
171
172
173
174
175 String[] getMethodNames() {
176 return byName.keySet().toArray(new String[0]);
177 }
178
179
180
181
182
183
184
185 Method[] getMethods(final String methodName) {
186 final Method[] lm = byName.get(methodName);
187 if (lm != null && lm.length > 0) {
188 return lm.clone();
189 }
190 return null;
191 }
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210 Method getMethod(final MethodKey methodKey) throws MethodKey.AmbiguousException {
211
212 Method cacheEntry = byKey.get(methodKey);
213
214 if (cacheEntry == CACHE_MISS) {
215 return null;
216 }
217 if (cacheEntry == null) {
218 try {
219
220 final Method[] methodList = byName.get(methodKey.getMethod());
221 if (methodList != null) {
222 cacheEntry = methodKey.getMostSpecificMethod(methodList);
223 }
224 byKey.put(methodKey, cacheEntry == null? CACHE_MISS : cacheEntry);
225 } catch (final MethodKey.AmbiguousException ae) {
226
227 byKey.put(methodKey, CACHE_MISS);
228 throw ae;
229 }
230 }
231
232
233 return cacheEntry;
234 }
235
236
237
238
239
240
241
242
243
244
245 private static void create(final ClassMap cache, final JexlPermissions permissions, final Class<?> clazz, final Log log) {
246
247
248
249
250
251
252
253
254 for (Class<?> classToReflect = clazz; classToReflect != null; classToReflect = classToReflect.getSuperclass()) {
255 if (Modifier.isPublic(classToReflect.getModifiers()) && ClassTool.isExported(classToReflect)) {
256 populateWithClass(cache, permissions, classToReflect, log);
257 }
258 final Class<?>[] interfaces = classToReflect.getInterfaces();
259 for (final Class<?> anInterface : interfaces) {
260 populateWithInterface(cache, permissions, anInterface, log);
261 }
262 }
263
264 if (!cache.byKey.isEmpty()) {
265 final List<Method> lm = new ArrayList<>(cache.byKey.size());
266 lm.addAll(cache.byKey.values());
267
268 lm.sort(Comparator.comparing(Method::getName));
269
270 int start = 0;
271 while (start < lm.size()) {
272 final String name = lm.get(start).getName();
273 int end = start + 1;
274 while (end < lm.size()) {
275 final String walk = lm.get(end).getName();
276 if (!walk.equals(name)) {
277 break;
278 }
279 end += 1;
280 }
281 final Method[] lmn = lm.subList(start, end).toArray(new Method[0]);
282 cache.byName.put(name, lmn);
283 start = end;
284 }
285 }
286 }
287
288
289
290
291
292
293
294
295
296 private static void populateWithInterface(final ClassMap cache,
297 final JexlPermissions permissions,
298 final Class<?> iface,
299 final Log log) {
300 if (Modifier.isPublic(iface.getModifiers())) {
301 populateWithClass(cache, permissions, iface, log);
302 final Class<?>[] supers = iface.getInterfaces();
303 for (final Class<?> aSuper : supers) {
304 populateWithInterface(cache, permissions, aSuper, log);
305 }
306 }
307 }
308
309
310
311
312
313
314
315
316
317 private static void populateWithClass(final ClassMap cache,
318 final JexlPermissions permissions,
319 final Class<?> clazz,
320 final Log log) {
321 try {
322 final Method[] methods = clazz.getDeclaredMethods();
323 for (final Method mi : methods) {
324
325 if (!Modifier.isPublic(mi.getModifiers())) {
326 continue;
327 }
328
329 final MethodKey key = new MethodKey(mi);
330 final Method pmi = cache.byKey.putIfAbsent(key, permissions.allow(mi) ? mi : CACHE_MISS);
331 if (pmi != null && pmi != CACHE_MISS && log.isDebugEnabled() && !key.equals(new MethodKey(pmi))) {
332
333 log.debug("Method " + pmi + " is already registered, key: " + key.debugString());
334 }
335 }
336 } catch (final SecurityException se) {
337
338 if (log.isDebugEnabled()) {
339 log.debug("While accessing methods of " + clazz + ": ", se);
340 }
341 }
342 }
343 }