Interface IReflection
-
- All Known Implementing Classes:
Java9ReflectionImpl
,ReflectionImpl
public interface IReflection
This type exposes helpful reflection methods.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description <T> T
callConstructor(Constructor<?> constructor, Object... args)
Call a constructor with the arguments.<T> T
callField(Object target, Field field)
Call a field on a target.<T> T
callMethod(Object target, String methodName, Object... args)
Call a method on the target object.Constructor<?>
findConstructor(Class<?> clazz, Class<?>... args)
Find a constructor on the given class.Method
findMethod(Class<?> clazz, String methodName, Class<?>... argTypes)
Scan the hierarchy of the input class for a method with the given name.Field
getField(Object target, String fieldName)
Get a filed from the target.Iterator<Field>
getFieldIterator(Class<?> clazz)
Get an iterator that will iterator over all the available fields on the given class.<T> T
getFieldValue(Object target, String fieldName)
Get a field from an object.Set<String>
getModifiers(Field field)
Get the modifier names of a field.boolean
setAccessible(Class<?> clazz, Constructor<?> constructor)
Set the constructor as accessible.boolean
setAccessible(Class<?> clazz, Field field)
Set the field as accessible.boolean
setAccessible(Class<?> clazz, Method method)
Set the method as accessible.
-
-
-
Method Detail
-
setAccessible
boolean setAccessible(Class<?> clazz, Field field)
Set the field as accessible.- Parameters:
clazz
- the clazz the field is onfield
- the field to access- Returns:
- could we complete the operation
- See Also:
Field.setAccessible(boolean)
-
setAccessible
boolean setAccessible(Class<?> clazz, Method method)
Set the method as accessible.- Parameters:
clazz
- the clazz the field is onmethod
- the method to access- Returns:
- could we complete the operation
- See Also:
Method.setAccessible(boolean)
-
setAccessible
boolean setAccessible(Class<?> clazz, Constructor<?> constructor)
Set the constructor as accessible.- Parameters:
clazz
- the clazz the field is onconstructor
- the constructor to set- Returns:
- could we complete the operation
- See Also:
Constructor.setAccessible(boolean)
-
callMethod
<T> T callMethod(Object target, String methodName, Object... args)
Call a method on the target object.This will look for the method using
findMethod(Class, String, Class[])
using the input arguments as the argument types of the method. The method will then be invoked on the target argument.- Type Parameters:
T
- the type of the response- Parameters:
target
- the target instance to call the method onmethodName
- the name of the method to callargs
- the arguments to the method- Returns:
- the response of the method
-
findMethod
Method findMethod(Class<?> clazz, String methodName, Class<?>... argTypes)
Scan the hierarchy of the input class for a method with the given name. This will scan declared methods and methods, as well as stepping up the super class.- Parameters:
clazz
- the class to start the scan onmethodName
- the name of the method to look forargTypes
- the argument types in the method signature- Returns:
- the discovered method or
null
-
getField
Field getField(Object target, String fieldName)
Get a filed from the target.- Parameters:
target
- the target instance to look on.fieldName
- the name of the field to get- Returns:
- the field, or
null
-
getFieldValue
<T> T getFieldValue(Object target, String fieldName)
Get a field from an object.- Type Parameters:
T
- the type to return as- Parameters:
target
- the object to look atfieldName
- the field name to look for- Returns:
- the field as T, else
null
-
getFieldIterator
Iterator<Field> getFieldIterator(Class<?> clazz)
Get an iterator that will iterator over all the available fields on the given class.- Parameters:
clazz
- the class to scan- Returns:
- the iterator for the fields
-
callField
<T> T callField(Object target, Field field)
Call a field on a target.- Type Parameters:
T
- the type of the return- Parameters:
target
- the object to get the field fromfield
- the field to call- Returns:
- the value of the field
-
getModifiers
Set<String> getModifiers(Field field)
Get the modifier names of a field.- Parameters:
field
- the field to look at- Returns:
- a set of strings that represent the modifiers.
-
findConstructor
Constructor<?> findConstructor(Class<?> clazz, Class<?>... args)
Find a constructor on the given class.- Parameters:
clazz
- the class to look onargs
- the arguments for the constructor- Returns:
- the constructor or
null
-
callConstructor
<T> T callConstructor(Constructor<?> constructor, Object... args) throws DeepRuntimeException
Call a constructor with the arguments.- Type Parameters:
T
- the type of the return- Parameters:
constructor
- the constructor to callargs
- the arguments for the constructor- Returns:
- the new object, or
null
- Throws:
DeepRuntimeException
- if we could not create a new object
-
-