Class ReflectionUtils


  • public class ReflectionUtils
    extends Object
    Inspired by Spring's ReflectionUtils.
    Since:
    1.0-beta-1
    Author:
    Keegan Witt
    • Method Detail

      • findMethod

        public static Method findMethod​(Class<?> clazz,
                                        String name,
                                        Class<?>... paramTypes)
        Attempt to find a Method on the supplied class with the supplied name and parameter types. Searches all superclasses up to Object.
        Parameters:
        clazz - The class to introspect
        name - The name of the method
        paramTypes - The parameter types of the method (may be null to indicate any signature)
        Returns:
        The Method object
      • findConstructor

        public static Constructor findConstructor​(Class<?> clazz,
                                                  Class<?>... paramTypes)
        Attempt to find a Constructor on the supplied class with the supplied parameter types. Searches all superclasses up to Object.
        Parameters:
        clazz - The class to introspect
        paramTypes - The parameter types of the method (may be null to indicate any signature)
        Returns:
        The Constructor object
      • invokeMethod

        public static Object invokeMethod​(Method method,
                                          Object target,
                                          Object... args)
                                   throws InvocationTargetException,
                                          IllegalAccessException
        Invoke the specified Method against the supplied target object with the supplied arguments. The target object can be null when invoking a static Method.
        Parameters:
        method - The method to invoke
        target - The target object to invoke the method on
        args - The invocation arguments (may be null)
        Returns:
        The invocation result, if any
        Throws:
        IllegalAccessException - when unable to access the specified method because access modifiers prevent it
        InvocationTargetException - when a reflection invocation fails
      • findField

        public static Field findField​(Class<?> clazz,
                                      String name,
                                      Class<?> type)
        Attempt to find a field on the supplied Class with the supplied name and/or type. Searches all superclasses up to Object.
        Parameters:
        clazz - The class to introspect
        name - The name of the field (may be null if type is specified)
        type - The type of the field (may be null if name is specified)
        Returns:
        The corresponding Field object
      • getField

        public static Object getField​(Field field,
                                      Object target)
                               throws IllegalAccessException
        Get the field represented by the supplied field object on the specified target object. In accordance with Field.get(Object) semantics, the returned value is automatically wrapped if the underlying field has a primitive type.
        Parameters:
        field - The field to get
        target - The target object from which to get the field
        Returns:
        The field's current value
        Throws:
        IllegalAccessException - when unable to access the specified field because access modifiers prevent it
      • getStaticField

        public static Object getStaticField​(Field field)
                                     throws IllegalAccessException
        Get the field represented by the supplied field object on the specified target object. In accordance with Field.get(Object) semantics, the returned value is automatically wrapped if the underlying field has a primitive type.
        Parameters:
        field - The field to get
        Returns:
        The field's current value
        Throws:
        IllegalAccessException - when unable to access the specified field because access modifiers prevent it
      • getEnumValue

        public static Object getEnumValue​(Class<?> clazz,
                                          String valueName)
        Find and return the specified value from the specified enum class.
        Parameters:
        clazz - The enum class to introspect
        valueName - The name of the enum value to get
        Returns:
        The enum value