org.codehaus.plexus.util.reflection

Class Reflector

public final class Reflector extends Object

Utility class used to instantiate an object using reflection. This utility hides many of the gory details needed to do this.

Author: John Casey

Field Summary
HashMapclassMaps
static StringCONSTRUCTOR_METHOD_NAME
static StringGET_INSTANCE_METHOD_NAME
Constructor Summary
Reflector()
Ensure no instances of Reflector are created...this is a utility.
Method Summary
ConstructorgetConstructor(Class targetClass, Class[] params)
Return the constructor, checking the cache first and storing in cache if not already there..
MapgetConstructorMap(Class theClass)
Retrieve the cache of constructors for the specified class.
ObjectgetField(Object target, String fieldName)
ObjectgetField(Object target, String fieldName, boolean breakAccessibility)
MethodgetMethod(Class targetClass, String methodName, Class[] params)
Return the method, checking the cache first and storing in cache if not already there..
MapgetMethodMap(Class theClass, String methodName)
Retrieve the cache of methods for the specified class and method name.
ObjectgetObjectProperty(Object target, String propertyName)
ObjectgetSingleton(Class theClass, Object[] initParams)
Retrieve the singleton instance of a class, given the array of parameters...
ObjectgetStaticField(Class targetClass, String fieldName)
Objectinvoke(Object target, String methodName, Object[] params)
Invoke the specified method on the specified target with the specified params...
ObjectinvokeStatic(Class targetClass, String methodName, Object[] params)
Invoke the specified static method with the specified params...
ObjectnewInstance(Class theClass, Object[] params)
Create a new instance of a class, given the array of parameters...
Method_getMethod(Class targetClass, String methodName, Class[] params)

Field Detail

classMaps

private HashMap classMaps

CONSTRUCTOR_METHOD_NAME

private static final String CONSTRUCTOR_METHOD_NAME

GET_INSTANCE_METHOD_NAME

private static final String GET_INSTANCE_METHOD_NAME

Constructor Detail

Reflector

public Reflector()
Ensure no instances of Reflector are created...this is a utility.

Method Detail

getConstructor

public Constructor getConstructor(Class targetClass, Class[] params)
Return the constructor, checking the cache first and storing in cache if not already there..

Parameters: targetClass The class to get the constructor from params 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.

getConstructorMap

private Map getConstructorMap(Class theClass)
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.

getField

public Object getField(Object target, String fieldName)

getField

public Object getField(Object target, String fieldName, boolean breakAccessibility)

getMethod

public Method getMethod(Class targetClass, String methodName, Class[] params)
Return the method, checking the cache first and storing in cache if not already there..

Parameters: targetClass The class to get the method from params 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.

getMethodMap

private Map getMethodMap(Class theClass, String methodName)
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.

getObjectProperty

public Object getObjectProperty(Object target, String propertyName)

getSingleton

public Object getSingleton(Class theClass, Object[] initParams)
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 of initParams The parameters to pass to the constructor

Returns: The singleton object

Throws: ReflectorException In case anything goes wrong here...

getStaticField

public Object getStaticField(Class targetClass, String fieldName)

invoke

public Object invoke(Object target, String methodName, Object[] params)
Invoke the specified method on the specified target with the specified params...

Parameters: target The target of the invocation methodName The method name to invoke params 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.

invokeStatic

public Object invokeStatic(Class targetClass, String methodName, Object[] params)
Invoke the specified static method with the specified params...

Parameters: targetClass The target class of the invocation methodName The method name to invoke params 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.

newInstance

public Object newInstance(Class theClass, Object[] params)
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 instantiate params The parameters to pass to the constructor

Returns: The instantiated object

Throws: ReflectorException In case anything goes wrong here...

_getMethod

private Method _getMethod(Class targetClass, String methodName, Class[] params)