From d1670ca2ca6530bf715f3b2d8d9828c8f95f66ae Mon Sep 17 00:00:00 2001 From: anuram Date: Fri, 5 Apr 2024 09:47:10 -0400 Subject: [PATCH 01/15] adding recipe RemovedToolProviderConstructor --- .../RemovedToolProviderConstructor.java | 94 +++++++++++++++++++ .../META-INF/rewrite/java-version-17.yml | 3 + .../RemovedToolProviderConstructorTest.java | 73 ++++++++++++++ 3 files changed, 170 insertions(+) create mode 100644 src/main/java/org/openrewrite/java/migrate/RemovedToolProviderConstructor.java create mode 100644 src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java diff --git a/src/main/java/org/openrewrite/java/migrate/RemovedToolProviderConstructor.java b/src/main/java/org/openrewrite/java/migrate/RemovedToolProviderConstructor.java new file mode 100644 index 0000000000..bb08187840 --- /dev/null +++ b/src/main/java/org/openrewrite/java/migrate/RemovedToolProviderConstructor.java @@ -0,0 +1,94 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.java.migrate; + +import org.openrewrite.ExecutionContext; +import org.openrewrite.Recipe; +import org.openrewrite.TreeVisitor; +import org.openrewrite.java.JavaVisitor; +import org.openrewrite.java.MethodMatcher; +import org.openrewrite.java.tree.Flag; +import org.openrewrite.java.tree.J; +import org.openrewrite.java.tree.JavaType; +import org.openrewrite.java.tree.Space; +import org.openrewrite.marker.Markers; + +import java.util.LinkedHashSet; +import java.util.Set; + +import static java.util.Collections.emptyList; +import static org.openrewrite.Tree.randomId; + +public class RemovedToolProviderConstructor extends Recipe { + + @Override + public String getDisplayName() { + return "TBD"; + } + + @Override + public String getDescription() { + return "TBD."; + } + + @Override + public TreeVisitor getVisitor() { + return new JavaVisitor() { + private final MethodMatcher COMPILER_METHODMATCHER = new MethodMatcher("javax.tools.ToolProvider getSystemJavaCompiler()", false); + private final MethodMatcher DOCUMENTATION_METHODMATCHER = new MethodMatcher("javax.tools.ToolProvider getSystemDocumentationTool()", false); + private final MethodMatcher CLASSLOADER_METHODMATCHER = new MethodMatcher("javax.tools.ToolProvider getSystemToolClassLoader()", false); + private final JavaType.FullyQualified classType = JavaType.ShallowClass.build("javax.tools.ToolProvider"); + + @Override + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + J.MethodInvocation m = method; + + if ( COMPILER_METHODMATCHER.matches(method, false)||(DOCUMENTATION_METHODMATCHER.matches(method, false)) ||(CLASSLOADER_METHODMATCHER.matches(method, false))) { + JavaType.Method transformedType = null; + if (method.getMethodType() != null) { + maybeRemoveImport(method.getMethodType().getDeclaringType()); + transformedType = method.getMethodType().withDeclaringType(classType); + if (!method.getMethodType().hasFlags(Flag.Static)) { + Set flags = new LinkedHashSet<>(method.getMethodType().getFlags()); + flags.add(Flag.Static); + transformedType = transformedType.withFlags(flags); + } + } + if (m.getSelect() == null) { + maybeAddImport("javax.tools.ToolProvider", m.getSimpleName(), true); + } else { + maybeAddImport("javax.tools.ToolProvider", true); + m = method.withSelect( + new J.Identifier(randomId(), + method.getSelect() == null ? + Space.EMPTY : + method.getSelect().getPrefix(), + Markers.EMPTY, + emptyList(), + classType.getClassName(), + classType, + null + ) + ); + } + m = m.withMethodType(transformedType) + .withName(m.getName().withType(transformedType)); + } + return m; + } + }; + } +} diff --git a/src/main/resources/META-INF/rewrite/java-version-17.yml b/src/main/resources/META-INF/rewrite/java-version-17.yml index 96a058213d..54832af753 100644 --- a/src/main/resources/META-INF/rewrite/java-version-17.yml +++ b/src/main/resources/META-INF/rewrite/java-version-17.yml @@ -54,6 +54,8 @@ recipeList: - org.openrewrite.java.migrate.SunNetSslPackageUnavailable - org.openrewrite.java.migrate.RemovedRMIConnectorServerCredentialTypesConstant - org.openrewrite.java.migrate.RemovedFileIOFinalizeMethods + - org.openrewrite.java.migrate.RemovedModifierAndConstantBootstrapsConstructors + - org.openrewrite.java.migrate.RemovedToolProviderConstructor --- type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.java.migrate.JavaVersion17 @@ -202,3 +204,4 @@ recipeList: methodPattern: "java.io.FileOutputStream finalize()" newMethodName: close ignoreDefinition: true + diff --git a/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java b/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java new file mode 100644 index 0000000000..8ecb50eea9 --- /dev/null +++ b/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java @@ -0,0 +1,73 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.java.migrate; + +import org.junit.jupiter.api.Test; +import org.openrewrite.config.Environment; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.java.Assertions.java; + +public class RemovedToolProviderConstructorTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec.expectedCyclesThatMakeChanges(2).recipe(new RemovedToolProviderConstructor()); + } + @Test + void moveToStaticTest() { + rewriteRun( + //language=java + java( + """ + package com.test; + + import javax.tools.ToolProvider; + + public class RemovedToolProviderConstructorApp { + + public void test() throws Exception { + ToolProvider tp = null; + tp.getSystemJavaCompiler(); + tp.getSystemDocumentationTool(); + tp.getSystemToolClassLoader(); + System.out.println(ToolProvider.getSystemJavaCompiler()); + } + } + """, + """ + package com.test; + + import javax.tools.ToolProvider; + + public class RemovedToolProviderConstructorApp { + + public void test() throws Exception { + ToolProvider tp = null; + ToolProvider.getSystemJavaCompiler(); + ToolProvider.getSystemDocumentationTool(); + ToolProvider.getSystemToolClassLoader(); + System.out.println(ToolProvider.getSystemJavaCompiler()); + } + } + """ + ) + ); + } + + +} From 057660ff775b673329e9b6563400ca296e73d07d Mon Sep 17 00:00:00 2001 From: anuram Date: Fri, 5 Apr 2024 09:55:55 -0400 Subject: [PATCH 02/15] removing extra recipe reference to modifier_bootstrap recipe --- src/main/resources/META-INF/rewrite/java-version-17.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/resources/META-INF/rewrite/java-version-17.yml b/src/main/resources/META-INF/rewrite/java-version-17.yml index 54832af753..d59ce7b461 100644 --- a/src/main/resources/META-INF/rewrite/java-version-17.yml +++ b/src/main/resources/META-INF/rewrite/java-version-17.yml @@ -54,7 +54,6 @@ recipeList: - org.openrewrite.java.migrate.SunNetSslPackageUnavailable - org.openrewrite.java.migrate.RemovedRMIConnectorServerCredentialTypesConstant - org.openrewrite.java.migrate.RemovedFileIOFinalizeMethods - - org.openrewrite.java.migrate.RemovedModifierAndConstantBootstrapsConstructors - org.openrewrite.java.migrate.RemovedToolProviderConstructor --- type: specs.openrewrite.org/v1beta/recipe From 21718ae754e75af3b1f6dea5539ef440e866a8ed Mon Sep 17 00:00:00 2001 From: Anu Ramamoorthy <93149514+ranuradh@users.noreply.github.com> Date: Fri, 5 Apr 2024 09:59:02 -0400 Subject: [PATCH 03/15] Update src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../java/migrate/RemovedToolProviderConstructorTest.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java b/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java index 8ecb50eea9..aa52070bee 100644 --- a/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java +++ b/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java @@ -16,7 +16,10 @@ package org.openrewrite.java.migrate; import org.junit.jupiter.api.Test; -import org.openrewrite.config.Environment; +import org.openrewrite.DocumentExample; +class RemovedToolProviderConstructorTest implements RewriteTest { + + @DocumentExample import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; From dbae944ed9484d1086fa73c99fa9eb24cde2a61c Mon Sep 17 00:00:00 2001 From: anuram Date: Fri, 5 Apr 2024 10:05:10 -0400 Subject: [PATCH 04/15] push some small test formatting fix --- .../RemovedToolProviderConstructorTest.java | 96 +++++++++---------- 1 file changed, 45 insertions(+), 51 deletions(-) diff --git a/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java b/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java index aa52070bee..6abd6f251d 100644 --- a/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java +++ b/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java @@ -17,60 +17,54 @@ import org.junit.jupiter.api.Test; import org.openrewrite.DocumentExample; -class RemovedToolProviderConstructorTest implements RewriteTest { - - @DocumentExample import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; - import static org.openrewrite.java.Assertions.java; -public class RemovedToolProviderConstructorTest implements RewriteTest { +class RemovedToolProviderConstructorTest implements RewriteTest { - @Override - public void defaults(RecipeSpec spec) { + @Override + public void defaults(RecipeSpec spec) { spec.expectedCyclesThatMakeChanges(2).recipe(new RemovedToolProviderConstructor()); - } - @Test - void moveToStaticTest() { - rewriteRun( - //language=java - java( - """ - package com.test; - - import javax.tools.ToolProvider; - - public class RemovedToolProviderConstructorApp { - - public void test() throws Exception { - ToolProvider tp = null; - tp.getSystemJavaCompiler(); - tp.getSystemDocumentationTool(); - tp.getSystemToolClassLoader(); - System.out.println(ToolProvider.getSystemJavaCompiler()); - } - } - """, - """ - package com.test; - - import javax.tools.ToolProvider; - - public class RemovedToolProviderConstructorApp { - - public void test() throws Exception { - ToolProvider tp = null; - ToolProvider.getSystemJavaCompiler(); - ToolProvider.getSystemDocumentationTool(); - ToolProvider.getSystemToolClassLoader(); - System.out.println(ToolProvider.getSystemJavaCompiler()); - } - } - """ - ) - ); - } - - -} + } + @Test + void moveToStaticTest() { + rewriteRun( + //language=java + java( + """ + package com.test; + + import javax.tools.ToolProvider; + + public class RemovedToolProviderConstructorApp { + + public void test() throws Exception { + ToolProvider tp = null; + tp.getSystemJavaCompiler(); + tp.getSystemDocumentationTool(); + tp.getSystemToolClassLoader(); + System.out.println(ToolProvider.getSystemJavaCompiler()); + } + } + """, + """ + package com.test; + + import javax.tools.ToolProvider; + + public class RemovedToolProviderConstructorApp { + + public void test() throws Exception { + ToolProvider tp = null; + ToolProvider.getSystemJavaCompiler(); + ToolProvider.getSystemDocumentationTool(); + ToolProvider.getSystemToolClassLoader(); + System.out.println(ToolProvider.getSystemJavaCompiler()); + } + } + """ + ) + ); + } +} \ No newline at end of file From 5d1959e37708eaeb49abf4ed0e1c89e2b00f0a1f Mon Sep 17 00:00:00 2001 From: anuram Date: Fri, 5 Apr 2024 10:10:06 -0400 Subject: [PATCH 05/15] removed extra import --- .../java/migrate/RemovedToolProviderConstructorTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java b/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java index 6abd6f251d..5ff96da9e1 100644 --- a/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java +++ b/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java @@ -16,7 +16,6 @@ package org.openrewrite.java.migrate; import org.junit.jupiter.api.Test; -import org.openrewrite.DocumentExample; import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; import static org.openrewrite.java.Assertions.java; From e1b1fbc8c9e552f70ab7ad5a8e0c7b3bed04204d Mon Sep 17 00:00:00 2001 From: anuram Date: Fri, 5 Apr 2024 10:16:32 -0400 Subject: [PATCH 06/15] added description + displayName --- .../java/migrate/RemovedToolProviderConstructor.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/RemovedToolProviderConstructor.java b/src/main/java/org/openrewrite/java/migrate/RemovedToolProviderConstructor.java index bb08187840..7c69b0a8e0 100644 --- a/src/main/java/org/openrewrite/java/migrate/RemovedToolProviderConstructor.java +++ b/src/main/java/org/openrewrite/java/migrate/RemovedToolProviderConstructor.java @@ -36,12 +36,14 @@ public class RemovedToolProviderConstructor extends Recipe { @Override public String getDisplayName() { - return "TBD"; + return "Converts method invocations to `javax.tools.ToolProvider()` to static calls "; } @Override public String getDescription() { - return "TBD."; + return "The `javax.tools.ToolProvider()` constructor has been removed in Java SE 16 since the class only contains Static methods." + + "The recipe converts javax.tools.ToolProvider getSystemJavaCompiler(), javax.tools.ToolProvider getSystemDocumentationTool() and javax.tools.ToolProvider getSystemToolClassLoader() to static methods"; + } @Override From cd6104cf916a8606984aa58bc9845667f6b05644 Mon Sep 17 00:00:00 2001 From: Anu Ramamoorthy <93149514+ranuradh@users.noreply.github.com> Date: Fri, 5 Apr 2024 10:18:26 -0400 Subject: [PATCH 07/15] Update src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../java/migrate/RemovedToolProviderConstructorTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java b/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java index 5ff96da9e1..30bad7b43d 100644 --- a/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java +++ b/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java @@ -16,6 +16,8 @@ package org.openrewrite.java.migrate; import org.junit.jupiter.api.Test; +import org.openrewrite.DocumentExample; + import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; import static org.openrewrite.java.Assertions.java; From a983d8f87f0eaa9d9777395108af1cda8cbaade4 Mon Sep 17 00:00:00 2001 From: Anu Ramamoorthy <93149514+ranuradh@users.noreply.github.com> Date: Fri, 5 Apr 2024 10:18:33 -0400 Subject: [PATCH 08/15] Update src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../java/migrate/RemovedToolProviderConstructorTest.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java b/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java index 30bad7b43d..9b6e280201 100644 --- a/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java +++ b/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java @@ -28,8 +28,10 @@ class RemovedToolProviderConstructorTest implements RewriteTest { public void defaults(RecipeSpec spec) { spec.expectedCyclesThatMakeChanges(2).recipe(new RemovedToolProviderConstructor()); } - @Test - void moveToStaticTest() { + + @DocumentExample + @Test + void moveToStaticTest() { rewriteRun( //language=java java( From b31e339bebc433c011d7d7c7760e6a991fffa0e1 Mon Sep 17 00:00:00 2001 From: Anu Ramamoorthy <93149514+ranuradh@users.noreply.github.com> Date: Fri, 5 Apr 2024 10:23:00 -0400 Subject: [PATCH 09/15] Update src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../java/migrate/RemovedToolProviderConstructorTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java b/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java index 9b6e280201..2fcd3dbddf 100644 --- a/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java +++ b/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java @@ -17,7 +17,6 @@ import org.junit.jupiter.api.Test; import org.openrewrite.DocumentExample; - import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; import static org.openrewrite.java.Assertions.java; From 858420943da6e5ca528d09509f81f46b33645b51 Mon Sep 17 00:00:00 2001 From: anuram Date: Fri, 5 Apr 2024 10:35:49 -0400 Subject: [PATCH 10/15] added . to description --- .../java/migrate/RemovedToolProviderConstructor.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/RemovedToolProviderConstructor.java b/src/main/java/org/openrewrite/java/migrate/RemovedToolProviderConstructor.java index 7c69b0a8e0..bc23dae000 100644 --- a/src/main/java/org/openrewrite/java/migrate/RemovedToolProviderConstructor.java +++ b/src/main/java/org/openrewrite/java/migrate/RemovedToolProviderConstructor.java @@ -36,13 +36,13 @@ public class RemovedToolProviderConstructor extends Recipe { @Override public String getDisplayName() { - return "Converts method invocations to `javax.tools.ToolProvider()` to static calls "; + return "Converts method invocations to `javax.tools.ToolProvider()` to static calls"; } @Override public String getDescription() { return "The `javax.tools.ToolProvider()` constructor has been removed in Java SE 16 since the class only contains Static methods." + - "The recipe converts javax.tools.ToolProvider getSystemJavaCompiler(), javax.tools.ToolProvider getSystemDocumentationTool() and javax.tools.ToolProvider getSystemToolClassLoader() to static methods"; + "The recipe converts javax.tools.ToolProvider getSystemJavaCompiler(), javax.tools.ToolProvider getSystemDocumentationTool() and javax.tools.ToolProvider getSystemToolClassLoader() to static methods."; } From ce771ff27046e9d0df1b848bd90eba925d195bd5 Mon Sep 17 00:00:00 2001 From: anuram Date: Fri, 5 Apr 2024 10:41:52 -0400 Subject: [PATCH 11/15] fixed formatting in test --- .../java/migrate/RemovedToolProviderConstructorTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java b/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java index 2fcd3dbddf..4d2eb05146 100644 --- a/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java +++ b/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java @@ -23,10 +23,10 @@ class RemovedToolProviderConstructorTest implements RewriteTest { - @Override - public void defaults(RecipeSpec spec) { + @Override + public void defaults(RecipeSpec spec) { spec.expectedCyclesThatMakeChanges(2).recipe(new RemovedToolProviderConstructor()); - } + } @DocumentExample @Test From e71c215f432b4a452cf35b43d3d62db5d8fbcd69 Mon Sep 17 00:00:00 2001 From: anuram Date: Fri, 5 Apr 2024 14:57:03 -0400 Subject: [PATCH 12/15] updating to add variable and check type --- .../RemovedToolProviderConstructor.java | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/RemovedToolProviderConstructor.java b/src/main/java/org/openrewrite/java/migrate/RemovedToolProviderConstructor.java index bc23dae000..2c1c80f582 100644 --- a/src/main/java/org/openrewrite/java/migrate/RemovedToolProviderConstructor.java +++ b/src/main/java/org/openrewrite/java/migrate/RemovedToolProviderConstructor.java @@ -19,11 +19,7 @@ import org.openrewrite.Recipe; import org.openrewrite.TreeVisitor; import org.openrewrite.java.JavaVisitor; -import org.openrewrite.java.MethodMatcher; -import org.openrewrite.java.tree.Flag; -import org.openrewrite.java.tree.J; -import org.openrewrite.java.tree.JavaType; -import org.openrewrite.java.tree.Space; +import org.openrewrite.java.tree.*; import org.openrewrite.marker.Markers; import java.util.LinkedHashSet; @@ -49,22 +45,23 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { return new JavaVisitor() { - private final MethodMatcher COMPILER_METHODMATCHER = new MethodMatcher("javax.tools.ToolProvider getSystemJavaCompiler()", false); - private final MethodMatcher DOCUMENTATION_METHODMATCHER = new MethodMatcher("javax.tools.ToolProvider getSystemDocumentationTool()", false); - private final MethodMatcher CLASSLOADER_METHODMATCHER = new MethodMatcher("javax.tools.ToolProvider getSystemToolClassLoader()", false); + private final JavaType.FullyQualified classType = JavaType.ShallowClass.build("javax.tools.ToolProvider"); + private final String TOOLPROVIDER_CLASS_TYPE = "javax.tools.ToolProvider"; @Override public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { - J.MethodInvocation m = method; - - if ( COMPILER_METHODMATCHER.matches(method, false)||(DOCUMENTATION_METHODMATCHER.matches(method, false)) ||(CLASSLOADER_METHODMATCHER.matches(method, false))) { + J.MethodInvocation m = method; + JavaType.Method methodType = method.getMethodType(); + boolean isSameReceiverType = method.getSelect() != null && + TypeUtils.isOfClassType(method.getSelect().getType(), TOOLPROVIDER_CLASS_TYPE); + if (isSameReceiverType) { JavaType.Method transformedType = null; - if (method.getMethodType() != null) { - maybeRemoveImport(method.getMethodType().getDeclaringType()); - transformedType = method.getMethodType().withDeclaringType(classType); - if (!method.getMethodType().hasFlags(Flag.Static)) { - Set flags = new LinkedHashSet<>(method.getMethodType().getFlags()); + if (methodType != null) { + maybeRemoveImport(methodType.getDeclaringType()); + transformedType = methodType.withDeclaringType(classType); + if (!methodType.hasFlags(Flag.Static)) { + Set flags = new LinkedHashSet<>(methodType.getFlags()); flags.add(Flag.Static); transformedType = transformedType.withFlags(flags); } From 32f80cc0b56e23d84f9c3053dd2c721896fae723 Mon Sep 17 00:00:00 2001 From: anuram Date: Fri, 5 Apr 2024 15:09:41 -0400 Subject: [PATCH 13/15] adding updates to test --- .../RemovedToolProviderConstructorTest.java | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java b/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java index 4d2eb05146..ee6929e097 100644 --- a/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java +++ b/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java @@ -38,15 +38,18 @@ void moveToStaticTest() { package com.test; import javax.tools.ToolProvider; - + import javax.tools.JavaCompiler; + import javax.tools.DocumentationTool; + import java.lang.ClassLoader; + public class RemovedToolProviderConstructorApp { public void test() throws Exception { ToolProvider tp = null; - tp.getSystemJavaCompiler(); - tp.getSystemDocumentationTool(); - tp.getSystemToolClassLoader(); - System.out.println(ToolProvider.getSystemJavaCompiler()); + JavaCompiler compiler = tp.getSystemJavaCompiler(); + DocumentationTool dT = tp.getSystemDocumentationTool(); + ClassLoader cl = tp.getSystemToolClassLoader(); + System.out.println(ToolProvider.getSystemJavaCompiler()); } } """, @@ -54,15 +57,18 @@ public void test() throws Exception { package com.test; import javax.tools.ToolProvider; + import javax.tools.JavaCompiler; + import javax.tools.DocumentationTool; + import java.lang.ClassLoader; public class RemovedToolProviderConstructorApp { public void test() throws Exception { ToolProvider tp = null; - ToolProvider.getSystemJavaCompiler(); - ToolProvider.getSystemDocumentationTool(); - ToolProvider.getSystemToolClassLoader(); - System.out.println(ToolProvider.getSystemJavaCompiler()); + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + DocumentationTool dT = ToolProvider.getSystemDocumentationTool(); + ClassLoader cl = ToolProvider.getSystemToolClassLoader(); + System.out.println(ToolProvider.getSystemJavaCompiler()); } } """ From 57f5df5b44518b115a92d7c74eb58b2536e63562 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Tue, 23 Apr 2024 10:51:04 +0200 Subject: [PATCH 14/15] Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../java/migrate/RemovedToolProviderConstructorTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java b/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java index ee6929e097..99e52acc85 100644 --- a/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java +++ b/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java @@ -19,6 +19,7 @@ import org.openrewrite.DocumentExample; import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; + import static org.openrewrite.java.Assertions.java; class RemovedToolProviderConstructorTest implements RewriteTest { From 11e284bd0dba6036b34a2e43769877ac4a542c25 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Mon, 6 May 2024 16:29:04 +0200 Subject: [PATCH 15/15] Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../java/migrate/RemovedToolProviderConstructorTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java b/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java index 99e52acc85..65674a01bf 100644 --- a/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java +++ b/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java @@ -76,4 +76,4 @@ public void test() throws Exception { ) ); } -} \ No newline at end of file +}