Class ClassUtils


  • public final class ClassUtils
    extends Object
    Author:
    patrick.deenen Utility class for dealing with classes and annotations
    • Method Detail

      • canSetProperty

        public static boolean canSetProperty​(Object obj,
                                             String fieldName)
        Checks if the specified property can be set for the specified object. This method supports nested properties
        Parameters:
        obj - the object
        fieldName - the name of the field
        Returns:
      • clearFieldValue

        public static void clearFieldValue​(Object obj,
                                           String fieldName,
                                           Class<?> argType)
        Clears a field
        Parameters:
        obj - the object on which to clear the field
        fieldName - the name of the field
        argType -
      • forClass

        public static Class<?> forClass​(String clazz)
        forClass which doesn't throw an exception and can handle empty class names
        Parameters:
        clazz - The fully qualified class name
        Returns:
        the specified class and null when class is not found
      • getAnnotation

        public static <T extends Annotation> T getAnnotation​(Class<?> clazz,
                                                             String fieldName,
                                                             Class<T> annotationClass)
        Tries to retrieve an annotation, by first looking at the field name, and then at the getter method
        Parameters:
        clazz - the class
        fieldName - the name of the field
        annotationClass - the annotation class to look for
        Returns:
      • getAnnotationAttributeValue

        public static <T extends Annotation,​R> R getAnnotationAttributeValue​(Field field,
                                                                                   Class<T> annotationClass,
                                                                                   String attributeName)
        Get the value of a field in an annotation
        Parameters:
        field - The field with annotations
        annotionType - The name of the annotation type to find
        attributeName - The name of the attribute on the annotation type to find the value
        Returns:
        the value of the field of the annotation or null when not found
      • getAnnotationOnField

        public static <T extends Annotation> T getAnnotationOnField​(Class<?> clazz,
                                                                    String fieldName,
                                                                    Class<T> annotationClass)
        Return an annotation on a certain field
        Parameters:
        clazz - the class
        fieldName - the field name
        annotationClass - the class of the annotation
        Returns:
      • getAnnotationOnField

        public static <T extends Annotation> T getAnnotationOnField​(Field field,
                                                                    Class<T> annotationClass)
        Returns an annotation on a certain field
        Parameters:
        field - the field
        annotationClass - the annotation class
        Returns:
      • getAnnotationOnMethod

        public static <T extends Annotation> T getAnnotationOnMethod​(Class<?> clazz,
                                                                     String fieldName,
                                                                     Class<T> annotationClass)
        Looks for an annotation on a getter method
        Parameters:
        clazz - the class
        fieldName - the name of the field (will be prepended with "get")
        annotationClass - the class of the annotation to look for
        Returns:
      • getAnnotationOnMethod

        public static <T extends Annotation> T getAnnotationOnMethod​(Method method,
                                                                     Class<T> annotationClass)
        Looks for an annotation on a (getter) method
        Parameters:
        method - the method
        annotationClass - the class of the annotation
        Returns:
      • getBytes

        public static byte[] getBytes​(Object obj,
                                      String fieldName)
        Retrieves the contents of a field as a byte array
        Parameters:
        obj -
        fieldName -
        Returns:
      • getConstructor

        public static <T> Constructor<T> getConstructor​(Class<T> clazz,
                                                        Object... args)
        Find constructor based on the types of the given arguments used to instantiate the class with the found constructor
        Parameters:
        clazz -
        args -
        Returns:
      • getField

        public static Field getField​(Class<?> clazz,
                                     String fieldName)
        Returns a field with a certain name from a class
        Parameters:
        clazz - the class
        fieldName - the name of the filed
        Returns:
      • getFieldValue

        public static Object getFieldValue​(Object obj,
                                           String fieldName)
        Retrieves a field value
        Parameters:
        obj - the object from which to retrieve the field value
        fieldName - the name of the field
        Returns:
      • getFieldValueAsString

        public static String getFieldValueAsString​(Object obj,
                                                   String fieldName)
        Returns a field value as a String
        Parameters:
        obj - the object
        fieldName - the name of the field
        Returns:
      • getFieldValueAsString

        public static String getFieldValueAsString​(Object obj,
                                                   String fieldName,
                                                   String defValue)
        Returns a field value as a String
        Parameters:
        obj - the object
        fieldName - the name of the field
        defValue - the default value that is used in case of a null value
        Returns:
      • getGetterMethod

        public static Method getGetterMethod​(Class<?> clazz,
                                             String fieldName)
        Retrieves the getter method for a certain property
        Parameters:
        clazz - the class
        fieldName - the name of the property
        Returns:
      • getMaxLength

        public static int getMaxLength​(Class<?> clazz,
                                       String fieldName)
        Returns the maximum allowed length of a field
        Parameters:
        clazz -
        fieldName -
        Returns:
      • getPropertyDescriptorForNestedProperty

        public static PropertyDescriptor getPropertyDescriptorForNestedProperty​(Class<?> clazz,
                                                                                String property)
        Returns the property descriptor for a nested property
        Parameters:
        clazz - the class
        property - the name of the property
        Returns:
      • getResolvedType

        public static <T> Class<?> getResolvedType​(Class<T> type,
                                                   String fieldName,
                                                   int... indexes)
        Return a Class representing the generic parameter for the given indexes. Indexes are zero based; for example given the type Map>, getGeneric(0) will access the Integer. Nested generics can be accessed by specifying multiple indexes; for example getGeneric(1, 0) will access the String from the nested List. For convenience, if no indexes are specified the first generic is returned.
        Parameters:
        type -
        fieldName -
        indexes -
        Returns:
      • getResolvedType

        public static Class<?> getResolvedType​(Object object,
                                               int... indexes)
        Return a Class representing the generic parameter for the given indexes. Indexes are zero based; for example given the type Map>, getGeneric(0) will access the Integer. Nested generics can be accessed by specifying multiple indexes; for example getGeneric(1, 0) will access the String from the nested List. For convenience, if no indexes are specified the first generic is returned.
        Parameters:
        method - or method parameter or type
        indexes -
        Returns:
      • hasMethod

        public static boolean hasMethod​(Object obj,
                                        String methodName)
        Check if the object has a (public) method that has the specified name
        Parameters:
        obj -
        methodName -
        Returns:
      • instantiateClass

        public static <T> T instantiateClass​(Class<T> clazz,
                                             Object... args)
        Instantiate a class with the given arguments; assumed is that all arguments are not null so the types can be determined and a matching constructor can be found. When no constructor is found null is returned.
        Parameters:
        clazz -
        args -
        Returns:
      • setBytes

        public static void setBytes​(byte[] bytes,
                                    Object obj,
                                    String fieldName)
      • setFieldValue

        public static void setFieldValue​(Object obj,
                                         String fieldName,
                                         Object value)
        Parameters:
        obj -
        fieldName -
        value -
      • copyFields

        public static <T,​S extends T> void copyFields​(T from,
                                                            S to)