View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * A cache of introspection information for a specific class instance.
37   * Keys objects by an aggregation of the method name and the classes
38   * that make up the parameters.
39   * <p>
40   * Originally taken from the Velocity tree so we can be self-sufficient.
41   * </p>
42   *
43   * @see MethodKey
44   * @since 1.0
45   */
46  final class ClassMap {
47  
48      /**
49       * The cache miss marker method.
50       */
51      static final Method CACHE_MISS = cacheMiss();
52  
53      /**
54       * Singleton for permissions non-allowed classes.
55       */
56      private static final ClassMap EMPTY = new ClassMap();
57  
58      /**
59       * A method that returns itself used as a marker for cache miss,
60       * allows the underlying cache map to be strongly typed.
61       *
62       * @return itself as a method
63       */
64      public static Method cacheMiss() {
65          try {
66              return ClassMap.class.getMethod("cacheMiss");
67          } catch (final Exception xio) {
68              // this really can't make an error...
69              return null;
70          }
71      }
72  
73      /**
74       * Populate the Map of direct hits. These are taken from all the public methods
75       * that our class, its parents and their implemented interfaces provide.
76       *
77       * @param cache          the ClassMap instance we create
78       * @param permissions    the permissions to apply during introspection
79       * @param clazz          the class to cache
80       * @param log            the Log
81       */
82      private static void create(final ClassMap cache, final JexlPermissions permissions, final Class<?> clazz, final Log log) {
83          //
84          // Build a list of all elements in the class hierarchy. This one is bottom-first; we start
85          // with the actual declaring class and its interfaces and then move up (superclass etc.) until we
86          // hit java.lang.Object. That is important because it will give us the methods of the declaring class
87          // which might in turn be abstract further up the tree.
88          //
89          // We also ignore all SecurityExceptions that might happen due to SecurityManager restrictions.
90          //
91          for (Class<?> classToReflect = clazz; classToReflect != null; classToReflect = classToReflect.getSuperclass()) {
92              if (Modifier.isPublic(classToReflect.getModifiers()) && ClassTool.isExported(classToReflect)) {
93                  populateWithClass(cache, permissions, classToReflect, log);
94              }
95              final Class<?>[] interfaces = classToReflect.getInterfaces();
96              for (final Class<?> anInterface : interfaces) {
97                  populateWithInterface(cache, permissions, anInterface, log);
98              }
99          }
100         // now that we've got all methods keyed in, lets organize them by name
101         if (!cache.byKey.isEmpty()) {
102             final List<Method> lm = new ArrayList<>(cache.byKey.values());
103             // sort all methods by name
104             lm.sort(Comparator.comparing(Method::getName));
105             // put all lists of methods with same name in byName cache
106             int start = 0;
107             while (start < lm.size()) {
108                 final String name = lm.get(start).getName();
109                 int end = start + 1;
110                 while (end < lm.size()) {
111                     final String walk = lm.get(end).getName();
112                     if (!walk.equals(name)) {
113                         break;
114                     }
115                     end += 1;
116                 }
117                 final Method[] lmn = lm.subList(start, end).toArray(new Method[0]);
118                 cache.byName.put(name, lmn);
119                 start = end;
120             }
121         }
122     }
123 
124     /**
125      * @return the empty classmap instance
126      */
127     static ClassMap empty() {
128         return EMPTY;
129     }
130 
131     /**
132      * Recurses up class hierarchy to get all super classes.
133      *
134      * @param cache       the cache to fill
135      * @param permissions the permissions to apply during introspection
136      * @param clazz       the class to populate the cache from
137      * @param log         the Log
138      */
139     private static void populateWithClass(final ClassMap cache,
140                                           final JexlPermissions permissions,
141                                           final Class<?> clazz,
142                                           final Log log) {
143         try {
144             final Method[] methods = clazz.getDeclaredMethods();
145             for (final Method mi : methods) {
146                 // method must be public
147                 if (!Modifier.isPublic(mi.getModifiers())) {
148                     continue;
149                 }
150                 // add method to byKey cache; do not override
151                 final MethodKey key = new MethodKey(mi);
152                 final Method pmi = cache.byKey.putIfAbsent(key, permissions.allow(mi) ? mi : CACHE_MISS);
153                 if (pmi != null && pmi != CACHE_MISS && log.isDebugEnabled() && !key.equals(new MethodKey(pmi))) {
154                     // foo(int) and foo(Integer) have the same signature for JEXL
155                     log.debug("Method " + pmi + " is already registered, key: " + key.debugString());
156                 }
157             }
158         } catch (final SecurityException se) {
159             // Everybody feels better with...
160             if (log.isDebugEnabled()) {
161                 log.debug("While accessing methods of " + clazz + ": ", se);
162             }
163         }
164     }
165 
166     /**
167      * Recurses up interface hierarchy to get all super interfaces.
168      *
169      * @param cache       the cache to fill
170      * @param permissions the permissions to apply during introspection
171      * @param iface       the interface to populate the cache from
172      * @param log         the Log
173      */
174     private static void populateWithInterface(final ClassMap cache,
175                                               final JexlPermissions permissions,
176                                               final Class<?> iface,
177                                               final Log log) {
178         if (Modifier.isPublic(iface.getModifiers())) {
179             populateWithClass(cache, permissions, iface, log);
180             final Class<?>[] supers = iface.getInterfaces();
181             for (final Class<?> aSuper : supers) {
182                 populateWithInterface(cache, permissions, aSuper, log);
183             }
184         }
185     }
186 
187     /**
188      * This is the cache to store and look up the method information.
189      * <p>
190      * It stores the association between:
191      * - a key made of a method name and an array of argument types.
192      * - a method.
193      * </p>
194      * <p>
195      * Since the invocation of the associated method is dynamic, there is no need (nor way) to differentiate between
196      * foo(int, int) and foo(Integer, Integer) since in practice only the latter form will be used through a call.
197      * This of course, applies to all 8 primitive types.
198      * </p>
199      * Uses ConcurrentMap since 3.0, marginally faster than 2.1 under contention.
200      */
201     private final Map<MethodKey, Method> byKey ;
202 
203     /**
204      * Keep track of all methods with the same name; this is not modified after creation.
205      */
206     private final Map<String, Method[]> byName;
207 
208     /**
209      * Cache of fields.
210      */
211     private final Map<String, Field> fieldCache;
212 
213     /**
214      * Empty map.
215      */
216     private ClassMap() {
217         this.byKey = Collections.unmodifiableMap(new AbstractMap<MethodKey, Method>() {
218             @Override
219             public Set<Entry<MethodKey, Method>> entrySet() {
220                 return Collections.emptySet();
221             }
222             @Override public Method get(final Object name) {
223                 return CACHE_MISS;
224             }
225             @Override
226             public String toString() {
227                 return "emptyClassMap{}";
228             }
229         });
230         this.byName = Collections.emptyMap();
231         this.fieldCache = Collections.emptyMap();
232     }
233 
234     /**
235      * Standard constructor.
236      *
237      * @param aClass      the class to deconstruct.
238      * @param permissions the permissions to apply during introspection
239      * @param log         the logger.
240      */
241     @SuppressWarnings("LeakingThisInConstructor")
242     ClassMap(final Class<?> aClass, final JexlPermissions permissions, final Log log) {
243         this.byKey = new ConcurrentHashMap<>();
244         this.byName = new HashMap<>();
245         // eagerly cache methods
246         create(this, permissions, aClass, log);
247         // eagerly cache public fields
248         final Field[] fields = aClass.getFields();
249         if (fields.length > 0) {
250             final Map<String, Field> cache = new HashMap<>();
251             for (final Field field : fields) {
252                 if (permissions.allow(field)) {
253                     cache.put(field.getName(), field);
254                 }
255             }
256             fieldCache = cache;
257         } else {
258             fieldCache = Collections.emptyMap();
259         }
260     }
261 
262     /**
263      * Find a Field using its name.
264      *
265      * @param fieldName the field name
266      * @return A Field object representing the field to invoke or null.
267      */
268     Field getField(final String fieldName) {
269         return fieldCache.get(fieldName);
270     }
271 
272     /**
273      * Gets the field names cached by this map.
274      *
275      * @return the array of field names
276      */
277     String[] getFieldNames() {
278         return fieldCache.keySet().toArray(new String[0]);
279     }
280 
281     /**
282      * Find a Method using the method name and parameter objects.
283      * <p>
284      * Look in the methodMap for an entry. If found,
285      * it'll either be a CACHE_MISS, in which case we
286      * simply give up, or it'll be a Method, in which
287      * case, we return it.
288      * </p>
289      * <p>
290      * If nothing is found, then we must actually go
291      * and introspect the method from the MethodMap.
292      * </p>
293      *
294      * @param methodKey the method key
295      * @return A Method object representing the method to invoke or null.
296      * @throws MethodKey.AmbiguousException When more than one method is a match for the parameters.
297      */
298     Method getMethod(final MethodKey methodKey) throws MethodKey.AmbiguousException {
299         // Look up by key
300         Method cacheEntry = byKey.get(methodKey);
301         // We looked this up before and failed.
302         if (cacheEntry == CACHE_MISS) {
303             return null;
304         }
305         if (cacheEntry == null) {
306             try {
307                 // That one is expensive...
308                 final Method[] methodList = byName.get(methodKey.getMethod());
309                 if (methodList != null) {
310                     cacheEntry = methodKey.getMostSpecificMethod(methodList);
311                 }
312                 byKey.put(methodKey, cacheEntry == null ? CACHE_MISS : cacheEntry);
313             } catch (final MethodKey.AmbiguousException ae) {
314                 // that's a miss :-)
315                 byKey.put(methodKey, CACHE_MISS);
316                 throw ae;
317             }
318         }
319 
320         // Yes, this might just be null.
321         return cacheEntry;
322     }
323 
324     /**
325      * Gets the methods names cached by this map.
326      *
327      * @return the array of method names
328      */
329     String[] getMethodNames() {
330         return byName.keySet().toArray(new String[0]);
331     }
332 
333     /**
334      * Gets all the methods with a given name from this map.
335      *
336      * @param methodName the seeked methods name
337      * @return the array of methods (null or non-empty)
338      */
339     Method[] getMethods(final String methodName) {
340         final Method[] lm = byName.get(methodName);
341         if (lm != null && lm.length > 0) {
342             return lm.clone();
343         }
344         return null;
345     }
346 }