Class ReflectionManager


  • public class ReflectionManager
    extends Object
    ReflectionManager contains a set of visitors and an accessibility predicate. It applies each visitor to each declaration (class, method, field) that satisfies the predicate.

    For a non-enum class, visits:

    • all methods satisfying predicate.
    • all constructors satisfying predicate.
    • all fields that satisfy predicate and are not shadowed. (A shadowed field is a member of a superclass with the same name as a field in the current class. These are accessible via reflection.)
    • inner enums satisfying predicate.

    For an enum, visits:

    • all enum constants.
    • methods of the enum satisfying predicate other than values and valueOf.
    • methods defined for enum constants that satisfy predicate.

    Note that visitors may have their own predicates, but need not check accessibility.

    • Field Detail

      • logToStdout

        boolean logToStdout
        If true, output diagnostics to stdout rather than to a log. Useful when running unit tests, which don't do logging.
      • predicate

        private AccessibilityPredicate predicate
        The accessibility predicate for classes and class members.

        DO NOT use this field directly (except on classes and fields)! Instead, call the methods isAccessible() that are defined in this class.

    • Constructor Detail

      • ReflectionManager

        public ReflectionManager​(AccessibilityPredicate predicate)
        Creates a manager object that uses the given predicate to determine which classes, methods, and constructors should be visited. The list of visitors is initially empty.
        Parameters:
        predicate - the predicate to indicate whether classes and class members should be visited
    • Method Detail

      • apply

        public void apply​(Class<?> c)
        Applies the registered ClassVisitor objects of this object to the given class and its members that satisfy the given predicate. Excludes fields that are shadowed by inheritance that are otherwise still accessible by reflection. Each visitor is applied to each member at most once.
        Parameters:
        c - the Class object to be visited
      • apply

        public void apply​(ClassVisitor visitor,
                          Class<?> c)
        Applies the given ClassVisitor visitor to the class object and its members that satisfy the predicate of this reflection manager.

        Sorts class members to ensure a canonical order for visits.

        Parameters:
        visitor - the ClassVisitor to apply to the class
        c - the class
      • applyToEnum

        private void applyToEnum​(ClassVisitor visitor,
                                 Class<?> c)
        Applies the visitors to the constants and methods of the given enum. A method is included if it satisfies the predicate, and either is declared in the enum, or in the anonymous class of some constant. Note that methods will either belong to the enum itself, or to an anonymous class attached to a constant. Ordinarily, the type of the constant is the enum, but when there is an anonymous class for constant e, e.getClass() returns the anonymous class. This is used to check for method overrides (that could include Object methods) within the constant.

        Heuristically exclude methods values and valueOf since their definition is implicit, and we aren't testing Java enum implementation.

        Parameters:
        visitor - the ClassVisitor
        c - the enum class object from which constants and methods are extracted
      • applyTo

        private void applyTo​(ClassVisitor v,
                             Field f)
        Apply a visitor to a field.
        Parameters:
        v - the ClassVisitor
        f - the field to be visited
      • applyTo

        private void applyTo​(ClassVisitor v,
                             Class<?> c)
        Apply a visitor to the member class.

        The ReflectionManager does not apply itself to the member class, since it could violate assumptions in the visitor. And, so instead allows the visitor to implement that call if it is necessary.

        Parameters:
        v - the ClassVisitor
        c - the member class to be visited
      • applyTo

        private void applyTo​(ClassVisitor v,
                             Method m)
        Apply a visitor to a method.
        Parameters:
        v - the ClassVisitor
        m - the method to be visited
      • applyTo

        private void applyTo​(ClassVisitor v,
                             Enum<?> e)
        Apply a visitor to a enum value.
        Parameters:
        v - the ClassVisitor
        e - the enum value to be visited
      • isAccessible

        private boolean isAccessible​(Method m)
        Determines whether a method, its parameter types, and its return type are all accessible.
        Parameters:
        m - the method to check for accessibility
        Returns:
        true if the method, each parameter type, and the return type are all accessible; and false otherwise
      • isAccessible

        private boolean isAccessible​(Constructor<?> c)
        Determines whether a constructor and each of its parameter types are accessible.
        Parameters:
        c - the constructor
        Returns:
        true if the constructor and each parameter type are accessible; false, otherwise
      • isAccessible

        private boolean isAccessible​(Type type)
        Determines whether a java.lang.reflect.Type is a type accessible by to the generated tests.
        Parameters:
        type - the type to check
        Returns:
        true if the type is accessible, false otherwise
      • logPrintf

        private void logPrintf​(String fmt,
                               Object... args)
        Log a diagnostic message with formatting.
        Parameters:
        fmt - the format string
        args - the arguments to the format string
      • logPrintln

        private void logPrintln​(String s)
        Log a one-line literal diagnostic message.
        Parameters:
        s - the message, a complete line without line terminator