Class Reflection


  • public class Reflection
    extends Object
    Security-agnostic "blueprint" class for reflection-related operations. Intended for use by Apache BVal code.
    • Constructor Detail

      • Reflection

        public Reflection()
    • Method Detail

      • primitiveToWrapper

        public static Class<?> primitiveToWrapper​(Class<?> cls)

        Converts the specified primitive Class object to its corresponding wrapper Class object.

        NOTE: From v2.2, this method handles Void.TYPE, returning Void.TYPE.

        Parameters:
        cls - the class to convert, may be null
        Returns:
        the wrapper class for cls or cls if cls is not a primitive. null if null input.
        Since:
        2.1
      • wrapperToPrimitive

        public static Class<?> wrapperToPrimitive​(Class<?> cls)
      • getDeclaredField

        public static Field getDeclaredField​(Class<?> clazz,
                                             String fieldName)
        Get the declared field from clazz.
        Parameters:
        clazz -
        fieldName -
        Returns:
        Field or null
      • getDeclaredConstructor

        public static <T> Constructor<T> getDeclaredConstructor​(Class<T> clazz,
                                                                Class<?>... parameters)
        Get the declared constructor from clazz.
        Parameters:
        clazz -
        parameters -
        Returns:
        Constructor or null
      • getDeclaredMethod

        public static Method getDeclaredMethod​(Class<?> clazz,
                                               String name,
                                               Class<?>... parameters)
        Get the declared method from clazz.
        Parameters:
        clazz -
        name -
        parameters -
        Returns:
        Method or null
      • getPublicMethod

        public static Method getPublicMethod​(Class<?> clazz,
                                             String methodName,
                                             Class<?>... parameterTypes)
        Get the specified public Method from clazz.
        Parameters:
        clazz -
        methodName -
        Returns:
        Method or null
      • find

        public static <T> T find​(Class<?> clazz,
                                 Function<Class<?>,​T> search)
        Perform a search against the class hierarchy.
        Parameters:
        clazz -
        search -
        Returns:
        T or null
      • newInstance

        public static <T> T newInstance​(Class<T> cls)
        Construct a new instance of cls using its default constructor.
        Parameters:
        cls -
        Returns:
        T
      • makeAccessible

        public static void makeAccessible​(AccessibleObject o)
        Set the accessibility of o to true.
        Parameters:
        o -
      • hierarchy

        public static Iterable<Class<?>> hierarchy​(Class<?> type,
                                                   Reflection.Interfaces interfacesBehavior)
        Get an Iterable that can iterate over a class hierarchy in ascending (subclass to superclass) order. Taken from commons-lang3.
        Parameters:
        type - the type to get the class hierarchy from
        interfacesBehavior - switch indicating whether to include or exclude interfaces
        Returns:
        Iterable an Iterable over the class hierarchy of the given class