Package com.vladmihalcea.hibernate.util
Class ReflectionUtils
- java.lang.Object
-
- com.vladmihalcea.hibernate.util.ReflectionUtils
-
public final class ReflectionUtils extends java.lang.Object
ReflectionUtils
- Reflection utilities holder.- Since:
- 1.0
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String
GETTER_PREFIX
private static java.lang.String
SETTER_PREFIX
-
Constructor Summary
Constructors Modifier Constructor Description private
ReflectionUtils()
Prevent any instantiation.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> java.lang.Class<T>
getClass(java.lang.String className)
Get the JavaClass
with the given fully-qualified name.static <T> java.lang.Class<T>
getClassOrNull(java.lang.String className)
Get the JavaClass
with the given fully-qualified name or ornull
if noClass
was found matching the provided name.static java.lang.reflect.Method
getDeclaredMethodOrNull(java.lang.Class targetClass, java.lang.String methodName, java.lang.Class... parameterTypes)
Get theMethod
with the given signature (name and parameter types) belonging to the provided JavaClass
, excluding inherited ones, ornull
if noMethod
was found.static java.lang.reflect.Field
getField(java.lang.Class targetClass, java.lang.String fieldName)
Get theField
with the given name belonging to the provided JavaClass
.static java.lang.reflect.Field
getFieldOrNull(java.lang.Class targetClass, java.lang.String fieldName)
Get theField
with the given name belonging to the provided JavaClass
ornull
if noField
was found.static <T> T
getFieldValue(java.lang.Object target, java.lang.String fieldName)
Get the value of the field matching the given name and belonging to targetObject
.static <T> T
getFieldValueOrNull(java.lang.Object target, java.lang.String fieldName)
Get the value of the field matching the given name and belonging to targetObject
ornull
if noField
was found..static <T> java.lang.Class<T>
getFirstSuperClassFromPackage(java.lang.Class clazz, java.lang.String packageName)
Get the first super class matching the provided package name.static java.util.Set<java.lang.Class>
getGenericTypes(java.lang.reflect.ParameterizedType parameterizedType)
Get the generic types of a given Class.static java.lang.reflect.Method
getGetter(java.lang.Object target, java.lang.String propertyName)
Get the property getterMethod
with the given name belonging to the provided JavaObject
.static java.lang.reflect.Type
getMemberGenericTypeOrNull(java.lang.Class targetClass, java.lang.String memberName)
Get the genericType
of theMember
with the given name belonging to the provided JavaClass
ornull
if noMember
was found.static java.lang.reflect.Member
getMemberOrNull(java.lang.Class targetClass, java.lang.String memberName)
Get theMember
with the given name belonging to the provided JavaClass
ornull
if noMember
was found.static java.lang.reflect.Method
getMethod(java.lang.Class targetClass, java.lang.String methodName, java.lang.Class... parameterTypes)
Get theMethod
with the given signature (name and parameter types) belonging to the provided JavaClass
.static java.lang.reflect.Method
getMethod(java.lang.Object target, java.lang.String methodName, java.lang.Class... parameterTypes)
Get theMethod
with the given signature (name and parameter types) belonging to the provided JavaObject
.static java.lang.reflect.Method
getMethodOrNull(java.lang.Class targetClass, java.lang.String methodName, java.lang.Class... parameterTypes)
Get theMethod
with the given signature (name and parameter types) belonging to the provided JavaObject
ornull
if noMethod
was found.static java.lang.reflect.Method
getMethodOrNull(java.lang.Object target, java.lang.String methodName, java.lang.Class... parameterTypes)
Get theMethod
with the given signature (name and parameter types) belonging to the provided JavaObject
ornull
if noMethod
was found.static java.lang.reflect.Method
getSetter(java.lang.Object target, java.lang.String propertyName, java.lang.Class<?> parameterType)
Get the property setterMethod
with the given signature (name and parameter types) belonging to the provided JavaObject
.static java.lang.Class<?>
getWrapperClass(java.lang.Class<?> clazz)
Get the Java WrapperClass
associated to the given primitive type.private static java.lang.IllegalArgumentException
handleException(java.lang.ClassNotFoundException e)
Handle theClassNotFoundException
by rethrowing it as anIllegalArgumentException
.private static java.lang.IllegalArgumentException
handleException(java.lang.IllegalAccessException e)
Handle theIllegalAccessException
by rethrowing it as anIllegalArgumentException
.private static java.lang.IllegalArgumentException
handleException(java.lang.InstantiationException e)
Handle theInstantiationException
by rethrowing it as anIllegalArgumentException
.private static java.lang.IllegalArgumentException
handleException(java.lang.NoSuchFieldException e)
Handle theNoSuchFieldException
by rethrowing it as anIllegalArgumentException
.private static java.lang.IllegalArgumentException
handleException(java.lang.NoSuchMethodException e)
Handle theNoSuchMethodException
by rethrowing it as anIllegalArgumentException
.private static java.lang.IllegalArgumentException
handleException(java.lang.reflect.InvocationTargetException e)
Handle theInvocationTargetException
by rethrowing it as anIllegalArgumentException
.static boolean
hasMethod(java.lang.Class<?> targetClass, java.lang.String methodName, java.lang.Class... parameterTypes)
Check if the provided JavaClass
contains a method matching the given signature (name and parameter types).static <T> T
invokeGetter(java.lang.Object target, java.lang.String propertyName)
Invoke the property getter with the provided name on the given JavaObject
.static <T> T
invokeMethod(java.lang.Object target, java.lang.reflect.Method method, java.lang.Object... parameters)
Invoke the providedMethod
on the given JavaObject
.static <T> T
invokeMethod(java.lang.Object target, java.lang.String methodName, java.lang.Object... parameters)
Invoke the method with the provided signature (name and parameter types) on the given JavaObject
.static void
invokeSetter(java.lang.Object target, java.lang.String propertyName, boolean parameter)
Invoke theboolean
property setter with the provided name on the given JavaObject
.static void
invokeSetter(java.lang.Object target, java.lang.String propertyName, int parameter)
Invoke theint
property setter with the provided name on the given JavaObject
.static void
invokeSetter(java.lang.Object target, java.lang.String propertyName, java.lang.Object parameter)
Invoke the property setter with the provided signature (name and parameter types) on the given JavaObject
.static <T> T
invokeStaticMethod(java.lang.reflect.Method method, java.lang.Object... parameters)
Invoke thestatic
Method
with the provided parameters.static <T> T
newInstance(java.lang.Class clazz)
Instantiate a newObject
of the provided type.static <T> T
newInstance(java.lang.Class clazz, java.lang.Object[] args, java.lang.Class[] argsTypes)
Instantiate a newObject
of the provided type.static <T> T
newInstance(java.lang.String className)
Instantiate a newObject
of the provided type.static void
setFieldValue(java.lang.Object target, java.lang.String fieldName, java.lang.Object value)
Set the value of the field matching the given name and belonging to targetObject
.
-
-
-
Field Detail
-
GETTER_PREFIX
private static final java.lang.String GETTER_PREFIX
- See Also:
- Constant Field Values
-
SETTER_PREFIX
private static final java.lang.String SETTER_PREFIX
- See Also:
- Constant Field Values
-
-
Method Detail
-
newInstance
public static <T> T newInstance(java.lang.String className)
Instantiate a newObject
of the provided type.- Type Parameters:
T
- class type- Parameters:
className
- The fully-qualified Java class name of theObject
to instantiate- Returns:
- new Java
Object
of the provided type
-
newInstance
public static <T> T newInstance(java.lang.Class clazz)
Instantiate a newObject
of the provided type.- Type Parameters:
T
- class type- Parameters:
clazz
- The JavaClass
of theObject
to instantiate- Returns:
- new Java
Object
of the provided type
-
newInstance
public static <T> T newInstance(java.lang.Class clazz, java.lang.Object[] args, java.lang.Class[] argsTypes)
Instantiate a newObject
of the provided type.- Type Parameters:
T
- class type- Parameters:
clazz
- The JavaClass
of theObject
to instantiateargs
- The arguments that need to be passed to the constructorargsTypes
- The argument types that need to be passed to the constructor- Returns:
- new Java
Object
of the provided type
-
getField
public static java.lang.reflect.Field getField(java.lang.Class targetClass, java.lang.String fieldName)
Get theField
with the given name belonging to the provided JavaClass
.- Parameters:
targetClass
- the provided JavaClass
the field belongs tofieldName
- theField
name- Returns:
- the
Field
matching the given name
-
getFieldOrNull
public static java.lang.reflect.Field getFieldOrNull(java.lang.Class targetClass, java.lang.String fieldName)
Get theField
with the given name belonging to the provided JavaClass
ornull
if noField
was found.- Parameters:
targetClass
- the provided JavaClass
the field belongs tofieldName
- theField
name- Returns:
- the
Field
matching the given name ornull
-
getFieldValue
public static <T> T getFieldValue(java.lang.Object target, java.lang.String fieldName)
Get the value of the field matching the given name and belonging to targetObject
.- Type Parameters:
T
- field type- Parameters:
target
- targetObject
whose field we are retrieving the value fromfieldName
- field name- Returns:
- field value
-
getFieldValueOrNull
public static <T> T getFieldValueOrNull(java.lang.Object target, java.lang.String fieldName)
Get the value of the field matching the given name and belonging to targetObject
ornull
if noField
was found..- Type Parameters:
T
- field type- Parameters:
target
- targetObject
whose field we are retrieving the value fromfieldName
- field name- Returns:
- field value matching the given name or
null
-
setFieldValue
public static void setFieldValue(java.lang.Object target, java.lang.String fieldName, java.lang.Object value)
Set the value of the field matching the given name and belonging to targetObject
.- Parameters:
target
- target objectfieldName
- field namevalue
- field value
-
getMethod
public static java.lang.reflect.Method getMethod(java.lang.Object target, java.lang.String methodName, java.lang.Class... parameterTypes)
Get theMethod
with the given signature (name and parameter types) belonging to the provided JavaObject
.- Parameters:
target
- targetObject
methodName
- method nameparameterTypes
- method parameter types- Returns:
- return
Method
matching the provided signature
-
getMethodOrNull
public static java.lang.reflect.Method getMethodOrNull(java.lang.Object target, java.lang.String methodName, java.lang.Class... parameterTypes)
Get theMethod
with the given signature (name and parameter types) belonging to the provided JavaObject
ornull
if noMethod
was found.- Parameters:
target
- targetObject
methodName
- method nameparameterTypes
- method parameter types- Returns:
- return
Method
matching the provided signature ornull
-
getMethod
public static java.lang.reflect.Method getMethod(java.lang.Class targetClass, java.lang.String methodName, java.lang.Class... parameterTypes)
Get theMethod
with the given signature (name and parameter types) belonging to the provided JavaClass
.- Parameters:
targetClass
- targetClass
methodName
- method nameparameterTypes
- method parameter types- Returns:
- the
Method
matching the provided signature
-
getMethodOrNull
public static java.lang.reflect.Method getMethodOrNull(java.lang.Class targetClass, java.lang.String methodName, java.lang.Class... parameterTypes)
Get theMethod
with the given signature (name and parameter types) belonging to the provided JavaObject
ornull
if noMethod
was found.- Parameters:
targetClass
- targetClass
methodName
- method nameparameterTypes
- method parameter types- Returns:
- return
Method
matching the provided signature ornull
-
getDeclaredMethodOrNull
public static java.lang.reflect.Method getDeclaredMethodOrNull(java.lang.Class targetClass, java.lang.String methodName, java.lang.Class... parameterTypes)
Get theMethod
with the given signature (name and parameter types) belonging to the provided JavaClass
, excluding inherited ones, ornull
if noMethod
was found.- Parameters:
targetClass
- targetClass
methodName
- method nameparameterTypes
- method parameter types- Returns:
- return
Method
matching the provided signature ornull
-
hasMethod
public static boolean hasMethod(java.lang.Class<?> targetClass, java.lang.String methodName, java.lang.Class... parameterTypes)
Check if the provided JavaClass
contains a method matching the given signature (name and parameter types).- Parameters:
targetClass
- targetClass
methodName
- method nameparameterTypes
- method parameter types- Returns:
- the provided Java
Class
contains a method with the given signature
-
getSetter
public static java.lang.reflect.Method getSetter(java.lang.Object target, java.lang.String propertyName, java.lang.Class<?> parameterType)
Get the property setterMethod
with the given signature (name and parameter types) belonging to the provided JavaObject
.- Parameters:
target
- targetObject
propertyName
- property nameparameterType
- setter property type- Returns:
- the setter
Method
matching the provided signature
-
getGetter
public static java.lang.reflect.Method getGetter(java.lang.Object target, java.lang.String propertyName)
Get the property getterMethod
with the given name belonging to the provided JavaObject
.- Parameters:
target
- targetObject
propertyName
- property name- Returns:
- the getter
Method
matching the provided name
-
invokeMethod
public static <T> T invokeMethod(java.lang.Object target, java.lang.reflect.Method method, java.lang.Object... parameters)
Invoke the providedMethod
on the given JavaObject
.- Type Parameters:
T
- return value object type- Parameters:
target
- targetObject
whose method we are invokingmethod
- method to invokeparameters
- parameters passed to the method call- Returns:
- the value return by the
Method
invocation
-
invokeMethod
public static <T> T invokeMethod(java.lang.Object target, java.lang.String methodName, java.lang.Object... parameters)
Invoke the method with the provided signature (name and parameter types) on the given JavaObject
.- Type Parameters:
T
- return value object type- Parameters:
target
- targetObject
whose method we are invokingmethodName
- method name to invokeparameters
- parameters passed to the method call- Returns:
- the value return by the method invocation
-
invokeGetter
public static <T> T invokeGetter(java.lang.Object target, java.lang.String propertyName)
Invoke the property getter with the provided name on the given JavaObject
.- Type Parameters:
T
- return value object type- Parameters:
target
- targetObject
whose property getter we are invokingpropertyName
- property name whose getter we are invoking- Returns:
- the value return by the getter invocation
-
invokeSetter
public static void invokeSetter(java.lang.Object target, java.lang.String propertyName, java.lang.Object parameter)
Invoke the property setter with the provided signature (name and parameter types) on the given JavaObject
.- Parameters:
target
- targetObject
whose property setter we are invokingpropertyName
- property name whose setter we are invokingparameter
- parameter passed to the setter call
-
invokeSetter
public static void invokeSetter(java.lang.Object target, java.lang.String propertyName, boolean parameter)
Invoke theboolean
property setter with the provided name on the given JavaObject
.- Parameters:
target
- targetObject
whose property setter we are invokingpropertyName
- property name whose setter we are invokingparameter
-boolean
parameter passed to the setter call
-
invokeSetter
public static void invokeSetter(java.lang.Object target, java.lang.String propertyName, int parameter)
Invoke theint
property setter with the provided name on the given JavaObject
.- Parameters:
target
- targetObject
whose property setter we are invokingpropertyName
- property name whose setter we are invokingparameter
-int
parameter passed to the setter call
-
invokeStaticMethod
public static <T> T invokeStaticMethod(java.lang.reflect.Method method, java.lang.Object... parameters)
Invoke thestatic
Method
with the provided parameters.- Type Parameters:
T
- return value object type- Parameters:
method
- targetstatic
Method
to invokeparameters
- parameters passed to the method call- Returns:
- the value return by the method invocation
-
getClass
public static <T> java.lang.Class<T> getClass(java.lang.String className)
Get the JavaClass
with the given fully-qualified name.- Type Parameters:
T
-Class
type- Parameters:
className
- the JavaClass
name to be retrieved- Returns:
- the Java
Class
object
-
getClassOrNull
public static <T> java.lang.Class<T> getClassOrNull(java.lang.String className)
Get the JavaClass
with the given fully-qualified name or ornull
if noClass
was found matching the provided name.- Type Parameters:
T
-Class
type- Parameters:
className
- the JavaClass
name to be retrieved- Returns:
- the Java
Class
object ornull
-
getWrapperClass
public static java.lang.Class<?> getWrapperClass(java.lang.Class<?> clazz)
Get the Java WrapperClass
associated to the given primitive type.- Parameters:
clazz
- primitive class- Returns:
- the Java Wrapper
Class
-
getFirstSuperClassFromPackage
public static <T> java.lang.Class<T> getFirstSuperClassFromPackage(java.lang.Class clazz, java.lang.String packageName)
Get the first super class matching the provided package name.- Type Parameters:
T
- class generic type- Parameters:
clazz
- Java classpackageName
- package name- Returns:
- the first super class matching the provided package name or
null
.
-
getGenericTypes
public static java.util.Set<java.lang.Class> getGenericTypes(java.lang.reflect.ParameterizedType parameterizedType)
Get the generic types of a given Class.- Parameters:
parameterizedType
- parameterized Type- Returns:
- generic types for the given Class.
-
getMemberOrNull
public static java.lang.reflect.Member getMemberOrNull(java.lang.Class targetClass, java.lang.String memberName)
Get theMember
with the given name belonging to the provided JavaClass
ornull
if noMember
was found.- Parameters:
targetClass
- the provided JavaClass
the field or method belongs tomemberName
- theField
orMethod
name- Returns:
- the
Field
orMethod
matching the given name ornull
-
getMemberGenericTypeOrNull
public static java.lang.reflect.Type getMemberGenericTypeOrNull(java.lang.Class targetClass, java.lang.String memberName)
Get the genericType
of theMember
with the given name belonging to the provided JavaClass
ornull
if noMember
was found.- Parameters:
targetClass
- the provided JavaClass
the field or method belongs tomemberName
- theField
orMethod
name- Returns:
- the generic
Type
of theField
orMethod
matching the given name ornull
-
handleException
private static java.lang.IllegalArgumentException handleException(java.lang.NoSuchFieldException e)
Handle theNoSuchFieldException
by rethrowing it as anIllegalArgumentException
.- Parameters:
e
- the originalNoSuchFieldException
- Returns:
- the
IllegalArgumentException
wrapping exception
-
handleException
private static java.lang.IllegalArgumentException handleException(java.lang.NoSuchMethodException e)
Handle theNoSuchMethodException
by rethrowing it as anIllegalArgumentException
.- Parameters:
e
- the originalNoSuchMethodException
- Returns:
- the
IllegalArgumentException
wrapping exception
-
handleException
private static java.lang.IllegalArgumentException handleException(java.lang.IllegalAccessException e)
Handle theIllegalAccessException
by rethrowing it as anIllegalArgumentException
.- Parameters:
e
- the originalIllegalAccessException
- Returns:
- the
IllegalArgumentException
wrapping exception
-
handleException
private static java.lang.IllegalArgumentException handleException(java.lang.reflect.InvocationTargetException e)
Handle theInvocationTargetException
by rethrowing it as anIllegalArgumentException
.- Parameters:
e
- the originalInvocationTargetException
- Returns:
- the
IllegalArgumentException
wrapping exception
-
handleException
private static java.lang.IllegalArgumentException handleException(java.lang.ClassNotFoundException e)
Handle theClassNotFoundException
by rethrowing it as anIllegalArgumentException
.- Parameters:
e
- the originalClassNotFoundException
- Returns:
- the
IllegalArgumentException
wrapping exception
-
handleException
private static java.lang.IllegalArgumentException handleException(java.lang.InstantiationException e)
Handle theInstantiationException
by rethrowing it as anIllegalArgumentException
.- Parameters:
e
- the originalInstantiationException
- Returns:
- the
IllegalArgumentException
wrapping exception
-
-