Package com.hotels.beans.utils
Class ReflectionUtils
- java.lang.Object
-
- com.hotels.beans.utils.ReflectionUtils
-
public final class ReflectionUtils extends java.lang.Object
Reflection class utils.
-
-
Constructor Summary
Constructors Constructor Description ReflectionUtils()
Default constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Class<?>
getArgumentTypeClass(java.lang.Object argument, java.lang.String declaringClass, java.lang.String fieldName, boolean getNestedGenericClass)
Gets the class of a given object.java.lang.Class<?>
getArrayType(java.lang.reflect.Field arrayField)
Gets the type of an array.java.lang.reflect.Method
getBuildMethod(java.lang.Class<?> builderClass)
Get build method inside the class.java.lang.reflect.Field
getDeclaredField(java.lang.String fieldName, java.lang.Class<?> targetClass)
Return the field of the given class.<A extends java.lang.annotation.Annotation>
AgetFieldAnnotation(java.lang.reflect.Field field, java.lang.Class<A> annotationClazz)
Returns (if existing) the field's given type annotation.java.lang.Object
getFieldValue(java.lang.Object target, java.lang.reflect.Field field)
Gets the value of a field.java.lang.Object
getFieldValue(java.lang.Object target, java.lang.String fieldName, java.lang.Class<?> fieldType)
Gets the value of a field through getter method.java.lang.Class<?>
getGenericFieldType(java.lang.reflect.Field field)
Gets the generic infer type of an object.MapType
getMapGenericType(java.lang.reflect.Type fieldType, java.lang.String declaringClass, java.lang.String fieldName)
Gets the generic infer type of a map object.<A extends java.lang.annotation.Annotation>
AgetParameterAnnotation(java.lang.reflect.Parameter parameter, java.lang.Class<A> annotationClazz, java.lang.String declaringClassName)
Returns (if existing) the constructor parameter's given type annotation.java.lang.reflect.Method
getSetterMethodForField(java.lang.Class<?> fieldClass, java.lang.String fieldName, java.lang.Class<?> fieldType)
Gets the setter method for a specific field.java.lang.Object
invokeMethod(java.lang.reflect.Method method, java.lang.Object target, java.lang.Object... args)
Invokes the method.boolean
isSetter(java.lang.reflect.Method method)
Checks if the given method is a setter.void
setFieldValue(java.lang.Object target, java.lang.reflect.Field field, java.lang.Object fieldValue)
Sets the value of a field through setter method.
-
-
-
Method Detail
-
invokeMethod
public java.lang.Object invokeMethod(java.lang.reflect.Method method, java.lang.Object target, java.lang.Object... args)
Invokes the method.- Parameters:
method
- the method to be invoked.target
- the class on which invoke the methodargs
- the method parameters- Returns:
- the method result
-
isSetter
public boolean isSetter(java.lang.reflect.Method method)
Checks if the given method is a setter.- Parameters:
method
- the method to be checked- Returns:
- true if the method is a setter method, false otherwise
-
getFieldValue
public java.lang.Object getFieldValue(java.lang.Object target, java.lang.reflect.Field field)
Gets the value of a field.- Parameters:
target
- the field's classfield
- the fieldField
- Returns:
- the field value
-
getFieldValue
public java.lang.Object getFieldValue(java.lang.Object target, java.lang.String fieldName, java.lang.Class<?> fieldType)
Gets the value of a field through getter method.- Parameters:
target
- the field's classfieldName
- the field namefieldType
- the field type- Returns:
- the field value
-
getFieldAnnotation
public <A extends java.lang.annotation.Annotation> A getFieldAnnotation(java.lang.reflect.Field field, java.lang.Class<A> annotationClazz)
Returns (if existing) the field's given type annotation.- Type Parameters:
A
- the annotation type object- Parameters:
field
- the field that should have the annotationannotationClazz
- the annotation type- Returns:
- the annotation
-
getBuildMethod
public java.lang.reflect.Method getBuildMethod(java.lang.Class<?> builderClass)
Get build method inside the class.- Parameters:
builderClass
- Class whit a build method (see Builder Pattern)- Returns:
- Build method if present
-
getParameterAnnotation
public <A extends java.lang.annotation.Annotation> A getParameterAnnotation(java.lang.reflect.Parameter parameter, java.lang.Class<A> annotationClazz, java.lang.String declaringClassName)
Returns (if existing) the constructor parameter's given type annotation.- Type Parameters:
A
- the annotation type object- Parameters:
parameter
- the field that should have the annotationannotationClazz
- the annotation typedeclaringClassName
- the class name that contains the parameter- Returns:
- the annotation
-
getDeclaredField
public java.lang.reflect.Field getDeclaredField(java.lang.String fieldName, java.lang.Class<?> targetClass)
Return the field of the given class.- Parameters:
fieldName
- the name of the filed to retrieve.targetClass
- the field's class- Returns:
- the field corresponding to the given name.
-
setFieldValue
public void setFieldValue(java.lang.Object target, java.lang.reflect.Field field, java.lang.Object fieldValue)
Sets the value of a field through setter method.- Parameters:
target
- the field's classfield
- the field to setfieldValue
- the value to set
-
getSetterMethodForField
public java.lang.reflect.Method getSetterMethodForField(java.lang.Class<?> fieldClass, java.lang.String fieldName, java.lang.Class<?> fieldType)
Gets the setter method for a specific field.- Parameters:
fieldClass
- class containing the fieldfieldName
- the name of the field to be retrievedfieldType
- the field class- Returns:
- the setter method
- Throws:
MissingMethodException
- if the method does not exists
-
getGenericFieldType
public java.lang.Class<?> getGenericFieldType(java.lang.reflect.Field field)
Gets the generic infer type of an object.- Parameters:
field
- the field containing the generic- Returns:
- the generic type class
-
getMapGenericType
public MapType getMapGenericType(java.lang.reflect.Type fieldType, java.lang.String declaringClass, java.lang.String fieldName)
Gets the generic infer type of a map object.- Parameters:
fieldType
- the field containing the genericdeclaringClass
- the class containing the fieldfieldName
- the field name- Returns:
- the generic type class
-
getArgumentTypeClass
public java.lang.Class<?> getArgumentTypeClass(java.lang.Object argument, java.lang.String declaringClass, java.lang.String fieldName, boolean getNestedGenericClass)
Gets the class of a given object.- Parameters:
argument
- the object from which the class has to be retrieveddeclaringClass
- the class containing the field of which the argument belongs tofieldName
- the field name of which the argument belongs togetNestedGenericClass
- if true it retrieves the class of the object generic (if any). i.e.argument = List<String>;
returnsString
, if false returnsList
- Returns:
- the given argument class type
-
getArrayType
public java.lang.Class<?> getArrayType(java.lang.reflect.Field arrayField)
Gets the type of an array.- Parameters:
arrayField
- the array- Returns:
- the array class
-
-