Class Reflector
- java.lang.Object
-
- dk.cloudcreate.essentials.shared.reflection.Reflector
-
public class Reflector extends Object
Caching Java Reflection helper
EncapsulatesConstructors,MethodsandFieldswhile providing high-level method for invoking constructors, method and getting/setting fields
Depending on the version of Java you're using you may need to explicitly add something like the following to your Module Config or Java arguments to ensure thatReflectoris allowed to perform Java reflection
See https://docs.oracle.com/en/java/javase/17/migrate/migrating-jdk-8-later-jdk-releases.html#GUID-12F945EB-71D6-46AF-8C3D-D354FD0B1781 for more information and to understand the implications of adding the--add-opensargument.- See Also:
Interfaces
-
-
Field Summary
Fields Modifier and Type Field Description List<Constructor<?>>constructorsAll the constructors defined intype- seeConstructors.constructors(Class)Set<Field>fieldsAll the fields defined intype- seeFields.fields(Class)Set<Method>methodsAll the methods defined intype- seeMethods.methods(Class)Class<?>typeThe type wrapped by thisReflectorinstance
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Optional<Field>findFieldByAnnotation(Class<? extends Annotation> annotation)Optional<Field>findFieldByName(String fieldName)Optional<Method>findMatchingMethod(String methodName, boolean staticMethod, Class<?>... argumentTypes)Find the first method in thetypewrapped by thisReflectorinstance that matches on: Method Name Being an Instance or a Static method Method parameters that match theargumentTypes
The parameters matching uses theParameters.parameterTypesMatches(Class[], Class[], boolean)without requiring an exact parameter type matchOptional<Field>findStaticFieldByName(String fieldName)<R> Rget(Object object, Field field)Get the value of an instance field<R> Rget(Object object, String fieldName)Get the value of an instance fieldOptional<Constructor<?>>getDefaultConstructor()Get the default no-arguments constructor<R> RgetStatic(Field field)<R> RgetStatic(String fieldName)booleanhasDefaultConstructor()Does the type have a default no-arguments constructor?booleanhasMatchingConstructorBasedOnArguments(Object... constructorArguments)Perform constructor matching based on actual argument values (as opposed tohasMatchingConstructorBasedOnParameterTypes(Class[]))
The constructor match doesn't require exact type matches only compatible type matchesbooleanhasMatchingConstructorBasedOnParameterTypes(Class<?>... parameterTypes)Perform constructor matching based on exact parameter types (as opposed tohasMatchingConstructorBasedOnArguments(Object...))booleanhasMethod(String methodName, boolean staticMethod, Class<?>... argumentTypes)Does thetypewrapped by thisReflectorinstance have a method that matches on: Method Name Being an Instance or a Static method Method parameters that match theargumentTypes
The parameters matching uses theParameters.parameterTypesMatches(Class[], Class[], boolean)without requiring an exact parameter type matchStream<Field>instanceFields()<R> Rinvoke(Method method, Object invokeOnObject, Object... withArguments)Invoke the instance method on theinvokeOnObjectinstance<R> Rinvoke(String methodName, Object invokeOnObject, Object... withArguments)Invoke an instance method on theinvokeOnObjectinstance<R> RinvokeStatic(Method method, Object... arguments)<R> RinvokeStatic(String methodName, Object... arguments)<T> TnewInstance(Object... constructorArguments)Create a new instance of thetype()using a constructor that matches theconstructorArguments
The constructor matching uses theParameters.parameterTypesMatches(Class[], Class[], boolean)without requiring an exact parameter type matchstatic ReflectorreflectOn(Class<?> type)Create aReflectorinstance for thetypestatic ReflectorreflectOn(String fullyQualifiedClassName)Create aReflectorinstance for the class withfullyQualifiedClassNamevoidset(Object object, Field field, Object newFieldValue)Set the instance field to the providednewFieldValuevoidset(Object object, String fieldName, Object newFieldValue)Set the instance field to the providednewFieldValuevoidsetStatic(Field field, Object newFieldValue)voidsetStatic(String fieldName, Object newFieldValue)Stream<Field>staticFields()Class<?>type()The type wrapped by thisReflectorinstance
-
-
-
Field Detail
-
constructors
public final List<Constructor<?>> constructors
All the constructors defined intype- seeConstructors.constructors(Class)
-
methods
public final Set<Method> methods
All the methods defined intype- seeMethods.methods(Class)
-
fields
public final Set<Field> fields
All the fields defined intype- seeFields.fields(Class)
-
-
Method Detail
-
reflectOn
public static Reflector reflectOn(String fullyQualifiedClassName)
Create aReflectorinstance for the class withfullyQualifiedClassName- Parameters:
fullyQualifiedClassName- the fully qualified class name for the type we want to reflect on- Returns:
- the
Reflectorinstance
-
reflectOn
public static Reflector reflectOn(Class<?> type)
Create aReflectorinstance for thetype- Parameters:
type- the type we want to reflect on- Returns:
- the
Reflectorinstance
-
hasDefaultConstructor
public boolean hasDefaultConstructor()
Does the type have a default no-arguments constructor?- Returns:
- true if the type has a default no-arguments constructor, otherwise false
-
getDefaultConstructor
public Optional<Constructor<?>> getDefaultConstructor()
Get the default no-arguments constructor- Returns:
- The default no-args constructor wrapped in an
Optional, otherwise anOptional.empty()if no default no-args constructor exists
-
hasMatchingConstructorBasedOnArguments
public boolean hasMatchingConstructorBasedOnArguments(Object... constructorArguments)
Perform constructor matching based on actual argument values (as opposed tohasMatchingConstructorBasedOnParameterTypes(Class[]))
The constructor match doesn't require exact type matches only compatible type matches- Parameters:
constructorArguments- the actual argument values- Returns:
- true if there's a single matching constructor, otherwise false
-
hasMatchingConstructorBasedOnParameterTypes
public boolean hasMatchingConstructorBasedOnParameterTypes(Class<?>... parameterTypes)
Perform constructor matching based on exact parameter types (as opposed tohasMatchingConstructorBasedOnArguments(Object...))- Parameters:
parameterTypes- the parameter types- Returns:
- true if there's a single matching constructor
-
newInstance
public <T> T newInstance(Object... constructorArguments)
Create a new instance of thetype()using a constructor that matches theconstructorArguments
The constructor matching uses theParameters.parameterTypesMatches(Class[], Class[], boolean)without requiring an exact parameter type match- Type Parameters:
T- the type of the object returned- Parameters:
constructorArguments- the arguments for the constructor- Returns:
- the newly created instance
- Throws:
ReflectionException- in case something goes wrong
-
type
public Class<?> type()
The type wrapped by thisReflectorinstance- Returns:
- The type wrapped by this
Reflectorinstance
-
hasMethod
public boolean hasMethod(String methodName, boolean staticMethod, Class<?>... argumentTypes)
Does thetypewrapped by thisReflectorinstance have a method that matches on:- Method Name
- Being an Instance or a Static method
- Method parameters that match the
argumentTypes
The parameters matching uses theParameters.parameterTypesMatches(Class[], Class[], boolean)without requiring an exact parameter type match
- Parameters:
methodName- the name of the methodstaticMethod- true if the matching method MUST be a static method, otherwise it will only search instance methodsargumentTypes- the argument types that the method must support- Returns:
- true if the
typehas a matching method, otherwise false - See Also:
findMatchingMethod(String, boolean, Class[])
-
findMatchingMethod
public Optional<Method> findMatchingMethod(String methodName, boolean staticMethod, Class<?>... argumentTypes)
Find the first method in thetypewrapped by thisReflectorinstance that matches on:- Method Name
- Being an Instance or a Static method
- Method parameters that match the
argumentTypes
The parameters matching uses theParameters.parameterTypesMatches(Class[], Class[], boolean)without requiring an exact parameter type match
- Parameters:
methodName- the name of the methodstaticMethod- true if the matching method MUST be a static method, otherwise it will only search instance methodsargumentTypes- the argument types that the method must support- Returns:
- an
Optionalwith the matching method orOptional.empty()if no matching could be made - Throws:
TooManyMatchingMethodsFoundException- in case there are more than 1 method that matches on the argument type
-
invokeStatic
public <R> R invokeStatic(String methodName, Object... arguments)
- Type Parameters:
R- the return type- Parameters:
methodName- the name of the method that will be invokedarguments- the arguments that will be passed to the matching static method.
The arguments will be converted toParameters.argumentTypes(Object...)and the search for a matching static method uses theParameters.parameterTypesMatches(Class[], Class[], boolean)without requiring an exact parameter type match- Returns:
- the result of invoking the static method
- Throws:
NoMatchingMethodFoundException- if a matching method cannot be foundMethodInvocationFailedException- if the method invocation fails- See Also:
invokeStatic(Method, Object...)
-
invokeStatic
public <R> R invokeStatic(Method method, Object... arguments)
- Type Parameters:
R- the return type- Parameters:
method- the method that will be invokedarguments- the arguments that will be passed to the static method.- Returns:
- the result of invoking the static method
- Throws:
MethodInvocationFailedException- if the method invocation fails- See Also:
invokeStatic(Method, Object...)
-
invoke
public <R> R invoke(String methodName, Object invokeOnObject, Object... withArguments)
Invoke an instance method on theinvokeOnObjectinstance- Type Parameters:
R- the return type- Parameters:
methodName- the name of the method that will be invokedinvokeOnObject- the object instance that the matching method will be invoked onwithArguments- the arguments that will be passed to the matching instance method.
The arguments will be converted toParameters.argumentTypes(Object...)and the search for a matching method on thetypewrapped by thisReflectorinstance. The parameter matching uses theParameters.parameterTypesMatches(Class[], Class[], boolean)without requiring an exact parameter type match- Returns:
- the result of invoking the method
- Throws:
NoMatchingMethodFoundException- if a matching method cannot be foundMethodInvocationFailedException- if the method invocation fails- See Also:
invoke(Method, Object, Object...)
-
invoke
public <R> R invoke(Method method, Object invokeOnObject, Object... withArguments)
Invoke the instance method on theinvokeOnObjectinstance- Type Parameters:
R- the return type- Parameters:
method- the method that will be invokedinvokeOnObject- the object instance that the matching method will be invoked onwithArguments- the arguments that will be passed to the instance method.<- Returns:
- the result of invoking the method
- Throws:
NoMatchingMethodFoundException- if a matching method cannot be foundMethodInvocationFailedException- if the method invocation fails
-
findFieldByName
public Optional<Field> findFieldByName(String fieldName)
Find the instanceField, in thetypewrapped by thisReflectorinstance, that matches on the field name- Parameters:
fieldName- the name of the field we're searching for- Returns:
- an
Optionalwith the matchingField, otherwise anOptional.empty()if no matching field can be found
-
findStaticFieldByName
public Optional<Field> findStaticFieldByName(String fieldName)
Find the staticField, in thetypewrapped by thisReflectorinstance, that matches on the field name- Parameters:
fieldName- the name of the field we're searching for- Returns:
- an
Optionalwith the matchingField, otherwise anOptional.empty()if no matching field can be found
-
get
public <R> R get(Object object, String fieldName)
Get the value of an instance field- Type Parameters:
R- the return type- Parameters:
object- the object where the field value will get read fromfieldName- the name of the field- Returns:
- the value of the field
- Throws:
NoFieldFoundException- in case we couldn't find a field with a matching name in thetypewrapped by thisReflectorinstanceGetFieldException- in case getting the field value fails
-
get
public <R> R get(Object object, Field field)
Get the value of an instance field- Type Parameters:
R- the return type- Parameters:
object- the object where the field value will get read fromfield- the field to read- Returns:
- the value of the field
- Throws:
NoFieldFoundException- in case we couldn't find a field with a matching nameGetFieldException- in case getting the field value fails
-
getStatic
public <R> R getStatic(String fieldName)
- Type Parameters:
R- the return type- Parameters:
fieldName- the name of the field- Returns:
- the value of the field
- Throws:
NoFieldFoundException- in case we couldn't find a field with a matching name in thetypewrapped by thisReflectorinstanceGetFieldException- in case getting the field value fails
-
getStatic
public <R> R getStatic(Field field)
- Type Parameters:
R- the return type- Parameters:
field- the static field to read- Returns:
- the value of the field
- Throws:
GetFieldException- in case getting the field value fails
-
set
public void set(Object object, String fieldName, Object newFieldValue)
Set the instance field to the providednewFieldValue- Parameters:
object- the object where the field value will get read fromfieldName- the name of the fieldnewFieldValue- the new value of the field- Throws:
NoFieldFoundException- in case we couldn't find a field with a matching name in thetypewrapped by thisReflectorinstanceSetFieldException- in case setting the field value fails
-
set
public void set(Object object, Field field, Object newFieldValue)
Set the instance field to the providednewFieldValue- Parameters:
object- the object where the field value will get read fromfield- the field on which to set a new valuenewFieldValue- the new value of the field- Throws:
NoFieldFoundException- in case we couldn't find a field with a matching name in thetypewrapped by thisReflectorinstanceSetFieldException- in case setting the field value fails
-
setStatic
public void setStatic(String fieldName, Object newFieldValue)
- Parameters:
fieldName- the name of the fieldnewFieldValue- the new value of the field- Throws:
NoFieldFoundException- in case we couldn't find a field with a matching name in thetypewrapped by thisReflectorinstanceSetFieldException- in case setting the field value fails
-
setStatic
public void setStatic(Field field, Object newFieldValue)
- Parameters:
field- the field on which to set a new valuenewFieldValue- the new value of the field- Throws:
SetFieldException- in case setting the field value fails
-
findFieldByAnnotation
public Optional<Field> findFieldByAnnotation(Class<? extends Annotation> annotation)
- Parameters:
annotation- the annotation- Returns:
- an
Optionalwith the matchingField, otherwise anOptional.empty()if no matching field can be found - Throws:
TooManyMatchingFieldsFoundException- in case we found more than one field with the specified annotation
-
-