From 581944e5ed56030b50ebabaeaffb0b7c7922b38a Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Mon, 4 Nov 2024 08:40:55 +0100 Subject: [PATCH] `MethodMatcher`: Optimize parameters case `(..)` Using `(..)` to match any parameters is very common for `MethodMatchers`. This case is now correctly optimized, as there was a bug in `matchAllArguments()`. --- .../src/main/java/org/openrewrite/java/MethodMatcher.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/rewrite-java/src/main/java/org/openrewrite/java/MethodMatcher.java b/rewrite-java/src/main/java/org/openrewrite/java/MethodMatcher.java index e2bdfd7ee85..6c1b6119d77 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/MethodMatcher.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/MethodMatcher.java @@ -91,7 +91,7 @@ public MethodMatcher(String signature, @Nullable Boolean matchOverrides) { this(signature, Boolean.TRUE.equals(matchOverrides)); } - public @Nullable MethodMatcher(String signature, boolean matchOverrides) { + public MethodMatcher(String signature, boolean matchOverrides) { this.matchOverrides = matchOverrides; MethodSignatureParser parser = new MethodSignatureParser(new CommonTokenStream(new MethodSignatureLexer( @@ -138,7 +138,7 @@ public MethodMatcher(String signature, @Nullable Boolean matchOverrides) { } private static boolean matchAllArguments(MethodSignatureParser.FormalsPatternContext context) { - return context.dotDot() != null && context.formalsPatternAfterDotDot() == null; + return context.dotDot() != null && context.formalsPatternAfterDotDot().isEmpty(); } private static boolean isPlainIdentifier(MethodSignatureParser.TargetTypePatternContext context) { @@ -344,6 +344,10 @@ private boolean matchesAllowingUnknownTypes(J.MethodInvocation method) { return false; } + if (argumentPattern == ANY_ARGUMENTS_PATTERN) { + return true; + } + final String argumentSignature = argumentsFromExpressionTypes(method); final Pattern relaxedArgumentPattern = Pattern.compile( argumentPattern.pattern().replaceAll("((?:[a-zA-Z0-9]+\\.?)+)",