Class ReflectionUtil

java.lang.Object
org.seppiko.commons.utils.ReflectionUtil

public class ReflectionUtil
extends java.lang.Object
Reflection Util
Author:
Leonard Woo
  • Nested Class Summary

    Nested Classes
    Modifier and Type Class Description
    static interface  ReflectionUtil.MethodCallback
    doMethod callback
    static interface  ReflectionUtil.MethodFilter
    doMethod filter
  • Constructor Summary

    Constructors
    Constructor Description
    ReflectionUtil()  
  • Method Summary

    Modifier and Type Method Description
    static java.lang.reflect.Field findField​(java.lang.Class<?> clazz, java.lang.String name, java.lang.Class<?> type)
    find field with type
    static <T> java.lang.reflect.Method findMethod​(java.lang.Class<T> clazz, java.lang.String methodName)
    find method
    static <T> java.lang.reflect.Method findMethod​(java.lang.Class<T> clazz, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
    find a method with parameter types
    static java.lang.reflect.Method[] getAllDeclaredMethods​(java.lang.Class<?> clazz)
    get methods
    static java.lang.reflect.Field[] getDeclaredFields​(java.lang.Class<?> clazz)
    get all fields
    static java.lang.reflect.Method[] getDeclaredMethods​(java.lang.Class<?> clazz)
    get methods
    static <T> java.lang.Object getField​(java.lang.reflect.Field field, T target)
    get field value
    static java.lang.reflect.Parameter[] getParameters​(java.lang.reflect.Method method)
    get method parameters
    static java.lang.Class<?>[] getParameterTypes​(java.lang.Object[] args)
    get parameters type
    static java.lang.Object invokeMethod​(java.lang.reflect.Method method, java.lang.Object target)
    invoke method without parameter
    static java.lang.Object invokeMethod​(java.lang.reflect.Method method, java.lang.Object target, java.lang.Object... args)
    invoke method on target instance
    static <T> java.lang.Object invokeMethod​(T t, java.lang.String methodName, java.lang.Class<?>[] parameterTypes, java.lang.Object... args)
    class instance method caller
    static <T> java.lang.Object invokeMethod​(T t, java.lang.String methodName, java.lang.Object... args)
    class instance method caller
    static <T> T newInstance​(java.lang.Class<T> clazz)
    get class instance without parameter
    static <T> T newInstance​(java.lang.Class<T> clazz, java.lang.Class<?>[] parameterTypes, java.lang.Object[] initArgs)
    get class instance with parameter
    static <T,​ V> void setField​(java.lang.reflect.Field field, T target, V value)
    set field value

    Methods inherited from class java.lang.Object

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

    • ReflectionUtil

      public ReflectionUtil()
  • Method Details

    • newInstance

      public static <T> T newInstance​(java.lang.Class<T> clazz) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, java.lang.InstantiationException
      get class instance without parameter
      Type Parameters:
      T - target class type
      Parameters:
      clazz - target class
      Returns:
      class instance
      Throws:
      java.lang.NoSuchMethodException - not found constructor method
      java.lang.IllegalAccessException - can not access constructor method
      java.lang.reflect.InvocationTargetException - can not call constructor method
      java.lang.InstantiationException - can not instance
    • newInstance

      public static <T> T newInstance​(java.lang.Class<T> clazz, java.lang.Class<?>[] parameterTypes, java.lang.Object[] initArgs) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, java.lang.InstantiationException
      get class instance with parameter
      Type Parameters:
      T - target class type
      Parameters:
      clazz - target class
      parameterTypes - class constructor parameter types
      initArgs - class constructor parameter objects
      Returns:
      class instance
      Throws:
      java.lang.NoSuchMethodException - not found constructor method
      java.lang.IllegalAccessException - can not access constructor method
      java.lang.reflect.InvocationTargetException - can not call constructor method
      java.lang.InstantiationException - can not instance
    • findMethod

      public static <T> java.lang.reflect.Method findMethod​(java.lang.Class<T> clazz, java.lang.String methodName) throws java.lang.NoSuchMethodException, java.lang.SecurityException
      find method
      Type Parameters:
      T - class type
      Parameters:
      clazz - method class
      methodName - method name
      Returns:
      method object
      Throws:
      java.lang.NoSuchMethodException - not found method
      java.lang.SecurityException - access failed
    • findMethod

      public static <T> java.lang.reflect.Method findMethod​(java.lang.Class<T> clazz, java.lang.String methodName, java.lang.Class<?>... parameterTypes) throws java.lang.NoSuchMethodException, java.lang.SecurityException
      find a method with parameter types
      Type Parameters:
      T - class type
      Parameters:
      clazz - method class
      methodName - method name
      parameterTypes - parameter types
      Returns:
      method object
      Throws:
      java.lang.NoSuchMethodException - not found method
      java.lang.SecurityException - access failed
    • invokeMethod

      public static <T> java.lang.Object invokeMethod​(T t, java.lang.String methodName, java.lang.Object... args) throws java.lang.ReflectiveOperationException, java.lang.RuntimeException
      class instance method caller
      Type Parameters:
      T - target class
      Parameters:
      t - target class instance
      methodName - call method name
      args - parameter objects (can NOT use basic data types, cause nonsupport auto unboxing)
      Returns:
      method return object
      Throws:
      java.lang.ReflectiveOperationException - can NOT found method or can NOT access method
      java.lang.RuntimeException - method or parameter exception
    • invokeMethod

      public static <T> java.lang.Object invokeMethod​(T t, java.lang.String methodName, java.lang.Class<?>[] parameterTypes, java.lang.Object... args) throws java.lang.ReflectiveOperationException, java.lang.RuntimeException
      class instance method caller
      Type Parameters:
      T - target class
      Parameters:
      t - target class instance
      methodName - call method name
      parameterTypes - parameter types
      args - parameter objects
      Returns:
      method return object
      Throws:
      java.lang.ReflectiveOperationException - can NOT found method or can NOT access method
      java.lang.RuntimeException - method or parameter exception
    • getParameterTypes

      public static java.lang.Class<?>[] getParameterTypes​(java.lang.Object[] args)
      get parameters type
      Parameters:
      args - data type objects
      Returns:
      get object types
    • invokeMethod

      public static java.lang.Object invokeMethod​(java.lang.reflect.Method method, java.lang.Object target, java.lang.Object... args) throws java.lang.reflect.InvocationTargetException, java.lang.IllegalAccessException, java.lang.IllegalArgumentException
      invoke method on target instance
      Parameters:
      method - target method
      target - target class instance
      args - method parameters
      Returns:
      method return object (if exist)
      Throws:
      java.lang.reflect.InvocationTargetException - method throws an exception
      java.lang.IllegalAccessException - method can not access
      java.lang.IllegalArgumentException - method parameter type or size exception
    • invokeMethod

      public static java.lang.Object invokeMethod​(java.lang.reflect.Method method, java.lang.Object target) throws java.lang.reflect.InvocationTargetException, java.lang.IllegalAccessException, java.lang.IllegalArgumentException
      invoke method without parameter
      Parameters:
      method - target method
      target - target class instance
      Returns:
      method return object (if exist)
      Throws:
      java.lang.reflect.InvocationTargetException - method throws an exception
      java.lang.IllegalAccessException - method can not access
      java.lang.IllegalArgumentException - method parameter type or size exception
    • getDeclaredMethods

      public static java.lang.reflect.Method[] getDeclaredMethods​(java.lang.Class<?> clazz) throws java.lang.SecurityException
      get methods
      Parameters:
      clazz - target class
      Returns:
      methods
      Throws:
      java.lang.SecurityException - can not access method or some else
    • getAllDeclaredMethods

      public static java.lang.reflect.Method[] getAllDeclaredMethods​(java.lang.Class<?> clazz) throws java.lang.SecurityException, java.lang.IllegalAccessException
      get methods
      Parameters:
      clazz - target class
      Returns:
      methods
      Throws:
      java.lang.SecurityException - can not access method or some else
      java.lang.IllegalAccessException - method can not access
    • findField

      public static java.lang.reflect.Field findField​(java.lang.Class<?> clazz, java.lang.String name, java.lang.Class<?> type) throws java.lang.NoSuchFieldException
      find field with type
      Parameters:
      clazz - target class
      name - field name
      type - field type
      Returns:
      field object
      Throws:
      java.lang.NoSuchFieldException - not found field
    • getDeclaredFields

      public static java.lang.reflect.Field[] getDeclaredFields​(java.lang.Class<?> clazz) throws java.lang.SecurityException
      get all fields
      Parameters:
      clazz - target class
      Returns:
      fields
      Throws:
      java.lang.SecurityException - field security
    • getField

      public static <T> java.lang.Object getField​(java.lang.reflect.Field field, T target) throws java.lang.IllegalArgumentException, java.lang.IllegalAccessException
      get field value
      Type Parameters:
      T - field name type
      Parameters:
      field - target class field
      target - field name
      Returns:
      target field value
      Throws:
      java.lang.IllegalArgumentException - field not exist
      java.lang.IllegalAccessException - field access failed
    • setField

      public static <T,​ V> void setField​(java.lang.reflect.Field field, T target, V value) throws java.lang.IllegalArgumentException, java.lang.IllegalAccessException
      set field value
      Type Parameters:
      T - field name type
      V - field value type
      Parameters:
      field - target class field
      target - field name
      value - field value
      Throws:
      java.lang.IllegalArgumentException - field not exist
      java.lang.IllegalAccessException - field access failed
    • getParameters

      public static java.lang.reflect.Parameter[] getParameters​(java.lang.reflect.Method method)
      get method parameters
      Parameters:
      method - target method object
      Returns:
      parameters