Class MethodMatcher

java.lang.Object
org.openrewrite.java.MethodMatcher

public class MethodMatcher extends Object
This class accepts an AspectJ method pattern and is used to identify methods that match the expression. The format of the method pattern is as follows:

#declaring class# #method name#(#argument list#)

  • The declaring class must be fully qualified.
  • A wildcard character, "*", may be used in either the declaring class or method name.
  • The argument list is expressed as a comma-separated list of the argument types
  • ".." can be used in the argument list to match zero or more arguments of any type.
  •  EXAMPLES:
     

    *..* *(..) - All method invocations java.util.* *(..) - All method invocations to classes belonging to java.util (including sub-packages) java.util.Collections *(..) - All method invocations on java.util.Collections class java.util.Collections unmodifiable*(..) - All method invocations starting with "unmodifiable" on java.util.Collections java.util.Collections min(..) - All method invocations for all overloads of "min" java.util.Collections emptyList() - All method invocations on java.util.Collections.emptyList() my.org.MyClass *(boolean, ..) - All method invocations where the first arg is a boolean in my.org.MyClass

    • Constructor Details

      • MethodMatcher

        public MethodMatcher(String signature, @Nullable @Nullable Boolean matchOverrides)
      • MethodMatcher

        public MethodMatcher(String signature, boolean matchOverrides)
      • MethodMatcher

        public MethodMatcher(org.openrewrite.java.tree.J.MethodDeclaration method, boolean matchOverrides)
      • MethodMatcher

        public MethodMatcher(String signature)
      • MethodMatcher

        public MethodMatcher(org.openrewrite.java.tree.J.MethodDeclaration method)
      • MethodMatcher

        public MethodMatcher(JavaType.Method method)
    • Method Details

      • getTargetTypePattern

        @Deprecated public Pattern getTargetTypePattern()
        Deprecated.
      • getMethodNamePattern

        @Deprecated public Pattern getMethodNamePattern()
        Deprecated.
      • getArgumentPattern

        @Deprecated public Pattern getArgumentPattern()
        Deprecated.
      • matches

        public boolean matches(@Nullable JavaType.Method type)
      • matches

        public boolean matches(@Nullable @Nullable MethodCall methodCall)
      • matches

        public boolean matches(@Nullable @Nullable Expression maybeMethod)
      • matches

        public boolean matches(org.openrewrite.java.tree.J.MethodDeclaration method, org.openrewrite.java.tree.J.ClassDeclaration enclosing)
      • matches

        public boolean matches(@Nullable org.openrewrite.java.tree.J.MethodInvocation method, boolean matchUnknownTypes)
        Prefer matches(MethodCall), which uses the default `false` behavior for matchUnknownTypes. Using matchUnknownTypes can improve Visitor resiliency for an AST with missing type information, but also increases the risk of false-positive matches on unrelated MethodInvocation instances.
      • isFullyQualifiedClassReference

        public boolean isFullyQualifiedClassReference(org.openrewrite.java.tree.J.FieldAccess fieldAccess)
        Evaluate whether this MethodMatcher and the specified FieldAccess are describing the same type or not. Known limitation/bug: MethodMatchers can have patterns/wildcards like "com.*.Bar" instead of something concrete like "com.foo.Bar". This limitation is not desirable or intentional and should be fixed. If a methodMatcher is passed that includes wildcards the result will always be "false"
        Parameters:
        fieldAccess - A J.FieldAccess that hopefully has the same fully qualified type as this matcher.
      • methodPattern

        public static String methodPattern(org.openrewrite.java.tree.J.MethodDeclaration method)
      • methodPattern

        public static String methodPattern(JavaType.Method method)
      • toString

        public String toString()
        Overrides:
        toString in class Object