Package randoop.reflection
Class ReflectionManager
- java.lang.Object
-
- randoop.reflection.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
andvalueOf
. - methods defined for enum constants that satisfy predicate.
Note that visitors may have their own predicates, but need not check accessibility.
-
-
Field Summary
Fields Modifier and Type Field Description (package private) boolean
logToStdout
If true, output diagnostics to stdout rather than to a log.private AccessibilityPredicate
predicate
The accessibility predicate for classes and class members.private ArrayList<ClassVisitor>
visitors
The visitors to apply.
-
Constructor Summary
Constructors Constructor Description ReflectionManager(AccessibilityPredicate predicate)
Creates a manager object that uses the given predicate to determine which classes, methods, and constructors should be visited.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(ClassVisitor visitor)
Registers aClassVisitor
for use by theapply(Class)
method.void
apply(Class<?> c)
Applies the registeredClassVisitor
objects of this object to the given class and its members that satisfy the given predicate.void
apply(ClassVisitor visitor, Class<?> c)
Applies the givenClassVisitor
visitor to the class object and its members that satisfy the predicate of this reflection manager.private void
applyTo(ClassVisitor v, Class<?> c)
Apply a visitor to the member class.private void
applyTo(ClassVisitor v, Enum<?> e)
Apply a visitor to a enum value.private void
applyTo(ClassVisitor v, Constructor<?> co)
Apply a visitor to a constructor.private void
applyTo(ClassVisitor v, Field f)
Apply a visitor to a field.private void
applyTo(ClassVisitor v, Method m)
Apply a visitor to a method.private void
applyToEnum(ClassVisitor visitor, Class<?> c)
Applies the visitors to the constants and methods of the given enum.private boolean
isAccessible(Constructor<?> c)
Determines whether a constructor and each of its parameter types are accessible.private boolean
isAccessible(Method m)
Determines whether a method, its parameter types, and its return type are all accessible.private boolean
isAccessible(Type type)
Determines whether ajava.lang.reflect.Type
is a type accessible by to the generated tests.private void
logPrintf(String fmt, Object... args)
Log a diagnostic message with formatting.private void
logPrintln(String s)
Log a one-line literal diagnostic message.private void
visitAfter(ClassVisitor v, Class<?> c)
Apply a visitor to a class.private void
visitBefore(ClassVisitor v, Class<?> c)
Apply a visitor to a class.
-
-
-
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.
-
visitors
private ArrayList<ClassVisitor> visitors
The visitors to apply.
-
-
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
-
add
public void add(ClassVisitor visitor)
Registers aClassVisitor
for use by theapply(Class)
method.- Parameters:
visitor
- theClassVisitor
object to add
-
apply
public void apply(Class<?> c)
Applies the registeredClassVisitor
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
- theClass
object to be visited
-
apply
public void apply(ClassVisitor visitor, Class<?> c)
Applies the givenClassVisitor
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
- theClassVisitor
to apply to the classc
- 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
andvalueOf
since their definition is implicit, and we aren't testing Java enum implementation.- Parameters:
visitor
- theClassVisitor
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
- theClassVisitor
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
- theClassVisitor
c
- the member class to be visited
-
applyTo
private void applyTo(ClassVisitor v, Constructor<?> co)
Apply a visitor to a constructor.- Parameters:
v
- theClassVisitor
co
- the constructor to be visited
-
applyTo
private void applyTo(ClassVisitor v, Method m)
Apply a visitor to a method.- Parameters:
v
- theClassVisitor
m
- the method to be visited
-
applyTo
private void applyTo(ClassVisitor v, Enum<?> e)
Apply a visitor to a enum value.- Parameters:
v
- theClassVisitor
e
- the enum value to be visited
-
visitBefore
private void visitBefore(ClassVisitor v, Class<?> c)
Apply a visitor to a class. Called at the beginning ofapply(Class)
.- Parameters:
v
- theClassVisitor
c
- the class to be visited
-
visitAfter
private void visitAfter(ClassVisitor v, Class<?> c)
Apply a visitor to a class. Called at the end ofapply(Class)
.- Parameters:
v
- theClassVisitor
c
- the class 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 ajava.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 stringargs
- 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
-
-