public class ReflectionUtils extends Object
| Constructor and Description |
|---|
ReflectionUtils() |
| Modifier and Type | Method and Description |
|---|---|
static void |
copyFields(Object fromObject,
Object toObject) |
static <T> T |
createInstanceOfType(Class<T> type,
boolean bypassAccessibility)
Creates an instance of the given type
|
static <T> T |
createInstanceOfType(Class<T> type,
boolean bypassAccessibility,
Class[] argumentTypes,
Object[] arguments)
Creates an instance of the given type
|
static <T> T |
createInstanceOfType(String className,
boolean bypassAccessibility)
Creates an instance of the class with the given name.
|
static Set<Field> |
getAllFields(Class<?> clazz)
Gets all fields of the given class and all its super-classes.
|
static Set<Method> |
getAllMethods(Class<?> clazz)
Gets all methods of the given class and all its super-classes.
|
static Set<Method> |
getAllMethods(Class<?> clazz,
boolean isStatic)
Gets all methods of the given class and all its super-classes.
|
static Set<Method> |
getAllMethods(Class<?> clazz,
boolean isStatic,
Class<?> returnType)
Gets all methods of the given class and all its super-classes.
|
static <T> Class<T> |
getClassForType(Type type)
Gets the class instance for the given type instance.
|
static <T> Class<T> |
getClassWithName(String className)
Gets the class for the given name.
|
static <T extends Enum<?>> |
getEnumValue(Class<T> enumClass,
String enumValueName)
Gets the enum value that has the given name.
|
static Set<Field> |
getFieldsAssignableFrom(Class<?> clazz,
Type type,
boolean isStatic)
Returns all declared fields of the given class that are assignable from
the given type.
|
static Set<Field> |
getFieldsOfType(Class<?> clazz,
Type type,
boolean isStatic)
Returns the fields in the given class that have the exact given type.
|
static <T> T |
getFieldValue(Object object,
Field field)
Returns the value of the given field (may be private) in the given object
|
static Field |
getFieldWithName(Class<?> clazz,
String fieldName,
boolean isStatic)
From the given class, returns the field with the given name. isStatic
indicates if it should be a static field or not.
|
static Type |
getGenericType(Field field)
Gets the T from a Class
|
static Method |
getGetter(Class<?> clazz,
String propertyName,
boolean isStatic)
From the given class, returns the getter for the given property name.
|
static Method |
getGetter(Method setter,
boolean isStatic)
From the given class, returns the getter for the given setter method.
|
static Method |
getMethod(Class<?> clazz,
String methodName,
boolean isStatic,
Class<?>... parameterTypes)
Gets the method with the given name from the given class or one of its
super-classes.
|
static String |
getPropertyName(Method setterMethod)
Gets the name of the field for the given setter method.
|
static Method |
getSetter(Class<?> clazz,
String propertyName,
boolean isStatic)
From the given class, returns the setter for the property with the given
name and 1 argument.
|
static Set<Method> |
getSettersAssignableFrom(Class<?> clazz,
Type type,
boolean isStatic)
Returns all declared setter methods of fields of the given class that are
assignable from the given type.
|
static Set<Method> |
getSettersOfType(Class<?> clazz,
Type type,
boolean isStatic)
Returns the setter methods in the given class that have an argument with
the exact given type.
|
static String |
getSimpleMethodName(Method method)
Gets the string representation of the method as follows:
'class name'.'
|
static <T> T |
invokeMethod(Object target,
Method method,
Object... arguments)
Invokes the given method with the given parameters on the given target
object
|
static boolean |
isAssignable(Type fromType,
Type toType)
Checks whether the given fromType is assignable to the given toType, also
taking into account possible auto-boxing.
|
static boolean |
isSetter(Method method)
For each method, check if it can be a setter for an object of the given
type.
|
static void |
setFieldAndSetterValue(Object object,
Set<Field> fields,
Set<Method> setterMethods,
Object value)
Sets the given value to the given field and setters on the given object.
|
static void |
setFieldValue(Object object,
Field field,
Object value)
Sets the given value to the given field on the given object
|
static void |
setFieldValue(Object obj,
String nameField,
Object newObject)
Sets the given value to the given field on the given object
|
public static <T> T createInstanceOfType(String className, boolean bypassAccessibility)
className - The name of the class, not nullbypassAccessibility - If true, no exception is thrown if the parameterless
constructor is not publicUnitilsException - if the class could not be found or no instance could be
createdpublic static <T> T createInstanceOfType(Class<T> type, boolean bypassAccessibility)
T - The type of the instancetype - The type of the instancebypassAccessibility - If true, no exception is thrown if the parameterless
constructor is not publicUnitilsException - If an instance could not be createdpublic static <T> T createInstanceOfType(Class<T> type, boolean bypassAccessibility, Class[] argumentTypes, Object[] arguments)
T - The type of the instancetype - The type of the instancebypassAccessibility - If true, no exception is thrown if the parameterless
constructor is not publicargumentTypes - The constructor arg types, not nullarguments - The constructor args, not nullUnitilsException - If an instance could not be createdpublic static <T> T getFieldValue(Object object, Field field)
object - The object containing the field, null for static fieldsfield - The field, not nullUnitilsException - if the field could not be accessedpublic static void setFieldValue(Object object, Field field, Object value)
object - The object containing the field, not nullfield - The field, not nullvalue - The value for the given field in the given objectUnitilsException - if the field could not be accessedpublic static void setFieldValue(Object obj, String nameField, Object newObject) throws SecurityException, NoSuchFieldException
obj - nameField - newObject - SecurityExceptionNoSuchFieldExceptionpublic static void setFieldAndSetterValue(Object object, Set<Field> fields, Set<Method> setterMethods, Object value)
object - The object containing the field and setters, not nullfields - The fields, not nullsetterMethods - The setter methods, not nullvalue - The value for the given field and setters in the given objectpublic static <T> T invokeMethod(Object target, Method method, Object... arguments) throws InvocationTargetException
target - The object containing the method, not nullmethod - The method, not nullarguments - The method argumentsUnitilsException - if the method could not be invokedInvocationTargetException - If the called method throwed an exceptionpublic static Set<Field> getFieldsAssignableFrom(Class<?> clazz, Type type, boolean isStatic)
clazz - The class to get fields from, not nulltype - The type, not nullisStatic - True if static fields are to be returned, false for non-staticpublic static Set<Field> getFieldsOfType(Class<?> clazz, Type type, boolean isStatic)
clazz - The class to get the field from, not nulltype - The type, not nullisStatic - True if static fields are to be returned, false for non-staticpublic static Set<Method> getSettersAssignableFrom(Class<?> clazz, Type type, boolean isStatic)
clazz - The class to get setters from, not nulltype - The type, not nullisStatic - True if static setters are to be returned, false for
non-staticpublic static Set<Method> getSettersOfType(Class<?> clazz, Type type, boolean isStatic)
clazz - The class to get the setter from, not nulltype - The type, not nullisStatic - True if static setters are to be returned, false for
non-staticpublic static Method getSetter(Class<?> clazz, String propertyName, boolean isStatic)
clazz - The class to get the setter from, not nullpropertyName - The name of the property, not nullisStatic - True if a static setter is to be returned, false for
non-staticpublic static Method getGetter(Class<?> clazz, String propertyName, boolean isStatic)
clazz - The class to get the setter from, not nullpropertyName - The name of the property, not nullisStatic - True if a static getter is to be returned, false for
non-staticpublic static Set<Method> getAllMethods(Class<?> clazz, boolean isStatic)
clazz - The classisStatic - public static Set<Method> getAllMethods(Class<?> clazz, boolean isStatic, Class<?> returnType)
clazz - isStatic - returnType - public static Method getGetter(Method setter, boolean isStatic)
setter - The setter method, not nullisStatic - True if a static getter is to be returned, false for
non-staticpublic static Field getFieldWithName(Class<?> clazz, String fieldName, boolean isStatic)
clazz - The class to get the field from, not nullfieldName - The name, not nullisStatic - True if a static field is to be returned, false for non-staticpublic static <T extends Enum<?>> T getEnumValue(Class<T> enumClass, String enumValueName)
enumClass - The enum class, not nullenumValueName - The name of the enum value, not nullUnitilsException - if no value could be found with the given namepublic static boolean isSetter(Method method)
method - The method to check, not nullpublic static String getPropertyName(Method setterMethod)
setterMethod - The method, not nullpublic static <T> Class<T> getClassWithName(String className)
className - The name of the class, not nullpublic static Method getMethod(Class<?> clazz, String methodName, boolean isStatic, Class<?>... parameterTypes)
clazz - The class containing the methodmethodName - The name of the method, not nullisStatic - True for a static method, false for non-staticparameterTypes - The parameter typespublic static Set<Method> getAllMethods(Class<?> clazz)
clazz - The classpublic static Set<Field> getAllFields(Class<?> clazz)
clazz - The classpublic static String getSimpleMethodName(Method method)
method - The method, not nullpublic static boolean isAssignable(Type fromType, Type toType)
fromType - The from type, not nulltoType - The to type, not nullpublic static Type getGenericType(Field field)
field - The field to get the type from, not nullpublic static <T> Class<T> getClassForType(Type type)
type - The type to get a class instance for, not nullCopyright © 2016. All Rights Reserved.