Class ReflectionUtils


  • public final class ReflectionUtils
    extends java.lang.Object
    Reflection class utils.
    • Constructor Summary

      Constructors 
      Constructor Description
      ReflectionUtils()
      Default constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Class<?> getArgumentTypeClass​(java.lang.Object argument, java.lang.String declaringClass, java.lang.String fieldName, boolean getNestedGenericClass)
      Gets the class of a given object.
      java.lang.Class<?> getArrayType​(java.lang.reflect.Field arrayField)
      Gets the type of an array.
      java.lang.reflect.Method getBuildMethod​(java.lang.Class<?> builderClass)
      Get build method inside the class.
      java.lang.reflect.Field getDeclaredField​(java.lang.String fieldName, java.lang.Class<?> targetClass)
      Return the field of the given class.
      <A extends java.lang.annotation.Annotation>
      A
      getFieldAnnotation​(java.lang.reflect.Field field, java.lang.Class<A> annotationClazz)
      Returns (if existing) the field's given type annotation.
      java.lang.Object getFieldValue​(java.lang.Object target, java.lang.reflect.Field field)
      Gets the value of a field.
      java.lang.Object getFieldValue​(java.lang.Object target, java.lang.String fieldName, java.lang.Class<?> fieldType)
      Gets the value of a field through getter method.
      java.lang.Class<?> getGenericFieldType​(java.lang.reflect.Field field)
      Gets the generic infer type of an object.
      MapType getMapGenericType​(java.lang.reflect.Type fieldType, java.lang.String declaringClass, java.lang.String fieldName)
      Gets the generic infer type of a map object.
      <A extends java.lang.annotation.Annotation>
      A
      getParameterAnnotation​(java.lang.reflect.Parameter parameter, java.lang.Class<A> annotationClazz, java.lang.String declaringClassName)
      Returns (if existing) the constructor parameter's given type annotation.
      java.lang.reflect.Method getSetterMethodForField​(java.lang.Class<?> fieldClass, java.lang.String fieldName, java.lang.Class<?> fieldType)
      Gets the setter method for a specific field.
      java.lang.Object invokeMethod​(java.lang.reflect.Method method, java.lang.Object target, java.lang.Object... args)
      Invokes the method.
      boolean isSetter​(java.lang.reflect.Method method)
      Checks if the given method is a setter.
      void setFieldValue​(java.lang.Object target, java.lang.reflect.Field field, java.lang.Object fieldValue)
      Sets the value of a field through setter method.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ReflectionUtils

        public ReflectionUtils()
        Default constructor.
    • Method Detail

      • invokeMethod

        public java.lang.Object invokeMethod​(java.lang.reflect.Method method,
                                             java.lang.Object target,
                                             java.lang.Object... args)
        Invokes the method.
        Parameters:
        method - the method to be invoked.
        target - the class on which invoke the method
        args - the method parameters
        Returns:
        the method result
      • isSetter

        public boolean isSetter​(java.lang.reflect.Method method)
        Checks if the given method is a setter.
        Parameters:
        method - the method to be checked
        Returns:
        true if the method is a setter method, false otherwise
      • getFieldValue

        public java.lang.Object getFieldValue​(java.lang.Object target,
                                              java.lang.reflect.Field field)
        Gets the value of a field.
        Parameters:
        target - the field's class
        field - the field Field
        Returns:
        the field value
      • getFieldValue

        public java.lang.Object getFieldValue​(java.lang.Object target,
                                              java.lang.String fieldName,
                                              java.lang.Class<?> fieldType)
        Gets the value of a field through getter method.
        Parameters:
        target - the field's class
        fieldName - the field name
        fieldType - the field type
        Returns:
        the field value
      • getFieldAnnotation

        public <A extends java.lang.annotation.Annotation> A getFieldAnnotation​(java.lang.reflect.Field field,
                                                                                java.lang.Class<A> annotationClazz)
        Returns (if existing) the field's given type annotation.
        Type Parameters:
        A - the annotation type object
        Parameters:
        field - the field that should have the annotation
        annotationClazz - the annotation type
        Returns:
        the annotation
      • getBuildMethod

        public java.lang.reflect.Method getBuildMethod​(java.lang.Class<?> builderClass)
        Get build method inside the class.
        Parameters:
        builderClass - Class whit a build method (see Builder Pattern)
        Returns:
        Build method if present
      • getParameterAnnotation

        public <A extends java.lang.annotation.Annotation> A getParameterAnnotation​(java.lang.reflect.Parameter parameter,
                                                                                    java.lang.Class<A> annotationClazz,
                                                                                    java.lang.String declaringClassName)
        Returns (if existing) the constructor parameter's given type annotation.
        Type Parameters:
        A - the annotation type object
        Parameters:
        parameter - the field that should have the annotation
        annotationClazz - the annotation type
        declaringClassName - the class name that contains the parameter
        Returns:
        the annotation
      • getDeclaredField

        public java.lang.reflect.Field getDeclaredField​(java.lang.String fieldName,
                                                        java.lang.Class<?> targetClass)
        Return the field of the given class.
        Parameters:
        fieldName - the name of the filed to retrieve.
        targetClass - the field's class
        Returns:
        the field corresponding to the given name.
      • setFieldValue

        public void setFieldValue​(java.lang.Object target,
                                  java.lang.reflect.Field field,
                                  java.lang.Object fieldValue)
        Sets the value of a field through setter method.
        Parameters:
        target - the field's class
        field - the field to set
        fieldValue - the value to set
      • getSetterMethodForField

        public java.lang.reflect.Method getSetterMethodForField​(java.lang.Class<?> fieldClass,
                                                                java.lang.String fieldName,
                                                                java.lang.Class<?> fieldType)
        Gets the setter method for a specific field.
        Parameters:
        fieldClass - class containing the field
        fieldName - the name of the field to be retrieved
        fieldType - the field class
        Returns:
        the setter method
        Throws:
        MissingMethodException - if the method does not exists
      • getGenericFieldType

        public java.lang.Class<?> getGenericFieldType​(java.lang.reflect.Field field)
        Gets the generic infer type of an object.
        Parameters:
        field - the field containing the generic
        Returns:
        the generic type class
      • getMapGenericType

        public MapType getMapGenericType​(java.lang.reflect.Type fieldType,
                                         java.lang.String declaringClass,
                                         java.lang.String fieldName)
        Gets the generic infer type of a map object.
        Parameters:
        fieldType - the field containing the generic
        declaringClass - the class containing the field
        fieldName - the field name
        Returns:
        the generic type class
      • getArgumentTypeClass

        public java.lang.Class<?> getArgumentTypeClass​(java.lang.Object argument,
                                                       java.lang.String declaringClass,
                                                       java.lang.String fieldName,
                                                       boolean getNestedGenericClass)
        Gets the class of a given object.
        Parameters:
        argument - the object from which the class has to be retrieved
        declaringClass - the class containing the field of which the argument belongs to
        fieldName - the field name of which the argument belongs to
        getNestedGenericClass - if true it retrieves the class of the object generic (if any). i.e. argument = List<String>; returns String, if false returns List
        Returns:
        the given argument class type
      • getArrayType

        public java.lang.Class<?> getArrayType​(java.lang.reflect.Field arrayField)
        Gets the type of an array.
        Parameters:
        arrayField - the array
        Returns:
        the array class