Class ReflectionUtil


  • public class ReflectionUtil
    extends Object
    • Constructor Detail

      • ReflectionUtil

        public ReflectionUtil()
    • Method Detail

      • getField

        public static Field getField​(Class clazz,
                                     String name)
        Get a field from a given class
        Parameters:
        clazz - clazz
        name - field name
        Returns:
        field object
      • getFieldValue

        public static <T> T getFieldValue​(Object target,
                                          String name)
        Get a field value from a given object
        Parameters:
        target - Object whose field is being read
        name - field name
        Returns:
        field object
      • getStaticFieldValue

        public static <T> T getStaticFieldValue​(Class<?> target,
                                                String name)
        Get a field value from a given class
        Parameters:
        target - Class whose field is being read
        name - field name
        Returns:
        field value
      • setField

        public static void setField​(Object target,
                                    Field field,
                                    Object value)
        Set target Object field to a certain value
        Parameters:
        target - Object whose field is being set
        field - Object field to set
        value - the new value for the given field
      • setField

        public static void setField​(Object target,
                                    String fieldName,
                                    Object value)
        Set target Object field to a certain value
        Parameters:
        target - Object whose field is being set
        fieldName - Object field naem to set
        value - the new value for the given field
      • setStaticField

        public static void setStaticField​(Class<?> target,
                                          String fieldName,
                                          Object value)
        Set target Class field to a certain value
        Parameters:
        target - Class whose field is being set
        fieldName - Class field name to set
        value - the new value for the given field
      • newInstance

        public static <T> T newInstance​(Supplier<Constructor<T>> constructorSupplier,
                                        Object... args)
        New target Object instance using the given arguments
        Parameters:
        constructorSupplier - constructor supplier
        args - Constructor arguments
        Returns:
        new Object instance
      • newInstance

        public static <T> T newInstance​(String className)
        New target Object instance using the given Class name
        Parameters:
        className - class name
        Returns:
        new Object instance
      • getSetter

        public static Method getSetter​(Object target,
                                       String property,
                                       Class<?> parameterType)
        Get setter method
        Parameters:
        target - target object
        property - property
        parameterType - setter parameter type
        Returns:
        setter method
      • getMethod

        public static Method getMethod​(Object target,
                                       String methodName,
                                       Class... parameterTypes)
        Get target method
        Parameters:
        target - target object
        methodName - method name
        parameterTypes - method parameter types
        Returns:
        return value
      • setProperty

        public static void setProperty​(Object target,
                                       String property,
                                       Object parameter)
        Invoke setter method with the given parameter
        Parameters:
        target - target object
        property - property
        parameter - setter parameter