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.
-
-
Constructor Summary
Constructors Constructor Description ClassUtils()
Default constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods 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
getAllArgsConstructor(Class<K> clazz)
Retrieves the all args constructor.ClassType
getClassType(Class<?> clazz)
Returns the class type.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> 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.
-
-
-
Method Detail
-
isPrimitiveOrSpecialType
public boolean isPrimitiveOrSpecialType(Class<?> clazz)
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
public boolean isPrimitiveType(Class<?> clazz)
Checks if an object is a special type.- Parameters:
clazz
- the class to check- Returns:
- true if is special type, false otherwise
-
isPrimitiveTypeArray
public boolean isPrimitiveTypeArray(Class<?> clazz)
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
public boolean isSpecialType(Class<?> clazz)
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
public static boolean isDouble(Class<?> type)
Checks if the given type is aDouble
.- Parameters:
type
- the class to check- Returns:
- true if is Double
-
isFloat
public static boolean isFloat(Class<?> type)
Checks if the given type is aFloat
.- Parameters:
type
- the class to check- Returns:
- true if is Float
-
isLong
public static boolean isLong(Class<?> type)
Checks if the given type is aLong
.- Parameters:
type
- the class to check- Returns:
- true if is Long
-
isShort
public static boolean isShort(Class<?> type)
Checks if the given type is aShort
.- Parameters:
type
- the class to check- Returns:
- true if is Short
-
isInt
public static boolean isInt(Class<?> type)
Checks if the given type is anInteger
.- Parameters:
type
- the class to check- Returns:
- true if is Integer
-
isByte
public static boolean isByte(Class<?> type)
Checks if the given type is aByte
.- Parameters:
type
- the class to check- Returns:
- true if is Byte
-
isChar
public static boolean isChar(Class<?> type)
Checks if the given type is aCharacter
.- Parameters:
type
- the class to check- Returns:
- true if is Character
-
isBoolean
public static boolean isBoolean(Class<?> type)
Checks if the given type is aBoolean
.- Parameters:
type
- the class to check- Returns:
- true if is Boolean
-
isString
public static boolean isString(Class<?> type)
Checks if the given type is a String.- Parameters:
type
- the class to check- Returns:
- true if is String
-
isBigInteger
public static boolean isBigInteger(Class<?> type)
Checks if the given type is aBigInteger
.- Parameters:
type
- the class to check- Returns:
- true if is String
-
isBigDecimal
public static boolean isBigDecimal(Class<?> type)
Checks if the given type is aBigDecimal
.- Parameters:
type
- the class to check- Returns:
- true if is String
-
isByteArray
public static boolean isByteArray(Class<?> type)
Checks if the given type is a byte[].- Parameters:
type
- the class to check- Returns:
- true if is String
-
getPrivateFinalFields
public List<Field> getPrivateFinalFields(Class<?> clazz)
Return the private final fields of a class.- Parameters:
clazz
- class from which gets the field- Returns:
- a list of private final fields.
-
getTotalFields
public int getTotalFields(Class<?> clazz, Predicate<? super Field> predicate)
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
public List<Field> getPrivateFields(Class<?> clazz)
Return the private fields of a class.- Parameters:
clazz
- class from which gets the field- Returns:
- a list of private final fields.
-
getPrivateFields
public List<Field> getPrivateFields(Class<?> clazz, boolean skipFinal)
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
public List<Field> getDeclaredFields(Class<?> clazz, boolean skipStatic)
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.
-
hasAccessibleConstructors
public <K> boolean hasAccessibleConstructors(Class<K> targetClass)
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
public Class[] getDeclaredClasses(Class<?> clazz)
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
-
getInstance
public <T> T getInstance(Constructor constructor, Object... constructorArgs)
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
public <K> Supplier<K> getNoArgsConstructor(Class<K> clazz)
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
public <K> Constructor getAllArgsConstructor(Class<K> clazz)
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
public Parameter[] getConstructorParameters(Constructor constructor)
Gets all the constructor parameters.- Parameters:
constructor
- the constructor.- Returns:
- the constructor parameters
-
hasField
public boolean hasField(Object target, String fieldName)
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
public boolean hasSetterMethods(Class<?> clazz)
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
public boolean hasFinalFields(Class<?> clazz)
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
public boolean areParameterNamesAvailable(Constructor constructor)
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
public ClassType getClassType(Class<?> clazz)
Returns the class type.- Parameters:
clazz
- the class to check- Returns:
- the class type
ClassType
-
getSetterMethods
public List<Method> getSetterMethods(Class<?> clazz)
Retrieves all the setters method for the given class.- Parameters:
clazz
- the clazz containing the methods.- Returns:
- all the class setter methods
-
getDefaultTypeValue
public Object getDefaultTypeValue(Class<?> objectType)
Gets the default value of a primitive type.- Parameters:
objectType
- the primitive object class- Returns:
- the default value of a primitive type
-
-