Class Reflector
- java.lang.Object
-
- org.codehaus.plexus.util.reflection.Reflector
-
public final class Reflector extends java.lang.Object
Utility class used to instantiate an object using reflection. This utility hides many of the gory details needed to do this.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.reflect.Method>>>
classMaps
private static java.lang.String
CONSTRUCTOR_METHOD_NAME
private static java.lang.String
GET_INSTANCE_METHOD_NAME
-
Constructor Summary
Constructors Constructor Description Reflector()
Ensure no instances of Reflector are created...this is a utility.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private java.lang.reflect.Method
_getMethod(java.lang.Class targetClass, java.lang.String methodName, java.lang.Class[] params)
<T> java.lang.reflect.Constructor<T>
getConstructor(java.lang.Class<T> targetClass, java.lang.Class[] params)
Return the constructor, checking the cache first and storing in cache if not already there..private <T> java.util.Map<java.lang.String,java.lang.reflect.Constructor<T>>
getConstructorMap(java.lang.Class<T> theClass)
Retrieve the cache of constructors for the specified class.java.lang.Object
getField(java.lang.Object target, java.lang.String fieldName)
java.lang.Object
getField(java.lang.Object target, java.lang.String fieldName, boolean breakAccessibility)
java.lang.reflect.Method
getMethod(java.lang.Class targetClass, java.lang.String methodName, java.lang.Class[] params)
Return the method, checking the cache first and storing in cache if not already there..private java.util.Map<java.lang.String,?>
getMethodMap(java.lang.Class theClass, java.lang.String methodName)
Retrieve the cache of methods for the specified class and method name.java.lang.Object
getObjectProperty(java.lang.Object target, java.lang.String propertyName)
<T> T
getSingleton(java.lang.Class<T> theClass, java.lang.Object[] initParams)
Retrieve the singleton instance of a class, given the array of parameters...java.lang.Object
getStaticField(java.lang.Class targetClass, java.lang.String fieldName)
java.lang.Object
invoke(java.lang.Object target, java.lang.String methodName, java.lang.Object[] params)
Invoke the specified method on the specified target with the specified params...java.lang.Object
invokeStatic(java.lang.Class targetClass, java.lang.String methodName, java.lang.Object[] params)
Invoke the specified static method with the specified params...<T> T
newInstance(java.lang.Class<T> theClass, java.lang.Object[] params)
Create a new instance of a class, given the array of parameters...
-
-
-
Field Detail
-
CONSTRUCTOR_METHOD_NAME
private static final java.lang.String CONSTRUCTOR_METHOD_NAME
- See Also:
- Constant Field Values
-
GET_INSTANCE_METHOD_NAME
private static final java.lang.String GET_INSTANCE_METHOD_NAME
- See Also:
- Constant Field Values
-
classMaps
private java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.reflect.Method>>> classMaps
-
-
Method Detail
-
newInstance
public <T> T newInstance(java.lang.Class<T> theClass, java.lang.Object[] params) throws ReflectorException
Create a new instance of a class, given the array of parameters... Uses constructor caching to find a constructor that matches the parameter types, either specifically (first choice) or abstractly...- Parameters:
theClass
- The class to instantiateparams
- The parameters to pass to the constructor- Returns:
- The instantiated object
- Throws:
ReflectorException
- In case anything goes wrong here...
-
getSingleton
public <T> T getSingleton(java.lang.Class<T> theClass, java.lang.Object[] initParams) throws ReflectorException
Retrieve the singleton instance of a class, given the array of parameters... Uses constructor caching to find a constructor that matches the parameter types, either specifically (first choice) or abstractly...- Parameters:
theClass
- The class to retrieve the singleton ofinitParams
- The parameters to pass to the constructor- Returns:
- The singleton object
- Throws:
ReflectorException
- In case anything goes wrong here...
-
invoke
public java.lang.Object invoke(java.lang.Object target, java.lang.String methodName, java.lang.Object[] params) throws ReflectorException
Invoke the specified method on the specified target with the specified params...- Parameters:
target
- The target of the invocationmethodName
- The method name to invokeparams
- The parameters to pass to the method invocation- Returns:
- The result of the method call
- Throws:
ReflectorException
- In case of an error looking up or invoking the method.
-
getStaticField
public java.lang.Object getStaticField(java.lang.Class targetClass, java.lang.String fieldName) throws ReflectorException
- Throws:
ReflectorException
-
getField
public java.lang.Object getField(java.lang.Object target, java.lang.String fieldName) throws ReflectorException
- Throws:
ReflectorException
-
getField
public java.lang.Object getField(java.lang.Object target, java.lang.String fieldName, boolean breakAccessibility) throws ReflectorException
- Throws:
ReflectorException
-
invokeStatic
public java.lang.Object invokeStatic(java.lang.Class targetClass, java.lang.String methodName, java.lang.Object[] params) throws ReflectorException
Invoke the specified static method with the specified params...- Parameters:
targetClass
- The target class of the invocationmethodName
- The method name to invokeparams
- The parameters to pass to the method invocation- Returns:
- The result of the method call
- Throws:
ReflectorException
- In case of an error looking up or invoking the method.
-
getConstructor
public <T> java.lang.reflect.Constructor<T> getConstructor(java.lang.Class<T> targetClass, java.lang.Class[] params) throws ReflectorException
Return the constructor, checking the cache first and storing in cache if not already there..- Parameters:
targetClass
- The class to get the constructor fromparams
- The classes of the parameters which the constructor should match.- Returns:
- the Constructor object that matches.
- Throws:
ReflectorException
- In case we can't retrieve the proper constructor.
-
getObjectProperty
public java.lang.Object getObjectProperty(java.lang.Object target, java.lang.String propertyName) throws ReflectorException
- Throws:
ReflectorException
-
getMethod
public java.lang.reflect.Method getMethod(java.lang.Class targetClass, java.lang.String methodName, java.lang.Class[] params) throws ReflectorException
Return the method, checking the cache first and storing in cache if not already there..- Parameters:
targetClass
- The class to get the method fromparams
- The classes of the parameters which the method should match.- Returns:
- the Method object that matches.
- Throws:
ReflectorException
- In case we can't retrieve the proper method.
-
_getMethod
private java.lang.reflect.Method _getMethod(java.lang.Class targetClass, java.lang.String methodName, java.lang.Class[] params) throws ReflectorException
- Throws:
ReflectorException
-
getConstructorMap
private <T> java.util.Map<java.lang.String,java.lang.reflect.Constructor<T>> getConstructorMap(java.lang.Class<T> theClass) throws ReflectorException
Retrieve the cache of constructors for the specified class.- Parameters:
theClass
- the class to lookup.- Returns:
- The cache of constructors.
- Throws:
ReflectorException
- in case of a lookup error.
-
getMethodMap
private java.util.Map<java.lang.String,?> getMethodMap(java.lang.Class theClass, java.lang.String methodName) throws ReflectorException
Retrieve the cache of methods for the specified class and method name.- Parameters:
theClass
- the class to lookup.methodName
- The name of the method to lookup.- Returns:
- The cache of constructors.
- Throws:
ReflectorException
- in case of a lookup error.
-
-