Class Introspector
- java.lang.Object
-
- org.apache.commons.jexl3.internal.introspection.Introspector
-
public final class Introspector extends java.lang.Object
This basic function of this class is to return a Method object for a particular class given the name of a method and the parameters to the method in the form of an Object[].The first time the Introspector sees a class it creates a class method map for the class in question. Basically the class method map is a Hashtable where Method objects are keyed by the aggregation of the method name and the array of parameters classes. This mapping is performed for all the public methods of a class and stored.
- Since:
- 1.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
Introspector.CacheMiss
A Constructor get cache-miss.
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<java.lang.Class<?>,ClassMap>
classMethodMaps
Holds the method maps for the classes we know about, keyed by Class.private java.util.Map<java.lang.String,java.lang.Class<?>>
constructibleClasses
Holds the set of classes we have introspected.private java.util.Map<MethodKey,java.lang.reflect.Constructor<?>>
constructorsMap
Holds the map of classes ctors we know about as well as unknown ones.private static java.lang.reflect.Constructor<?>
CTOR_MISS
The cache-miss marker for the constructors map.private java.lang.ClassLoader
loader
The class loader used to solve constructors if needed.private java.util.concurrent.locks.ReadWriteLock
lock
The read/write lock.private org.apache.commons.logging.Log
logger
the logger.private JexlPermissions
permissions
The permissions.
-
Constructor Summary
Constructors Constructor Description Introspector(org.apache.commons.logging.Log log, java.lang.ClassLoader cloader)
Create the introspector.Introspector(org.apache.commons.logging.Log log, java.lang.ClassLoader cloader, JexlPermissions perms)
Create the introspector.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Class<?>
getClassByName(java.lang.String className)
Gets a class by name through this introspector class loader.java.lang.reflect.Constructor<?>
getConstructor(java.lang.Class<?> c, MethodKey key)
Gets the constructor defined by theMethodKey
.java.lang.reflect.Constructor<?>
getConstructor(MethodKey key)
Gets the constructor defined by theMethodKey
.java.lang.reflect.Field
getField(java.lang.Class<?> c, java.lang.String key)
Gets the field named bykey
for the classc
.java.lang.String[]
getFieldNames(java.lang.Class<?> c)
Gets the array of accessible field names known for a given class.java.lang.ClassLoader
getLoader()
Gets the class loader used by this introspector.private ClassMap
getMap(java.lang.Class<?> c)
Gets the ClassMap for a given class.java.lang.reflect.Method
getMethod(java.lang.Class<?> c, java.lang.String name, java.lang.Object... params)
Gets a method defined by a class, a name and a set of parameters.java.lang.reflect.Method
getMethod(java.lang.Class<?> c, MethodKey key)
Gets the method defined by theMethodKey
for the classc
.java.lang.String[]
getMethodNames(java.lang.Class<?> c)
Gets the array of accessible methods names known for a given class.java.lang.reflect.Method[]
getMethods(java.lang.Class<?> c, java.lang.String methodName)
Gets the array of accessible method known for a given class.private static boolean
isLoadedBy(java.lang.ClassLoader loader, java.lang.Class<?> clazz)
Checks whether a class is loaded through a given class loader or one of its ascendants.void
setLoader(java.lang.ClassLoader classLoader)
Sets the class loader used to solve constructors.
-
-
-
Field Detail
-
CTOR_MISS
private static final java.lang.reflect.Constructor<?> CTOR_MISS
The cache-miss marker for the constructors map.
-
logger
private final org.apache.commons.logging.Log logger
the logger.
-
loader
private java.lang.ClassLoader loader
The class loader used to solve constructors if needed.
-
permissions
private final JexlPermissions permissions
The permissions.
-
lock
private final java.util.concurrent.locks.ReadWriteLock lock
The read/write lock.
-
classMethodMaps
private final java.util.Map<java.lang.Class<?>,ClassMap> classMethodMaps
Holds the method maps for the classes we know about, keyed by Class.
-
constructorsMap
private final java.util.Map<MethodKey,java.lang.reflect.Constructor<?>> constructorsMap
Holds the map of classes ctors we know about as well as unknown ones.
-
constructibleClasses
private final java.util.Map<java.lang.String,java.lang.Class<?>> constructibleClasses
Holds the set of classes we have introspected.
-
-
Constructor Detail
-
Introspector
public Introspector(org.apache.commons.logging.Log log, java.lang.ClassLoader cloader)
Create the introspector.- Parameters:
log
- the logger to usecloader
- the class loader
-
Introspector
public Introspector(org.apache.commons.logging.Log log, java.lang.ClassLoader cloader, JexlPermissions perms)
Create the introspector.- Parameters:
log
- the logger to usecloader
- the class loaderperms
- the permissions
-
-
Method Detail
-
isLoadedBy
private static boolean isLoadedBy(java.lang.ClassLoader loader, java.lang.Class<?> clazz)
Checks whether a class is loaded through a given class loader or one of its ascendants.- Parameters:
loader
- the class loaderclazz
- the class to check- Returns:
- true if clazz was loaded through the loader, false otherwise
-
getClassByName
public java.lang.Class<?> getClassByName(java.lang.String className)
Gets a class by name through this introspector class loader.- Parameters:
className
- the class name- Returns:
- the class instance or null if it could not be found
-
getConstructor
public java.lang.reflect.Constructor<?> getConstructor(java.lang.Class<?> c, MethodKey key)
Gets the constructor defined by theMethodKey
.- Parameters:
c
- the class we want to instantiatekey
- Key of the constructor being searched for- Returns:
- The desired constructor object or null if no unambiguous constructor could be found through introspection.
-
getConstructor
public java.lang.reflect.Constructor<?> getConstructor(MethodKey key)
Gets the constructor defined by theMethodKey
.- Parameters:
key
- Key of the constructor being searched for- Returns:
- The desired constructor object or null if no unambiguous constructor could be found through introspection.
-
getField
public java.lang.reflect.Field getField(java.lang.Class<?> c, java.lang.String key)
Gets the field named bykey
for the classc
.- Parameters:
c
- Class in which the field search is taking placekey
- Name of the field being searched for- Returns:
- the desired field or null if it does not exist or is not accessible
-
getFieldNames
public java.lang.String[] getFieldNames(java.lang.Class<?> c)
Gets the array of accessible field names known for a given class.- Parameters:
c
- the class- Returns:
- the class field names
-
getLoader
public java.lang.ClassLoader getLoader()
Gets the class loader used by this introspector.- Returns:
- the class loader
-
getMap
private ClassMap getMap(java.lang.Class<?> c)
Gets the ClassMap for a given class.- Parameters:
c
- the class- Returns:
- the class map
-
getMethod
public java.lang.reflect.Method getMethod(java.lang.Class<?> c, MethodKey key)
Gets the method defined by theMethodKey
for the classc
.- Parameters:
c
- Class in which the method search is taking placekey
- Key of the method being searched for- Returns:
- The desired method object
- Throws:
MethodKey.AmbiguousException
- if no unambiguous method could be found through introspection
-
getMethod
public java.lang.reflect.Method getMethod(java.lang.Class<?> c, java.lang.String name, java.lang.Object... params)
Gets a method defined by a class, a name and a set of parameters.- Parameters:
c
- the classname
- the method nameparams
- the method parameters- Returns:
- the desired method object
- Throws:
MethodKey.AmbiguousException
- if no unambiguous method could be found through introspection
-
getMethodNames
public java.lang.String[] getMethodNames(java.lang.Class<?> c)
Gets the array of accessible methods names known for a given class.- Parameters:
c
- the class- Returns:
- the class method names
-
getMethods
public java.lang.reflect.Method[] getMethods(java.lang.Class<?> c, java.lang.String methodName)
Gets the array of accessible method known for a given class.- Parameters:
c
- the classmethodName
- the method name- Returns:
- the array of methods (null or not empty)
-
setLoader
public void setLoader(java.lang.ClassLoader classLoader)
Sets the class loader used to solve constructors.Also cleans the constructors and methods caches.
- Parameters:
classLoader
- the class loader; if null, use this instance class loader
-
-