Package com.hotels.transformer.utils
Class ReflectionUtils
- java.lang.Object
-
- com.hotels.transformer.utils.ReflectionUtils
-
public final class ReflectionUtils extends Object
Reflection class utils.
-
-
Constructor Summary
Constructors Constructor Description ReflectionUtils()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Class<?>
getArgumentTypeClass(Object argument, String declaringClass, String fieldName, boolean getNestedGenericClass)
Gets the class of a given object.Class<?>
getArrayType(Field arrayField)
Gets the type of an array.Field
getDeclaredField(String fieldName, Class<?> targetClass)
Return the field of the given class.Class<?>
getDeclaredFieldType(String fieldName, Class<?> clazz)
Return the class of the given field.<A extends Annotation>
AgetFieldAnnotation(Field field, Class<A> annotationClazz)
Returns (if existing) the field's given type annotation.Object
getFieldValue(Object target, Field field)
Gets the value of a field.Object
getFieldValue(Object target, String fieldName, Class<?> fieldType)
Gets the value of a field through getter method.Class<?>
getGenericFieldType(Field field)
Gets the generic infer type of an object.MapType
getMapGenericType(Type fieldType, String declaringClass, String fieldName)
Gets the generic infer type of a map object.<A extends Annotation>
AgetParameterAnnotation(Parameter parameter, Class<A> annotationClazz, String declaringClassName)
Returns (if existing) the constructor parameter's given type annotation.Method
getSetterMethodForField(Class<?> fieldClass, String fieldName, Class<?> fieldType)
Gets the setter method for a specific field.protected Object
invokeMethod(Method method, Object target, Object... args)
Invokes the method.boolean
isGetter(Method method)
Checks if the given method is a getter.boolean
isSetter(Method method)
Checks if the given method is a setter.void
setFieldValue(Object target, Field field, Object fieldValue)
Set the value of a field.
-
-
-
Method Detail
-
invokeMethod
protected Object invokeMethod(Method method, Object target, 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(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
-
isGetter
public boolean isGetter(Method method)
Checks if the given method is a getter.- Parameters:
method
- the method to be checked- Returns:
- true if the method is a getter method, false otherwise
-
getFieldValue
public Object getFieldValue(Object target, Field field)
Gets the value of a field.- Parameters:
target
- the field's classfield
- the fieldField
- Returns:
- the field value
-
getFieldValue
public Object getFieldValue(Object target, String fieldName, 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 Annotation> A getFieldAnnotation(Field field, 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
-
getParameterAnnotation
public <A extends Annotation> A getParameterAnnotation(Parameter parameter, Class<A> annotationClazz, 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 Field getDeclaredField(String fieldName, Class<?> targetClass)
Return the field of the given class.- Parameters:
fieldName
- the name of the field to retrieve.targetClass
- the field's class- Returns:
- the field corresponding to the given name.
-
getDeclaredFieldType
public Class<?> getDeclaredFieldType(String fieldName, Class<?> clazz)
Return the class of the given field.- Parameters:
fieldName
- the name of the filed to retrieve.clazz
- the field's class- Returns:
- the class field of the given field.
-
setFieldValue
public void setFieldValue(Object target, Field field, Object fieldValue)
Set the value of a field.- Parameters:
target
- the field's classfield
- the field to setfieldValue
- the value to set
-
getSetterMethodForField
public Method getSetterMethodForField(Class<?> fieldClass, String fieldName, 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 Class<?> getGenericFieldType(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(Type fieldType, String declaringClass, 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 Class<?> getArgumentTypeClass(Object argument, String declaringClass, 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
-
-