Skip to content

Commit

Permalink
MethodMatcher: Optimize parameters case (..)
Browse files Browse the repository at this point in the history
Using `(..)` to match any parameters is very common for `MethodMatchers`. This case is now correctly optimized, as there was a bug in `matchAllArguments()`.
  • Loading branch information
knutwannheden committed Nov 4, 2024
1 parent b09b22f commit 581944e
Showing 1 changed file with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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]+\\.?)+)",
Expand Down

0 comments on commit 581944e

Please sign in to comment.