Class ClassUtils


  • public final class ClassUtils
    extends Object
    Reflection utils for Class objects.
    • Constructor Detail

      • ClassUtils

        public ClassUtils()
        Default constructor.
    • 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 a Double.
        Parameters:
        type - the class to check
        Returns:
        true if is Double
      • isFloat

        public static boolean isFloat​(Class<?> type)
        Checks if the given type is a Float.
        Parameters:
        type - the class to check
        Returns:
        true if is Float
      • isLong

        public static boolean isLong​(Class<?> type)
        Checks if the given type is a Long.
        Parameters:
        type - the class to check
        Returns:
        true if is Long
      • isShort

        public static boolean isShort​(Class<?> type)
        Checks if the given type is a Short.
        Parameters:
        type - the class to check
        Returns:
        true if is Short
      • isInt

        public static boolean isInt​(Class<?> type)
        Checks if the given type is an Integer.
        Parameters:
        type - the class to check
        Returns:
        true if is Integer
      • isByte

        public static boolean isByte​(Class<?> type)
        Checks if the given type is a Byte.
        Parameters:
        type - the class to check
        Returns:
        true if is Byte
      • isChar

        public static boolean isChar​(Class<?> type)
        Checks if the given type is a Character.
        Parameters:
        type - the class to check
        Returns:
        true if is Character
      • isBoolean

        public static boolean isBoolean​(Class<?> type)
        Checks if the given type is a Boolean.
        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 a BigInteger.
        Parameters:
        type - the class to check
        Returns:
        true if is String
      • isBigDecimal

        public static boolean isBigDecimal​(Class<?> type)
        Checks if the given type is a BigDecimal.
        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 field
        predicate - 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 field
        skipFinal - 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 field
        skipStatic - 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 be
        fieldName - 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
      • getNotFinalFields

        public List<Field> getNotFinalFields​(Class<?> clazz,
                                             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.