Package org.omnifaces.utils.reflect
Class Reflections
- java.lang.Object
-
- org.omnifaces.utils.reflect.Reflections
-
public final class Reflections extends Object
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T
accessField(Object instance, Field field)
Returns the value of the field of the given instance on the given field name.static <T> T
accessField(Object instance, String fieldName)
Returns the value of the field of the given instance on the given field name.static <T> Optional<Class<T>>
findClass(String className)
Returns the class object associated with the given class name, using the context class loader and if that fails the defining class loader of the current class.static <T> Optional<Constructor<T>>
findConstructor(Class<T> clazz, Class<?>... parameterTypes)
Finds a constructor based on the given parameter types and returnsnull
is none is found.static Optional<Field>
findField(Class<?> clazz, String fieldName)
Finds a field based on the field name.static Optional<Field>
findField(Object base, String fieldName)
Finds a field based on the field name.static Optional<Method>
findMethod(Object base, String methodName, Object... params)
Finds a method based on the method name, amount of parameters and limited typing and returnsnull
is none is found.static <T> List<Class<?>>
getActualTypeArguments(Class<? extends T> subclass, Class<T> superclass)
Returns the actual type arguments of the given subclass against which are declared on the given superclass.static <T> T
instantiate(Class<T> clazz)
Returns a new instance of the given class object using the default constructor.static <T> T
instantiate(String className)
Returns a new instance of the given class name using the default constructor.static <T> T
invokeGetter(Object instance, String propertyName)
Invoke getter method of the given instance on the given property name and return the result.static <T> T
invokeMethod(Object instance, Method method, Object... parameters)
Invoke given method of the given instance with the given parameters and return the result.static <T> T
invokeMethod(Object instance, String methodName, Object... parameters)
Invoke a method of the given instance on the given method name with the given parameters and return the result.static void
invokeSetter(Object instance, String propertyName, Object propertyValue)
Invoke setter method of the given instance on the given property name with the given property value and return the result.static List<Class<? extends Enum<?>>>
listAnnotatedEnumFields(Class<?> clazz, Class<? extends Annotation>... annotations)
Finds all enum fields having all of given annotations.static List<Field>
listAnnotatedFields(Class<?> clazz, Class<? extends Annotation>... annotations)
Finds all fields having all of given annotations.static <T> void
map(Member member, T from, T to)
Map given member from given object to given object.static <T> T
modifyField(Object instance, Field field, T value)
Modifies the value of the given field of the given instance with the given value.static <T> T
modifyField(Object instance, String fieldName, T value)
Modifies the value of the field of the given instance on the given field name with the given value.static <T> Class<T>
toClass(String className)
Returns the class object associated with the given class name, using the context class loader and if that fails the defining class loader of the current class.
-
-
-
Method Detail
-
findField
public static Optional<Field> findField(Object base, String fieldName)
Finds a field based on the field name.- Parameters:
base
- the object in which the field is to be foundfieldName
- The name the field to be found.- Returns:
- The found field, if any.
-
findField
public static Optional<Field> findField(Class<?> clazz, String fieldName)
Finds a field based on the field name.- Parameters:
clazz
- The class object for which the field is to be found.fieldName
- The name the field to be found.- Returns:
- The found field, if any.
-
listAnnotatedFields
@SafeVarargs public static List<Field> listAnnotatedFields(Class<?> clazz, Class<? extends Annotation>... annotations)
Finds all fields having all of given annotations.- Parameters:
clazz
- The class object for which the annotated fields is to be found.annotations
- The annotations of the field.- Returns:
- All fields having all of given annotations.
- Throws:
IllegalArgumentException
- When no annotations are specified.
-
listAnnotatedEnumFields
@SafeVarargs public static List<Class<? extends Enum<?>>> listAnnotatedEnumFields(Class<?> clazz, Class<? extends Annotation>... annotations)
Finds all enum fields having all of given annotations.- Parameters:
clazz
- The class object for which the annotated enum fields is to be found.annotations
- The annotations of the field.- Returns:
- All enum fields having all of given annotations.
- Throws:
IllegalArgumentException
- When no annotations are specified.
-
findMethod
public static Optional<Method> findMethod(Object base, String methodName, Object... params)
Finds a method based on the method name, amount of parameters and limited typing and returnsnull
is none is found.Note that this supports overloading, but a limited one. Given an actual parameter of type Long, this will select a method accepting Number when the choice is between Number and a non-compatible type like String. However, it will NOT select the best match if the choice is between Number and Long.
- Parameters:
base
- the object in which the method is to be foundmethodName
- name of the method to be foundparams
- the method parameters- Returns:
- The found method, if any.
-
toClass
public static <T> Class<T> toClass(String className)
Returns the class object associated with the given class name, using the context class loader and if that fails the defining class loader of the current class.- Type Parameters:
T
- The expected class type.- Parameters:
className
- Fully qualified class name of the class for which a class object needs to be created.- Returns:
- The class object associated with the given class name.
- Throws:
IllegalStateException
- If the class cannot be loaded.ClassCastException
- WhenT
is of wrong type.
-
findClass
public static <T> Optional<Class<T>> findClass(String className)
Returns the class object associated with the given class name, using the context class loader and if that fails the defining class loader of the current class. If the class cannot be loaded, then return null instead of throwing illegal state exception.- Type Parameters:
T
- The expected class type.- Parameters:
className
- Fully qualified class name of the class for which a class object needs to be created.- Returns:
- The found class object associated with the given class name, if any.
- Throws:
ClassCastException
- WhenT
is of wrong type.
-
findConstructor
public static <T> Optional<Constructor<T>> findConstructor(Class<T> clazz, Class<?>... parameterTypes)
Finds a constructor based on the given parameter types and returnsnull
is none is found.- Type Parameters:
T
- The generic object type.- Parameters:
clazz
- The class object for which the constructor is to be found.parameterTypes
- The desired method parameter types.- Returns:
- The found constructor, if any.
-
instantiate
public static <T> T instantiate(String className)
Returns a new instance of the given class name using the default constructor.- Type Parameters:
T
- The expected return type.- Parameters:
className
- Fully qualified class name of the class for which an instance needs to be created.- Returns:
- A new instance of the given class name using the default constructor.
- Throws:
IllegalStateException
- If the class cannot be loaded.ClassCastException
- WhenT
is of wrong type.
-
instantiate
public static <T> T instantiate(Class<T> clazz)
Returns a new instance of the given class object using the default constructor.- Type Parameters:
T
- The generic object type.- Parameters:
clazz
- The class object for which an instance needs to be created.- Returns:
- A new instance of the given class object using the default constructor.
- Throws:
IllegalStateException
- If the class cannot be found, or cannot be instantiated, or when a security manager prevents this operation.
-
accessField
public static <T> T accessField(Object instance, String fieldName)
Returns the value of the field of the given instance on the given field name.- Type Parameters:
T
- The expected return type.- Parameters:
instance
- The instance to access the given field on.fieldName
- The name of the field to be accessed on the given instance.- Returns:
- The value of the field of the given instance on the given field name.
- Throws:
ClassCastException
- WhenT
is of wrong type.IllegalStateException
- If the field cannot be accessed.
-
accessField
public static <T> T accessField(Object instance, Field field)
Returns the value of the field of the given instance on the given field name.- Type Parameters:
T
- The expected return type.- Parameters:
instance
- The instance to access the given field on.field
- The field to be accessed on the given instance.- Returns:
- The value of the field of the given instance on the given field name.
- Throws:
ClassCastException
- WhenT
is of wrong type.IllegalStateException
- If the field cannot be accessed.
-
modifyField
public static <T> T modifyField(Object instance, String fieldName, T value)
Modifies the value of the field of the given instance on the given field name with the given value.- Type Parameters:
T
- The field type.- Parameters:
instance
- The instance to access the given field on.fieldName
- The name of the field to be accessed on the given instance.value
- The new value of the field of the given instance on the given field name.- Returns:
- The old value of the field of the given instance on the given field name.
- Throws:
ClassCastException
- WhenT
is of wrong type.IllegalStateException
- If the field cannot be modified.
-
modifyField
public static <T> T modifyField(Object instance, Field field, T value)
Modifies the value of the given field of the given instance with the given value.- Type Parameters:
T
- The field type.- Parameters:
instance
- The instance to access the given field on.field
- The field to be accessed on the given instance.value
- The new value of the given field of the given instance.- Returns:
- The old value of the given field of the given instance.
- Throws:
ClassCastException
- WhenT
is of wrong type.IllegalStateException
- If the field cannot be modified.
-
invokeMethod
public static <T> T invokeMethod(Object instance, String methodName, Object... parameters)
Invoke a method of the given instance on the given method name with the given parameters and return the result.Note: the current implementation assumes for simplicity that no one of the given parameters is null. If one of them is still null, a NullPointerException will be thrown.
- Type Parameters:
T
- The expected return type.- Parameters:
instance
- The instance to invoke the given method on.methodName
- The name of the method to be invoked on the given instance.parameters
- The method parameters, if any.- Returns:
- The result of the method invocation, if any.
- Throws:
IllegalStateException
- If the method cannot be invoked.ClassCastException
- WhenT
is of wrong type.
-
invokeMethod
public static <T> T invokeMethod(Object instance, Method method, Object... parameters)
Invoke given method of the given instance with the given parameters and return the result.- Type Parameters:
T
- The expected return type.- Parameters:
instance
- The instance to invoke the given method on.method
- The method to be invoked on the given instance.parameters
- The method parameters, if any.- Returns:
- The result of the method invocation, if any.
- Throws:
IllegalStateException
- If the method cannot be invoked.ClassCastException
- WhenT
is of wrong type.
-
invokeGetter
public static <T> T invokeGetter(Object instance, String propertyName)
Invoke getter method of the given instance on the given property name and return the result. If the property name is dot-separated, then it will be invoked recursively.- Type Parameters:
T
- The expected return type.- Parameters:
instance
- The instance to invoke the given getter method on.propertyName
- The property name of the getter method to be invoked on the given instance.- Returns:
- The result of the method invocation, if any.
- Throws:
IllegalStateException
- If the getter method cannot be invoked.ClassCastException
- WhenT
is of wrong type.
-
invokeSetter
public static void invokeSetter(Object instance, String propertyName, Object propertyValue)
Invoke setter method of the given instance on the given property name with the given property value and return the result. If the property name is dot-separated, then it will be invoked recursively.- Parameters:
instance
- The instance to invoke the given setter method on.propertyName
- The property name of the setter method to be invoked on the given instance.propertyValue
- The property value to be set.- Throws:
IllegalStateException
- If the setter method cannot be invoked.
-
map
public static <T> void map(Member member, T from, T to)
Map given member from given object to given object.- Type Parameters:
T
- The from/target object type.- Parameters:
member
- Member to be mapped.from
- Source object.to
- Target object.
-
getActualTypeArguments
public static <T> List<Class<?>> getActualTypeArguments(Class<? extends T> subclass, Class<T> superclass)
Returns the actual type arguments of the given subclass against which are declared on the given superclass. The returned list is ordered.- Type Parameters:
T
- The generic superclass type.- Parameters:
subclass
- The subclass to get the actual type arguments from.superclass
- The superclass where the generic type arguments are declared.- Returns:
- The actual type arguments of the given subclass against which are declared on the given superclass.
-
-