From 128d0618edbdd967bb68337abad065eb01c2d75a Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Mon, 20 May 2024 00:24:23 +0200 Subject: [PATCH] ChangeMethodTargetToStatic should also change instance usage For https://github.com/openrewrite/rewrite-migrate-java/pull/477 --- .../java/ChangeMethodTargetToStaticTest.java | 39 +++++++++++++++++++ .../java/ChangeMethodTargetToStatic.java | 13 ++++--- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeMethodTargetToStaticTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeMethodTargetToStaticTest.java index c585778803d..3728ba4ae64 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeMethodTargetToStaticTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeMethodTargetToStaticTest.java @@ -146,6 +146,45 @@ public void test() { ); } + @Test + void staticMethodCalledOnInstanceToCallOnClass() { + rewriteRun( + spec -> spec.recipe(new ChangeMethodTargetToStatic("a.A method()", "a.A", null, null)), + java( + """ + package a; + public class A { + public static void method() {} + } + """ + ), + java( + """ + import a.A; + class Test { + public void test() { + A.method(); + new A().method(); + A a = new A(); + a.method(); + } + } + """, + """ + import a.A; + class Test { + public void test() { + A.method(); + A.method(); + A a = new A(); + A.method(); + } + } + """ + ) + ); + } + @SuppressWarnings("ResultOfMethodCallIgnored") @Test @Issue("https://github.com/openrewrite/rewrite/issues/3085") diff --git a/rewrite-java/src/main/java/org/openrewrite/java/ChangeMethodTargetToStatic.java b/rewrite-java/src/main/java/org/openrewrite/java/ChangeMethodTargetToStatic.java index b79e71ccdb4..7def157c889 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/ChangeMethodTargetToStatic.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/ChangeMethodTargetToStatic.java @@ -113,11 +113,12 @@ public ChangeMethodTargetToStaticVisitor(MethodMatcher methodMatcher, boolean ma @Override public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { J.MethodInvocation m = super.visitMethodInvocation(method, ctx); + Expression select = method.getSelect(); boolean isStatic = method.getMethodType() != null && method.getMethodType().hasFlags(Flag.Static); - boolean isSameReceiverType = method.getSelect() != null && - TypeUtils.isOfClassType(method.getSelect().getType(), fullyQualifiedTargetTypeName); - - if ((!isStatic || !isSameReceiverType) && methodMatcher.matches(method, matchUnknownTypes)) { + boolean isSameReceiverType = select != null && TypeUtils.isOfClassType(select.getType(), fullyQualifiedTargetTypeName); + boolean calledOnTargetType = select instanceof J.Identifier && ((J.Identifier) select).getFieldType() == null; + if ((!isStatic || !isSameReceiverType || !calledOnTargetType) && + methodMatcher.matches(method, matchUnknownTypes)) { JavaType.Method transformedType = null; if (method.getMethodType() != null) { maybeRemoveImport(method.getMethodType().getDeclaringType()); @@ -138,9 +139,9 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu maybeAddImport(fullyQualifiedTargetTypeName, !matchUnknownTypes); m = method.withSelect( new J.Identifier(randomId(), - method.getSelect() == null ? + select == null ? Space.EMPTY : - method.getSelect().getPrefix(), + select.getPrefix(), Markers.EMPTY, emptyList(), classType.getClassName(),