From a28d29e23fe75fa7994cd9fab1110121ac44f6ec Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 26 Aug 2026 00:14:50 +0200 Subject: [PATCH] Drop rewrite-analysis from ReplaceCollectionToArrayArgWithEmptyArray `UsesInvocation` documents itself as equivalent to `UsesMethod` and only exists because `MethodMatcher` has no base interface; since the matcher here is a plain `MethodMatcher`, `UsesMethod` applies directly and uses the indexed `TypesInUse.hasMethodUse(..)` rather than iterating every used method. `InvocationMatcher.advanced().isFirstArgument(..)` expands to a cursor walk to the enclosing `MethodCall` plus an identity check on its first argument, so it is inlined against `MethodMatcher.matches(MethodCall)`, which is identical to the `InvocationMatcher` default it replaced. The dependency remains for the recipes that use its dataflow engine. --- ...eplaceCollectionToArrayArgWithEmptyArray.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/openrewrite/staticanalysis/ReplaceCollectionToArrayArgWithEmptyArray.java b/src/main/java/org/openrewrite/staticanalysis/ReplaceCollectionToArrayArgWithEmptyArray.java index c25769340..98556419e 100644 --- a/src/main/java/org/openrewrite/staticanalysis/ReplaceCollectionToArrayArgWithEmptyArray.java +++ b/src/main/java/org/openrewrite/staticanalysis/ReplaceCollectionToArrayArgWithEmptyArray.java @@ -17,12 +17,13 @@ import lombok.Getter; import org.openrewrite.*; -import org.openrewrite.analysis.InvocationMatcher; -import org.openrewrite.analysis.search.UsesInvocation; import org.openrewrite.internal.ListUtils; import org.openrewrite.java.JavaIsoVisitor; +import org.openrewrite.java.MethodMatcher; +import org.openrewrite.java.search.UsesMethod; import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.JavaType; +import org.openrewrite.java.tree.MethodCall; import org.openrewrite.java.tree.Space; import org.openrewrite.marker.Markers; @@ -50,20 +51,23 @@ public class ReplaceCollectionToArrayArgWithEmptyArray extends Recipe { @Override public TreeVisitor getVisitor() { return Preconditions.check( - new UsesInvocation<>(ReplaceCollectionToArrayArgWithEmptyArrayVisitor.COLLECTION_TO_ARRAY), + new UsesMethod<>(ReplaceCollectionToArrayArgWithEmptyArrayVisitor.COLLECTION_TO_ARRAY), new ReplaceCollectionToArrayArgWithEmptyArrayVisitor<>() ); } private static class ReplaceCollectionToArrayArgWithEmptyArrayVisitor

extends JavaIsoVisitor

{ - private static final InvocationMatcher COLLECTION_TO_ARRAY = - InvocationMatcher.fromMethodMatcher("java.util.Collection toArray(..)"); + private static final MethodMatcher COLLECTION_TO_ARRAY = + new MethodMatcher("java.util.Collection toArray(..)"); @Override public J.NewArray visitNewArray(J.NewArray newArray, P p) { boolean isInitializerEmpty = newArray.getInitializer() == null || (newArray.getInitializer().size() == 1 && newArray.getInitializer().get(0) instanceof J.Empty); - if (COLLECTION_TO_ARRAY.advanced().isFirstArgument(getCursor()) && isInitializerEmpty) { + Tree parent = getCursor().getParentTreeCursor().getValue(); + MethodCall call = parent instanceof MethodCall ? (MethodCall) parent : null; + if (isInitializerEmpty && call != null && !call.getArguments().isEmpty() && + call.getArguments().get(0) == newArray && COLLECTION_TO_ARRAY.matches(call)) { J.NewArray newArrayZero = newArray.withDimensions(ListUtils.mapFirst(newArray.getDimensions(), d -> { if (d.getIndex() instanceof J.Literal && Integer.valueOf(0).equals(((J.Literal) d.getIndex()).getValue())) { return d;