Class Reflector
java.lang.Object
org.codehaus.plexus.util.reflection.Reflector
Utility class used to instantiate an object using reflection. This utility hides many of the gory details needed to
do this.
- Author:
- John Casey
-
Constructor Summary
ConstructorDescriptionEnsure no instances of Reflector are created...this is a utility. -
Method Summary
Modifier and TypeMethodDescription<T> Constructor
<T> getConstructor
(Class<T> targetClass, Class[] params) Return the constructor, checking the cache first and storing in cache if not already there..Return the method, checking the cache first and storing in cache if not already there..getObjectProperty
(Object target, String propertyName) <T> T
getSingleton
(Class<T> theClass, Object[] initParams) Retrieve the singleton instance of a class, given the array of parameters...getStaticField
(Class targetClass, String fieldName) Invoke the specified method on the specified target with the specified params...invokeStatic
(Class targetClass, String methodName, Object[] params) Invoke the specified static method with the specified params...<T> T
newInstance
(Class<T> theClass, Object[] params) Create a new instance of a class, given the array of parameters...
-
Constructor Details
-
Reflector
public Reflector()Ensure no instances of Reflector are created...this is a utility.
-
-
Method Details
-
newInstance
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...- Type Parameters:
T
- the type- 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
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...- Type Parameters:
T
- the type- 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
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
- Throws:
ReflectorException
-
getField
- Throws:
ReflectorException
-
getField
public Object getField(Object target, String fieldName, boolean breakAccessibility) throws ReflectorException - Throws:
ReflectorException
-
invokeStatic
public Object invokeStatic(Class targetClass, String methodName, 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> Constructor<T> getConstructor(Class<T> targetClass, Class[] params) throws ReflectorException Return the constructor, checking the cache first and storing in cache if not already there..- Type Parameters:
T
- the type- 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
- Throws:
ReflectorException
-
getMethod
public Method getMethod(Class targetClass, String methodName, 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 frommethodName
- the method nameparams
- 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.
-