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