Package com.hotels.transformer.utils
Class ClassUtils
java.lang.Object
com.hotels.transformer.utils.ClassUtils
public final class ClassUtils extends Object
Reflection utils for Class objects.
-
Field Summary
Fields Modifier and Type Field Description static String
BUILD_METHOD_NAME
Default method name used by a Builder for creating an object. -
Constructor Summary
Constructors Constructor Description ClassUtils()
Default constructor. -
Method Summary
Modifier and Type Method Description boolean
allParameterAnnotatedWith(Constructor constructor, Class<? extends Annotation> annotationClass)
Checks if any of the class constructor's parameters are not annotated with the given class.boolean
areParameterNamesAvailable(Constructor constructor)
Checks if the constructor's parameters names are defined.<K> Constructor<K>
getAllArgsConstructor(Class<K> clazz)
Retrieves the all args constructor.Optional<Class<?>>
getBuilderClass(Class<?> targetClass)
Returns the builder class.Method
getBuildMethod(Class<?> parentClass, Class<?> builderClass)
Get build method inside the Builder class.ClassType
getClassType(Class<?> clazz)
Returns the class type.Class<?>
getConcreteClass(Field field, Object fieldValue)
Returns the concrete class of a field.Parameter[]
getConstructorParameters(Constructor constructor)
Gets all the constructor parameters.Class[]
getDeclaredClasses(Class<?> clazz)
Retrieves all classes defined into the given one.List<Field>
getDeclaredFields(Class<?> clazz, boolean skipStatic)
Return the fields of a class.Object
getDefaultTypeValue(Class<?> objectType)
Gets the default value of a primitive type.<T> Class<?>
getFieldClass(Field field, T objectInstance)
Returns the concrete class of a field.List<Method>
getGetterMethods(Class<?> clazz)
Retrieves all the getters method for the given class.<T> T
getInstance(Constructor constructor, Object... constructorArgs)
Creates an instance of the given class invoking the given constructor.<K> Supplier<K>
getNoArgsConstructor(Class<K> clazz)
Retrieves the no args constructor.List<Field>
getNotFinalFields(Class<?> clazz, Boolean skipStatic)
Returns all the not final fields.List<Field>
getPrivateFields(Class<?> clazz)
Return the private fields of a class.List<Field>
getPrivateFields(Class<?> clazz, boolean skipFinal)
Return the private fields of a class.List<Field>
getPrivateFinalFields(Class<?> clazz)
Return the private final fields of a class.List<Method>
getSetterMethods(Class<?> clazz)
Retrieves all the setters method for the given class.int
getTotalFields(Class<?> clazz, Predicate<? super Field> predicate)
Return the total fields matching with the given predicate.<K> boolean
hasAccessibleConstructors(Class<K> targetClass)
Checks if the destination class has accessible constructor.boolean
hasField(Object target, String fieldName)
Checks that the class has a specific field.boolean
hasFinalFields(Class<?> clazz)
Checks if a class has any final field.boolean
hasSetterMethods(Class<?> clazz)
Checks if a class has setter methods.static boolean
isBigDecimal(Class<?> type)
Checks if the given type is aBigDecimal
.static boolean
isBigInteger(Class<?> type)
Checks if the given type is aBigInteger
.static boolean
isBoolean(Class<?> type)
Checks if the given type is aBoolean
.static boolean
isByte(Class<?> type)
Checks if the given type is aByte
.static boolean
isByteArray(Class<?> type)
Checks if the given type is a byte[].static boolean
isChar(Class<?> type)
Checks if the given type is aCharacter
.static boolean
isDouble(Class<?> type)
Checks if the given type is aDouble
.static boolean
isFloat(Class<?> type)
Checks if the given type is aFloat
.static boolean
isInt(Class<?> type)
Checks if the given type is anInteger
.static boolean
isLong(Class<?> type)
Checks if the given type is aLong
.boolean
isPrimitiveOrSpecialType(Class<?> clazz)
Checks if an object is a primitive or special type.boolean
isPrimitiveType(Class<?> clazz)
Checks if an object is a special type.boolean
isPrimitiveTypeArray(Class<?> clazz)
Checks if an object is a primitive type array.static boolean
isShort(Class<?> type)
Checks if the given type is aShort
.boolean
isSpecialType(Class<?> clazz)
Checks if an object is a special type.static boolean
isString(Class<?> type)
Checks if the given type is a String.
-
Field Details
-
BUILD_METHOD_NAME
Default method name used by a Builder for creating an object.- See Also:
- Constant Field Values
-
-
Constructor Details
-
ClassUtils
public ClassUtils()Default constructor.
-
-
Method Details
-
isPrimitiveOrSpecialType
Checks if an object is a primitive or special type.- Parameters:
clazz
- the class to check- Returns:
- true if is primitive or special type, false otherwise
-
isPrimitiveType
Checks if an object is a special type.- Parameters:
clazz
- the class to check- Returns:
- true if is special type, false otherwise
-
isPrimitiveTypeArray
Checks if an object is a primitive type array.- Parameters:
clazz
- the class to check- Returns:
- true if is primitive type array, false otherwise
-
isSpecialType
Checks if an object is a special type. The label "Special classes" refers to all objects that has to be copied without applying any special transformation.- Parameters:
clazz
- the class to check- Returns:
- true if is special type, false otherwise
-
isDouble
Checks if the given type is aDouble
.- Parameters:
type
- the class to check- Returns:
- true if is Double
-
isFloat
Checks if the given type is aFloat
.- Parameters:
type
- the class to check- Returns:
- true if is Float
-
isLong
Checks if the given type is aLong
.- Parameters:
type
- the class to check- Returns:
- true if is Long
-
isShort
Checks if the given type is aShort
.- Parameters:
type
- the class to check- Returns:
- true if is Short
-
isInt
Checks if the given type is anInteger
.- Parameters:
type
- the class to check- Returns:
- true if is Integer
-
isByte
Checks if the given type is aByte
.- Parameters:
type
- the class to check- Returns:
- true if is Byte
-
isChar
Checks if the given type is aCharacter
.- Parameters:
type
- the class to check- Returns:
- true if is Character
-
isBoolean
Checks if the given type is aBoolean
.- Parameters:
type
- the class to check- Returns:
- true if is Boolean
-
isString
Checks if the given type is a String.- Parameters:
type
- the class to check- Returns:
- true if is String
-
isBigInteger
Checks if the given type is aBigInteger
.- Parameters:
type
- the class to check- Returns:
- true if is String
-
isBigDecimal
Checks if the given type is aBigDecimal
.- Parameters:
type
- the class to check- Returns:
- true if is String
-
isByteArray
Checks if the given type is a byte[].- Parameters:
type
- the class to check- Returns:
- true if is String
-
getPrivateFinalFields
Return the private final fields of a class.- Parameters:
clazz
- class from which gets the field- Returns:
- a list of private final fields.
-
getTotalFields
Return the total fields matching with the given predicate.- Parameters:
clazz
- class from which gets the fieldpredicate
- the condition that needs to match- Returns:
- the total matching item.
-
getPrivateFields
Return the private fields of a class.- Parameters:
clazz
- class from which gets the field- Returns:
- a list of private final fields.
-
getPrivateFields
Return the private fields of a class.- Parameters:
clazz
- class from which gets the fieldskipFinal
- if true it skips the final fields otherwise all private fields are retrieved.- Returns:
- a list of private fields.
-
getDeclaredFields
Return the fields of a class.- Parameters:
clazz
- class from which gets the fieldskipStatic
- if true it skips the static fields otherwise all private fields are retrieved.- Returns:
- a list of class fields.
-
getFieldClass
Returns the concrete class of a field.- Type Parameters:
T
- the object instance class.- Parameters:
field
- the field for which the concrete class has to be retrieved.objectInstance
- the object instance.- Returns:
- the concrete class of a field
-
getConcreteClass
Returns the concrete class of a field.- Parameters:
field
- the field for which the concrete class has to be retrieved.fieldValue
- the field value.- Returns:
- the concrete class of a field
-
hasAccessibleConstructors
Checks if the destination class has accessible constructor.- Type Parameters:
K
- the target object type- Parameters:
targetClass
- the destination object class- Returns:
- true if the target class uses the builder pattern
-
getDeclaredClasses
Retrieves all classes defined into the given one.- Parameters:
clazz
- class where we search for a nested class- Returns:
- all classes defined into the given one
-
getBuilderClass
Returns the builder class.- Parameters:
targetClass
- the class where the builder should be searched- Returns:
- the Builder class if available.
-
getBuildMethod
Get build method inside the Builder class.- Parameters:
parentClass
- the class containing the builderbuilderClass
- the builder class (see Builder Pattern)- Returns:
- Builder build method if present
-
getInstance
Creates an instance of the given class invoking the given constructor.- Type Parameters:
T
- the class object type.- Parameters:
constructor
- the constructor to invoke.constructorArgs
- the constructor args.- Returns:
- the object instance.
- Throws:
InstanceCreationException
- in case the object creation fails.
-
getNoArgsConstructor
Retrieves the no args constructor.- Type Parameters:
K
- the object type- Parameters:
clazz
- the class from which gets the all arg constructor.- Returns:
- the no args constructor
- Throws:
InvalidBeanException
- if no default constructor is available
-
getAllArgsConstructor
Retrieves the all args constructor.- Type Parameters:
K
- the object type- Parameters:
clazz
- the class from which gets the all arg constructor.- Returns:
- the all args constructor
-
getConstructorParameters
Gets all the constructor parameters.- Parameters:
constructor
- the constructor.- Returns:
- the constructor parameters
-
hasField
Checks that the class has a specific field.- Parameters:
target
- the class where the field should befieldName
- the field name to retrieve- Returns:
- true if the field is available, false otherwise
-
hasSetterMethods
Checks if a class has setter methods.- Parameters:
clazz
- clazz the clazz containing the methods.- Returns:
- true if has at least one setter method, false otherwise
-
hasFinalFields
Checks if a class has any final field.- Parameters:
clazz
- class from which gets the field- Returns:
- true if it has private final field, false otherwise.
-
allParameterAnnotatedWith
public boolean allParameterAnnotatedWith(Constructor constructor, Class<? extends Annotation> annotationClass)Checks if any of the class constructor's parameters are not annotated with the given class.- Parameters:
constructor
- the constructor to check.annotationClass
- the annotation class to retrieve- Returns:
- true if any of the parameter does not contains the annotation, false otherwise.
-
areParameterNamesAvailable
Checks if the constructor's parameters names are defined.- Parameters:
constructor
- the constructor to check.- Returns:
- true if some parameters names are not defined, false otherwise.
-
getClassType
Returns the class type.- Parameters:
clazz
- the class to check- Returns:
- the class type
ClassType
-
getSetterMethods
Retrieves all the setters method for the given class.- Parameters:
clazz
- the class containing the methods.- Returns:
- all the class setter methods
-
getGetterMethods
Retrieves all the getters method for the given class.- Parameters:
clazz
- the class containing the methods.- Returns:
- all the class getter methods
-
getDefaultTypeValue
Gets the default value of a primitive type.- Parameters:
objectType
- the primitive object class- Returns:
- the default value of a primitive type
-
getNotFinalFields
Returns all the not final fields.- Parameters:
clazz
- the class containing fields.skipStatic
- if true the static fields are skipped.- Returns:
- a list containing all the not final fields.
-