Class ClassUtils
java.lang.Object
com.expediagroup.transformer.utils.ClassUtils
public final class ClassUtils
extends java.lang.Object
Reflection utils for Class objects.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
BUILD_METHOD_NAME
Default method name used by a Builder for creating an object.static java.util.Set<java.lang.Class<?>>
CUSTOM_SPECIAL_TYPES
Custom special types. -
Constructor Summary
Constructors Constructor Description ClassUtils()
Default constructor. -
Method Summary
Modifier and Type Method Description boolean
allParameterAnnotatedWith(java.lang.reflect.Constructor constructor, java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
Checks if any of the class constructor's parameters are not annotated with the given class.boolean
areParameterNamesAvailable(java.lang.reflect.Constructor constructor)
Checks if the constructor's parameters names are defined.<K> java.lang.reflect.Constructor<K>
getAllArgsConstructor(java.lang.Class<K> clazz)
Retrieves the all args constructor.java.util.Optional<java.lang.Class<?>>
getBuilderClass(java.lang.Class<?> targetClass)
Returns the builder class.java.lang.reflect.Method
getBuildMethod(java.lang.Class<?> parentClass, java.lang.Class<?> builderClass)
Get build method inside the Builder class.ClassType
getClassType(java.lang.Class<?> clazz)
Returns the class type.java.lang.Class<?>
getConcreteClass(java.lang.reflect.Field field, java.lang.Object fieldValue)
Returns the concrete class of a field.java.lang.reflect.Parameter[]
getConstructorParameters(java.lang.reflect.Constructor constructor)
Gets all the constructor parameters.java.lang.Class[]
getDeclaredClasses(java.lang.Class<?> clazz)
Retrieves all classes defined into the given one.java.util.List<java.lang.reflect.Field>
getDeclaredFields(java.lang.Class<?> clazz, boolean skipStatic)
Return the fields of a class.java.lang.Object
getDefaultTypeValue(java.lang.Class<?> objectType)
Gets the default value of a primitive type.<T> java.lang.Class<?>
getFieldClass(java.lang.reflect.Field field, T objectInstance)
Returns the concrete class of a field.java.util.List<java.lang.reflect.Method>
getGetterMethods(java.lang.Class<?> clazz)
Retrieves all the getters method for the given class.<T> T
getInstance(java.lang.reflect.Constructor constructor, java.lang.Object... constructorArgs)
Creates an instance of the given class invoking the given constructor.<K> java.util.function.Supplier<K>
getNoArgsConstructor(java.lang.Class<K> clazz)
Retrieves the no args constructor.java.util.List<java.lang.reflect.Field>
getNotFinalFields(java.lang.Class<?> clazz, java.lang.Boolean skipStatic)
Returns all the not final fields.java.util.List<java.lang.reflect.Field>
getPrivateFields(java.lang.Class<?> clazz)
Return the private fields of a class.java.util.List<java.lang.reflect.Field>
getPrivateFields(java.lang.Class<?> clazz, boolean skipFinal)
Return the private fields of a class.java.util.List<java.lang.reflect.Field>
getPrivateFinalFields(java.lang.Class<?> clazz)
Return the private final fields of a class.java.util.List<java.lang.reflect.Method>
getSetterMethods(java.lang.Class<?> clazz)
Retrieves all the setters method for the given class.int
getTotalFields(java.lang.Class<?> clazz, java.util.function.Predicate<? super java.lang.reflect.Field> predicate)
Return the total fields matching with the given predicate.<K> boolean
hasAccessibleConstructors(java.lang.Class<K> targetClass)
Checks if the destination class has accessible constructor.boolean
hasField(java.lang.Object target, java.lang.String fieldName)
Checks that the class has a specific field.boolean
hasFinalFields(java.lang.Class<?> clazz)
Checks if a class has any final field.boolean
hasSetterMethods(java.lang.Class<?> clazz)
Checks if a class has setter methods.static boolean
isBigDecimal(java.lang.Class<?> type)
Checks if the given type is aBigDecimal
.static boolean
isBigInteger(java.lang.Class<?> type)
Checks if the given type is aBigInteger
.static boolean
isBoolean(java.lang.Class<?> type)
Checks if the given type is aBoolean
.static boolean
isByte(java.lang.Class<?> type)
Checks if the given type is aByte
.static boolean
isByteArray(java.lang.Class<?> type)
Checks if the given type is a byte[].static boolean
isChar(java.lang.Class<?> type)
Checks if the given type is aCharacter
.static boolean
isDouble(java.lang.Class<?> type)
Checks if the given type is aDouble
.static boolean
isFloat(java.lang.Class<?> type)
Checks if the given type is aFloat
.static boolean
isInt(java.lang.Class<?> type)
Checks if the given type is anInteger
.static boolean
isLong(java.lang.Class<?> type)
Checks if the given type is aLong
.boolean
isPrimitiveOrSpecialType(java.lang.Class<?> clazz)
Checks if an object is a primitive or special type.boolean
isPrimitiveType(java.lang.Class<?> clazz)
Checks if an object is a special type.boolean
isPrimitiveTypeArray(java.lang.Class<?> clazz)
Checks if an object is a primitive type array.static boolean
isShort(java.lang.Class<?> type)
Checks if the given type is aShort
.boolean
isSpecialType(java.lang.Class<?> clazz)
Checks if an object is a special type.static boolean
isString(java.lang.Class<?> type)
Checks if the given type is a String.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Field Details
-
BUILD_METHOD_NAME
public static final java.lang.String BUILD_METHOD_NAMEDefault method name used by a Builder for creating an object.- See Also:
- Constant Field Values
-
CUSTOM_SPECIAL_TYPES
public static final java.util.Set<java.lang.Class<?>> CUSTOM_SPECIAL_TYPESCustom special types.
-
-
Constructor Details
-
ClassUtils
public ClassUtils()Default constructor.
-
-
Method Details
-
isPrimitiveOrSpecialType
public boolean isPrimitiveOrSpecialType(java.lang.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(java.lang.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(java.lang.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(java.lang.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(java.lang.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(java.lang.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(java.lang.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(java.lang.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(java.lang.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(java.lang.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(java.lang.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(java.lang.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(java.lang.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(java.lang.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(java.lang.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(java.lang.Class<?> type)Checks if the given type is a byte[].- Parameters:
type
- the class to check- Returns:
- true if is String
-
getPrivateFinalFields
public java.util.List<java.lang.reflect.Field> getPrivateFinalFields(java.lang.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(java.lang.Class<?> clazz, java.util.function.Predicate<? super java.lang.reflect.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 java.util.List<java.lang.reflect.Field> getPrivateFields(java.lang.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 java.util.List<java.lang.reflect.Field> getPrivateFields(java.lang.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 java.util.List<java.lang.reflect.Field> getDeclaredFields(java.lang.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.
-
getFieldClass
public <T> java.lang.Class<?> getFieldClass(java.lang.reflect.Field field, T objectInstance)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
public java.lang.Class<?> getConcreteClass(java.lang.reflect.Field field, java.lang.Object fieldValue)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
public <K> boolean hasAccessibleConstructors(java.lang.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 java.lang.Class[] getDeclaredClasses(java.lang.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
-
getBuilderClass
public java.util.Optional<java.lang.Class<?>> getBuilderClass(java.lang.Class<?> targetClass)Returns the builder class.- Parameters:
targetClass
- the class where the builder should be searched- Returns:
- the Builder class if available.
-
getBuildMethod
public java.lang.reflect.Method getBuildMethod(java.lang.Class<?> parentClass, java.lang.Class<?> builderClass)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
public <T> T getInstance(java.lang.reflect.Constructor constructor, java.lang.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> java.util.function.Supplier<K> getNoArgsConstructor(java.lang.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> java.lang.reflect.Constructor<K> getAllArgsConstructor(java.lang.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 java.lang.reflect.Parameter[] getConstructorParameters(java.lang.reflect.Constructor constructor)Gets all the constructor parameters.- Parameters:
constructor
- the constructor.- Returns:
- the constructor parameters
-
hasField
public boolean hasField(java.lang.Object target, java.lang.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(java.lang.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(java.lang.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(java.lang.reflect.Constructor constructor, java.lang.Class<? extends java.lang.annotation.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(java.lang.reflect.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
Returns the class type.- Parameters:
clazz
- the class to check- Returns:
- the class type
ClassType
-
getSetterMethods
public java.util.List<java.lang.reflect.Method> getSetterMethods(java.lang.Class<?> clazz)Retrieves all the setters method for the given class.- Parameters:
clazz
- the class containing the methods.- Returns:
- all the class setter methods
-
getGetterMethods
public java.util.List<java.lang.reflect.Method> getGetterMethods(java.lang.Class<?> clazz)Retrieves all the getters method for the given class.- Parameters:
clazz
- the class containing the methods.- Returns:
- all the class getter methods
-
getDefaultTypeValue
public java.lang.Object getDefaultTypeValue(java.lang.Class<?> objectType)Gets the default value of a primitive type.- Parameters:
objectType
- the primitive object class- Returns:
- the default value of a primitive type
-
getNotFinalFields
public java.util.List<java.lang.reflect.Field> getNotFinalFields(java.lang.Class<?> clazz, java.lang.Boolean skipStatic)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.
-