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

    • Field Details

    • Constructor Details

      • MethodMatcher

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

        public MethodMatcher(String methodPattern, boolean matchOverrides)
      • MethodMatcher

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

        public MethodMatcher(String methodPattern)
      • MethodMatcher

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

        public MethodMatcher(JavaType.Method method)
    • Method Details

      • getDeclaringTypeMatchPrefix

        public @Nullable String getDeclaringTypeMatchPrefix()
        The literal prefix that the declaring-type pattern requires of any matching type's fully qualified name: everything in the type pattern up to the first wildcard metacharacter (* or ..), with $ canonicalized to .. A .. subpackage wildcard keeps the package separator that precedes it, since every type it matches has that separator in its FQN (java.util..* yields java.util.). Returns null when the pattern starts with a wildcard, i.e. no prefix can be required.

        Because the type portion of a method pattern is matched verbatim against the declaring type's name (ignoring matchOverrides), this prefix is a necessary prefix of any non-override match. Callers holding a collection of methods sorted by canonical declaring-type FQN can therefore binary-search to this prefix and scan only the startsWith(prefix) window, confirming each candidate with matches(JavaType.Method).

      • validate

        public static org.openrewrite.Validated<String> validate(@Nullable String signature)
      • matches

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

        public boolean matches(@Nullable MethodCall methodCall)
      • matches

        public boolean matches(@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(org.openrewrite.java.tree.J.MethodDeclaration method, org.openrewrite.java.tree.J.NewClass 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