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    *      http://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 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   * 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       * A method that returns itself used as a marker for cache miss,
49       * allows the underlying cache map to be strongly typed.
50       *
51       * @return itself as a method
52       */
53      public static Method cacheMiss() {
54          try {
55              return ClassMap.class.getMethod("cacheMiss");
56          } catch (final Exception xio) {
57              // this really can't make an error...
58              return null;
59          }
60      }
61  
62      /**
63       * The cache miss marker method.
64       */
65      static final Method CACHE_MISS = cacheMiss();
66      /**
67       * This is the cache to store and look up the method information.
68       * <p>
69       * It stores the association between:
70       * - a key made of a method name and an array of argument types.
71       * - a method.
72       * </p>
73       * <p>
74       * Since the invocation of the associated method is dynamic, there is no need (nor way) to differentiate between
75       * foo(int,int) and foo(Integer,Integer) since in practice only the latter form will be used through a call.
76       * This of course, applies to all 8 primitive types.
77       * </p>
78       * Uses ConcurrentMap since 3.0, marginally faster than 2.1 under contention.
79       */
80      private final Map<MethodKey, Method> byKey ;
81      /**
82       * Keep track of all methods with the same name; this is not modified after creation.
83       */
84      private final Map<String, Method[]> byName;
85      /**
86       * Cache of fields.
87       */
88      private final Map<String, Field> fieldCache;
89  
90      /**
91       * Singleton for permissions non-allowed classes.
92       */
93      private static final ClassMap EMPTY = new ClassMap();
94  
95      /**
96       * @return the empty classmap instance
97       */
98      static ClassMap empty() {
99          return EMPTY;
100     }
101 
102     /**
103      * Empty map.
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      * Standard constructor.
125      *
126      * @param aClass      the class to deconstruct.
127      * @param permissions the permissions to apply during introspection
128      * @param log         the logger.
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         // eagerly cache methods
135         create(this, permissions, aClass, log);
136         // eagerly cache public fields
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      * Find a Field using its name.
153      *
154      * @param fname the field name
155      * @return A Field object representing the field to invoke or null.
156      */
157     Field getField(final String fname) {
158         return fieldCache.get(fname);
159     }
160 
161     /**
162      * Gets the field names cached by this map.
163      *
164      * @return the array of field names
165      */
166     String[] getFieldNames() {
167         return fieldCache.keySet().toArray(new String[0]);
168     }
169 
170     /**
171      * Gets the methods names cached by this map.
172      *
173      * @return the array of method names
174      */
175     String[] getMethodNames() {
176         return byName.keySet().toArray(new String[0]);
177     }
178 
179     /**
180      * Gets all the methods with a given name from this map.
181      *
182      * @param methodName the seeked methods name
183      * @return the array of methods (null or non-empty)
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      * Find a Method using the method name and parameter objects.
195      * <p>
196      * Look in the methodMap for an entry. If found,
197      * it'll either be a CACHE_MISS, in which case we
198      * simply give up, or it'll be a Method, in which
199      * case, we return it.
200      * </p>
201      * <p>
202      * If nothing is found, then we must actually go
203      * and introspect the method from the MethodMap.
204      * </p>
205      *
206      * @param methodKey the method key
207      * @return A Method object representing the method to invoke or null.
208      * @throws MethodKey.AmbiguousException When more than one method is a match for the parameters.
209      */
210     Method getMethod(final MethodKey methodKey) throws MethodKey.AmbiguousException {
211         // Look up by key
212         Method cacheEntry = byKey.get(methodKey);
213         // We looked this up before and failed.
214         if (cacheEntry == CACHE_MISS) {
215             return null;
216         }
217         if (cacheEntry == null) {
218             try {
219                 // That one is expensive...
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                 // that's a miss :-)
227                 byKey.put(methodKey, CACHE_MISS);
228                 throw ae;
229             }
230         }
231 
232         // Yes, this might just be null.
233         return cacheEntry;
234     }
235 
236     /**
237      * Populate the Map of direct hits. These are taken from all the public methods
238      * that our class, its parents and their implemented interfaces provide.
239      *
240      * @param cache          the ClassMap instance we create
241      * @param permissions    the permissions to apply during introspection
242      * @param clazz          the class to cache
243      * @param log            the Log
244      */
245     private static void create(final ClassMap cache, final JexlPermissions permissions, final Class<?> clazz, final Log log) {
246         //
247         // Build a list of all elements in the class hierarchy. This one is bottom-first; we start
248         // with the actual declaring class and its interfaces and then move up (superclass etc.) until we
249         // hit java.lang.Object. That is important because it will give us the methods of the declaring class
250         // which might in turn be abstract further up the tree.
251         //
252         // We also ignore all SecurityExceptions that might happen due to SecurityManager restrictions.
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         // now that we've got all methods keyed in, lets organize them by name
264         if (!cache.byKey.isEmpty()) {
265             final List<Method> lm = new ArrayList<>(cache.byKey.size());
266             lm.addAll(cache.byKey.values());
267             // sort all methods by name
268             lm.sort(Comparator.comparing(Method::getName));
269             // put all lists of methods with same name in byName cache
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      * Recurses up interface hierarchy to get all super interfaces.
290      *
291      * @param cache       the cache to fill
292      * @param permissions the permissions to apply during introspection
293      * @param iface       the interface to populate the cache from
294      * @param log         the Log
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      * Recurses up class hierarchy to get all super classes.
311      *
312      * @param cache       the cache to fill
313      * @param permissions the permissions to apply during introspection
314      * @param clazz       the class to populate the cache from
315      * @param log         the Log
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                 // method must be public
325                 if (!Modifier.isPublic(mi.getModifiers())) {
326                     continue;
327                 }
328                 // add method to byKey cache; do not override
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                     // foo(int) and foo(Integer) have the same signature for JEXL
333                     log.debug("Method " + pmi + " is already registered, key: " + key.debugString());
334                 }
335             }
336         } catch (final SecurityException se) {
337             // Everybody feels better with...
338             if (log.isDebugEnabled()) {
339                 log.debug("While accessing methods of " + clazz + ": ", se);
340             }
341         }
342     }
343 }