Class ReflectionPatterns
-
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <A,
B extends A>
Motif<B,A> forInstancesOf
(Class<? extends A> src, Class<? extends B> tgt) forInstancesOf
(Class<? extends B> cls) static <A,
B extends A>
Pattern<A>forInstancesOf
(Class<? extends B> cls, Pattern<? super B> body) Returns a pattern that delegates to a given pattern after dynamic type checking.isInstanceOf
(Class<?> cls) Returns a pattern that matches objects of a given class.(package private) static boolean
Checks whether the given class is considered a system class.static <A extends Annotation>
Pattern<AnnotatedElement>matchAnnotation
(Class<? extends A> cls, Pattern<? super A> body) Returns a pattern that matches a given pattern against an annotation of the target object.static <A> Pattern<A>
matchClass
(Pattern<? super Class<? extends A>> p) Returns a pattern that matches a given pattern against the class of the target object.matchField
(Field field, Pattern<Object> body) Returns a pattern that matches a given pattern against the value of a field of the target object.matchMethod
(Method method, Pattern<Object> body, Object... args) Returns a pattern that matches a given pattern against the return value of a method of the target object.
-
Field Details
-
classIsInstance
-
classCast
-
-
Method Details
-
isSystem
Checks whether the given class is considered a system class. A class is a system class if and only if its class loader is either the bootstrap class laoder or the system class loader.System classes can be handled more efficiently in compilation:
- References to system classes can be embedded as literal class references into generated class files.
- Reflective
operations on system classes (such as
Class.isInstance(java.lang.Object)
andClass.cast(java.lang.Object)
) and their members (such asField.get(java.lang.Object)
andMethod.invoke(java.lang.Object, java.lang.Object...)
) can be compiled to primitive JVM instructions.
Since compiled patterns are loaded with a class loader that delegates to class loader used to load the
paisley
library, which delegates to the system class loader, which in turn delegates to the bootstrap class loader, references to system classes by name, embedded in class files generated for compiled patterns, are guaranteed to resolve to the same classes again.Warning: Behavior is undefined if the
paisley
library is loaded with a class loader that does not follow the delegation model.- See Also:
-
isInstanceOf
Returns a pattern that matches objects of a given class.A pattern returned by this method is deterministic and binds no variables.
- Parameters:
cls
- the class of objects to match.- Returns:
- a pattern that matches objects of type
cls
, determined as if by invokingClass.isInstance(Object)
. - Throws:
NullPointerException
- ifcls
isnull
.- See Also:
-
forInstancesOf
public static <A,B extends A> Pattern<A> forInstancesOf(Class<? extends B> cls, Pattern<? super B> body) Returns a pattern that delegates to a given pattern after dynamic type checking.- Parameters:
cls
- the class to be checked against.body
- the pattern to match against objects of the class.- Returns:
- a pattern that matches whatever the pattern
body
matches, but only if the target object is an instance ofcls
, determined as if by successfully invokingClass.cast(java.lang.Object)
. - Throws:
NullPointerException
- ifcls
isnull
.NullPointerException
- ifbody
isnull
.- See Also:
-
forInstancesOf
-
forInstancesOf
-
matchField
Returns a pattern that matches a given pattern against the value of a field of the target object.Exceptions raised by the reflection machinery cause the pattern to fail silently.
- Parameters:
field
- the field to match against.body
- the pattern to match against the field.- Throws:
NullPointerException
- iffield
isnull
.NullPointerException
- ifbody
isnull
.- See Also:
-
matchMethod
Returns a pattern that matches a given pattern against the return value of a method of the target object.Exceptions raised by the reflection machinery cause the pattern to fail silently.
- Parameters:
method
- the method to invoke for the match.body
- the pattern to match against the method's return value.args
- the arguments to invoke the method with.- Throws:
IllegalArgumentException
- iffield
isnull
.IllegalArgumentException
- ifbody
isnull
.IllegalArgumentException
- ifargs
isnull
.- See Also:
-
matchClass
Returns a pattern that matches a given pattern against the class of the target object.- Parameters:
p
- the pattern to match against the class.- See Also:
-
matchAnnotation
public static <A extends Annotation> Pattern<AnnotatedElement> matchAnnotation(Class<? extends A> cls, Pattern<? super A> body) Returns a pattern that matches a given pattern against an annotation of the target object.If no such annotation is present on the target object, the match fails silently. The absence of an annotation can be matched negatively by
matchAnnotation(cls, Pattern.any()).noMatch()
.- Throws:
IllegalArgumentException
- ifcls
isnull
.- See Also:
-