Class ReflectionPatterns

java.lang.Object
eu.bandm.tools.paisley.ReflectionPatterns

public abstract class ReflectionPatterns extends Object
Static factory methods for creating patterns for runtime type information.
  • Field Details

    • classIsInstance

      static final Method classIsInstance
    • classCast

      static final Method classCast
  • Method Details

    • isSystem

      static boolean isSystem(Class<?> cls)
      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:

      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

      public static Pattern<Object> isInstanceOf(Class<?> cls)
      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 invoking Class.isInstance(Object).
      Throws:
      NullPointerException - if cls is null.
      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 of cls, determined as if by successfully invoking Class.cast(java.lang.Object).
      Throws:
      NullPointerException - if cls is null.
      NullPointerException - if body is null.
      See Also:
    • forInstancesOf

      public static <A, B extends A> Motif<B,A> forInstancesOf(Class<? extends A> src, Class<? extends B> tgt)
    • forInstancesOf

      public static <B> Motif<B,Object> forInstancesOf(Class<? extends B> cls)
    • matchField

      public static Pattern<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.

      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 - if field is null.
      NullPointerException - if body is null.
      See Also:
    • matchMethod

      public static Pattern<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.

      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 - if field is null.
      IllegalArgumentException - if body is null.
      IllegalArgumentException - if args is null.
      See Also:
    • matchClass

      public 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.
      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 - if cls is null.
      See Also: