Class Reflect

  • All Implemented Interfaces:
    Serializable

    public final class Reflect
    extends Object
    Wrapper around an object or class allowing easy access to reflection operations.
    Author:
    David B. Bracewell
    See Also:
    Serialized Form
    • Method Detail

      • getClassForName

        public static Class<?> getClassForName​(String name)
                                        throws Exception

        Attempts to determine the Class for a given name in string form. For convenience the String, primitives and Object versions of the primitives can be called with just their name. The following types also have a short hand

        • List - java.util.List
        • ArrayList - java.util.ArrayList
        • Set - java.util.Set
        • HashSet - java.util.HashSet
        • Map - java.util.Map
        • HashMap - java.util.HashMap

        Array versions can be created by appending [] to the end. For example, String[] refers to an array of java.lang.String

        Parameters:
        name - The class name
        Returns:
        The represented by the name
        Throws:
        Exception - the exception
      • getClassForNameQuietly

        public static Class<?> getClassForNameQuietly​(String name)

        Calls getClassForName(String), but suppresses exception to a log warning and returns null instead. Any exceptions are logged to the default logger.(at DEBUG level) and a null is returned.

        Parameters:
        name - Name of class
        Returns:
        The Class information or null
      • onClass

        public static Reflect onClass​(@NonNull
                                      @NonNull Class<?> clazz)
        Creates a Reflect object for the given class.
        Parameters:
        clazz - the class we want reflective access to
        Returns:
        the reflect object
      • onClass

        public static Reflect onClass​(@NonNull
                                      @NonNull Type clazz)
        Creates a Reflect object for the given type.
        Parameters:
        clazz - the type we want reflective access to
        Returns:
        the reflect object
      • onClass

        public static Reflect onClass​(String className)
                               throws ReflectionException
        Creates a Reflect object for the class represented by the given class name.
        Parameters:
        className - the name of the class we want reflective access to
        Returns:
        the reflect object
        Throws:
        ReflectionException - the reflection exception
      • onObject

        public static Reflect onObject​(Object object)
        Creates a Reflect object on the given object
        Parameters:
        object - the object we want reflective access to
        Returns:
        the reflect object
      • allowPrivilegedAccess

        public Reflect allowPrivilegedAccess()
        Allow privileged access during reflective calls.
        Returns:
        this reflect object
      • containsField

        public boolean containsField​(String name)
        Determines if a field with the given name is associated with the class
        Parameters:
        name - The field name
        Returns:
        True if there is a field with the given name
      • containsMethod

        public boolean containsMethod​(String name)
        Determines if a method with the given name is associated with the class
        Parameters:
        name - The method name
        Returns:
        True if there is a method with the given name
      • containsMethod

        public boolean containsMethod​(String name,
                                      Type... parameters)
        Determines if a method with the given name is associated with the class
        Parameters:
        name - The method name
        Returns:
        True if there is a method with the given name
      • create

        public Reflect create()
                       throws ReflectionException
        Creates an instance of the class being reflected using the no-argument constructor.
        Returns:
        A Reflect object to do further reflection
        Throws:
        ReflectionException - Something went wrong constructing the object
      • create

        public Reflect create​(Object... args)
                       throws ReflectionException
        Creates an instance of the class being reflected using the most specific constructor available.
        Parameters:
        args - The arguments to the constructor.
        Returns:
        A Reflect object to do further reflection
        Throws:
        ReflectionException - Something went wrong constructing the object
      • create

        public Reflect create​(@NonNull
                              @NonNull Class[] types,
                              @NonNull
                              @NonNull Object... args)
                       throws ReflectionException
        Creates an instance of the class being reflected using the best constructor that matches the given types.
        Parameters:
        types - The type of the given arguments.
        args - The arguments to the constructor.
        Returns:
        A Reflect object to do further reflection
        Throws:
        ReflectionException - Something went wrong constructing the object
      • get

        public <T> T get()
        Gets the underlying object.
        Type Parameters:
        T - the type parameter
        Returns:
        the underlying object or null if we are reflecting a class
      • get

        public <T> T get​(String name)
                  throws ReflectionException
        Convenience method for getting the value of a field or getter method
        Type Parameters:
        T - the value type
        Parameters:
        name - the name of the field
        Returns:
        the value of the field
        Throws:
        ReflectionException - Something went wrong accessing the field
      • getAncestors

        public Iterable<Reflect> getAncestors​(boolean reverseOrder)
        Gets an iterable of the ancestors of this class including super classes and interfaces.
        Parameters:
        reverseOrder - True - order starting at Object, False order starting at superclass.
        Returns:
        the iterable of Reflect objects representing super-classes and interfaces
      • getConstructor

        public RConstructor getConstructor​(@NonNull
                                           @NonNull Type... types)
                                    throws ReflectionException
        Gets the best constructor for the class matching the given types
        Parameters:
        types - The types (possibly empty) of the constructor parameters
        Returns:
        the best constructor constructor for the class
        Throws:
        ReflectionException - Either could not find an appropriate constructor or security did not allow reflective access.
      • getConstructorsWhere

        public final List<RConstructor> getConstructorsWhere​(@NonNull
                                                             @NonNull SerializablePredicate<? super RConstructor> predicate)
        Gets the constructors for the class matching the given predicate
        Parameters:
        predicate - The predicate to use for filtering the constructors
        Returns:
        the constructors for the class matching the given predicate
      • getConstructorsWithAnnotation

        @SafeVarargs
        public final List<RConstructor> getConstructorsWithAnnotation​(@NonNull
                                                                      @NonNull Class<? extends Annotation>... annotationClasses)
        Gets the constructors for the class with at least of the given annotations.
        Parameters:
        annotationClasses - The annotation classes to search for
        Returns:
        the constructors for the class with at least of the given annotations.
      • getDeclaringClass

        public Reflect getDeclaringClass()
        Gets the class that declares the reflected object
        Returns:
        the declaring class
      • getElement

        public Class<?> getElement()
        Gets the AnnotatedElement
        Returns:
        the AnnotatedElement object
      • getFields

        public List<RField> getFields()
        Gets all fields.
        Returns:
        the fields
      • getFieldsWhere

        public List<RField> getFieldsWhere​(@NonNull
                                           @NonNull SerializablePredicate<RField> predicate)
        Gets all fields that match the given predicate
        Parameters:
        predicate - the predicate to use for filtering the fields
        Returns:
        the fields matching the given predicate
      • getFieldsWithAnnotation

        @SafeVarargs
        public final List<RField> getFieldsWithAnnotation​(@NonNull
                                                          @NonNull Class<? extends Annotation>... annotationClasses)
        Gets the fields for the class with at least of the given annotations.
        Parameters:
        annotationClasses - The annotation classes to search for
        Returns:
        the fields for the class with at least of the given annotations.
      • getMethod

        public RMethod getMethod​(String name,
                                 Type... types)
                          throws ReflectionException
        Gets the method with the given name and with types compatible with the given types.
        Parameters:
        name - the name of the method
        types - the types of the method parameters
        Returns:
        the reflected method
        Throws:
        ReflectionException - No such method
      • getMethods

        public List<RMethod> getMethods​(String name)
        Gets all methods with the given name.
        Parameters:
        name - the name of the method
        Returns:
        the list of reflected methods
      • getMethods

        public List<RMethod> getMethods()
        Gets all methods.
        Returns:
        the list of reflected methods
      • getMethodsWhere

        public List<RMethod> getMethodsWhere​(String name,
                                             @NonNull
                                             @NonNull SerializablePredicate<? super RMethod> predicate)
        Gets the methods with the given name for the class matching the given predicate
        Parameters:
        name - the name of the method
        predicate - The predicate to use for filtering the constructors
        Returns:
        the methods for the class matching the given predicate
      • getMethodsWhere

        public List<RMethod> getMethodsWhere​(@NonNull
                                             @NonNull SerializablePredicate<? super RMethod> predicate)
        Gets the methods for the class matching the given predicate
        Parameters:
        predicate - The predicate to use for filtering the constructors
        Returns:
        the methods for the class matching the given predicate
      • getMethodsWithAnnotation

        @SafeVarargs
        public final List<RMethod> getMethodsWithAnnotation​(@NonNull
                                                            @NonNull Class<? extends Annotation>... annotationClasses)
        Gets the methods for the class with at least of the given annotations.
        Parameters:
        annotationClasses - The annotation classes to search for
        Returns:
        the methods for the class with at least of the given annotations.
      • getModifiers

        public int getModifiers()
        Gets the modifiers on this field.
        Returns:
        the modifiers
      • getName

        public String getName()
        Gets the name of the element
        Returns:
        the name of the element
      • getSuperClass

        public Reflect getSuperClass()
        Gets the super-class of the class being reflected
        Returns:
        the reflected super class
      • getType

        public Class<?> getType()
        Gets the type of the element
        Returns:
        the type of the element
      • isPrivileged

        public boolean isPrivileged()
        is privileged access allowed on this object?
        Returns:
        True - privileged access is allowed, False - no privileged access is allowed
      • isSingleton

        public boolean isSingleton()
        Does the reflected class have a singleton creation method (getInstance, getSingleton, or createInstance)
        Returns:
        True - if the class being reflected is a singleton, False otherwise
      • set

        public Reflect set​(String name,
                           Object value)
                    throws ReflectionException
        Convenience method for setting the value of a field or setter
        Parameters:
        name - the name of the field
        value - the value to set the field to
        Returns:
        this Reflect object
        Throws:
        ReflectionException - Something went wrong accessing the field
      • setIsPrivileged

        public Reflect setIsPrivileged​(boolean allowPrivilegedAccess)
        Sets whether or not privileged access is allowed on this object
        Parameters:
        allowPrivilegedAccess - True - privileged access is allowed, False - no privileged access is allowed
        Returns:
        this object
      • getAnnotation

        public final <A extends Annotation> A getAnnotation​(Class<A> annotationClass)
        Gets the annotation of the given class present on this object.
        Type Parameters:
        A - the annotation type parameter
        Parameters:
        annotationClass - the annotation class
        Returns:
        the annotation (null if does not exist)
      • getAnnotation

        public final <A extends Annotation> A getAnnotation​(@NonNull
                                                            @NonNull Class<A> annotationClass,
                                                            boolean onlyDirect)
        Gets the annotation of the given class on this object. When onlyDirect is true it will only return annotations directly present via getDeclaredAnnotation otherwise will return any instance present via getAnnotation.
        Type Parameters:
        A - the annotation type parameter
        Parameters:
        annotationClass - the annotation class
        onlyDirect - True - only declared annotations, False present annotations
        Returns:
        the annotation (null if does not exist)
      • getAnnotations

        public final Annotation[] getAnnotations​(boolean onlyDirect)
        Gets all annotations on this object. When onlyDirect is true it will only return annotations directly present via getDeclaredAnnotations otherwise will return any instance present via getAnnotations.
        Parameters:
        onlyDirect - True - only declared annotations, False present annotations
        Returns:
        the array of Annotation
      • getAnnotations

        public final Annotation[] getAnnotations()
        Gets all annotations present on this object.
        Returns:
        the array of Annotation
      • getAnnotations

        public final <A extends Annotation> A[] getAnnotations​(@NonNull
                                                               @NonNull Class<A> annotationClass,
                                                               boolean onlyDirect)
        Gets all annotations on this object. When onlyDirect is true it will only return annotations directly present via getDeclaredAnnotationsByType otherwise will return any instance present via getAnnotationsByType.
        Type Parameters:
        A - the annotation type parameter
        Parameters:
        annotationClass - the annotation class
        onlyDirect - True - only declared annotations, False present annotations
        Returns:
        the array of Annotation
      • getAnnotations

        public final <A extends Annotation> A[] getAnnotations​(@NonNull
                                                               @NonNull Class<A> annotationClass)
        Gets all associated annotations of given type on this object.
        Type Parameters:
        A - the annotation type parameter
        Parameters:
        annotationClass - the annotation class
        Returns:
        the array of Annotation
      • isAnnotationDeclared

        @SafeVarargs
        public final boolean isAnnotationDeclared​(@NonNull
                                                  @NonNull Class<? extends Annotation>... annotationClasses)
        Determines if any of the given annotations are declared on this object
        Parameters:
        annotationClasses - the annotation classes
        Returns:
        True - if any of the given annotations are declared on this object
      • isAnnotationPresent

        @SafeVarargs
        public final boolean isAnnotationPresent​(@NonNull
                                                 @NonNull Class<? extends Annotation>... annotationClasses)
        Determines if any of the given annotations are present on this object
        Parameters:
        annotationClasses - the annotation classes
        Returns:
        True - if any of the given annotations are present on this object
      • processAnnotation

        public final <A extends Annotation,​O> O processAnnotation​(@NonNull
                                                                        @NonNull Class<A> annotationClass,
                                                                        @NonNull
                                                                        @NonNull CheckedFunction<? super A,​? extends O> function)
                                                                 throws ReflectionException
        Applies the given CheckedBiFunction to this object with the given annotation if it is present on the object.
        Type Parameters:
        A - the annotation type parameter
        O - the return type parameter
        Parameters:
        annotationClass - the annotation class
        function - the function to apply
        Returns:
        the return value of the function or null if no annotation was present
        Throws:
        ReflectionException - Something went wrong during reflection
      • processAnnotations

        public final <A extends Annotation,​O> List<O> processAnnotations​(@NonNull
                                                                               @NonNull Class<A> annotationClass,
                                                                               @NonNull
                                                                               @NonNull CheckedFunction<? super A,​? extends O> function)
                                                                        throws ReflectionException
        Applies the given CheckedBiFunction to this object and all instances of the given annotation present on the object.
        Type Parameters:
        A - the annotation type parameter
        O - the return type parameter
        Parameters:
        annotationClass - the annotation class
        function - the function to apply
        Returns:
        A list of the return values of the function or null if no annotation was present
        Throws:
        ReflectionException - Something went wrong during reflection
      • withAnnotation

        public final <A extends Annotation> V withAnnotation​(@NonNull
                                                             @NonNull Class<A> annotationClass,
                                                             @NonNull
                                                             @NonNull CheckedConsumer<? super A> consumer)
                                                      throws ReflectionException
        Applies the given CheckedBiConsumer to this object with the given annotation if it is present on the object.
        Type Parameters:
        A - the annotation type parameter
        Parameters:
        annotationClass - the annotation class
        consumer - the consumer to apply
        Returns:
        This object
        Throws:
        ReflectionException - Something went wrong during reflection
      • withAnnotations

        public final <A extends Annotation> V withAnnotations​(@NonNull
                                                              @NonNull Class<A> annotationClass,
                                                              @NonNull
                                                              @NonNull CheckedConsumer<? super A> consumer)
                                                       throws ReflectionException
        Applies the given CheckedBiConsumer to this object and all instances of the given annotation present on this object.
        Type Parameters:
        A - the annotation type parameter
        Parameters:
        annotationClass - the annotation class
        consumer - the consumer to apply
        Returns:
        This object
        Throws:
        ReflectionException - Something went wrong during reflection