From f5038eac42a30d261df62d58d950e8e911f44442 Mon Sep 17 00:00:00 2001 From: anuram Date: Fri, 17 May 2024 09:22:58 -0400 Subject: [PATCH 1/3] adding recipe RemovedToolProvider --- .../RemovedToolProviderConstructor.java | 96 +++++++++++++++++++ .../META-INF/rewrite/java-version-17.yml | 9 +- .../RemovedToolProviderConstructorTest.java | 81 ++++++++++++++++ 3 files changed, 182 insertions(+), 4 deletions(-) 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..12fdcdc7b3 --- /dev/null +++ b/src/main/java/org/openrewrite/java/migrate/RemovedToolProviderConstructor.java @@ -0,0 +1,96 @@ +/* + * 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.*; +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 "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."; + + } + + @Override + public TreeVisitor getVisitor() { + + return new JavaVisitor() { + + 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; + 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 (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); + } + } + 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 (J.MethodInvocation) super.visitMethodInvocation(m, ctx); + } + }; + + } +} 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 1d4df01fb5..ca5123be19 100644 --- a/src/main/resources/META-INF/rewrite/java-version-17.yml +++ b/src/main/resources/META-INF/rewrite/java-version-17.yml @@ -56,6 +56,7 @@ recipeList: - org.openrewrite.java.migrate.SunNetSslPackageUnavailable - org.openrewrite.java.migrate.RemovedRMIConnectorServerCredentialTypesConstant - org.openrewrite.java.migrate.RemovedFileIOFinalizeMethods + - org.openrewrite.java.migrate.RemovedToolProviderConstructor --- type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.java.migrate.DeprecatedJavaxSecurityCert @@ -181,14 +182,14 @@ type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.java.migrate.DeprecatedCountStackFramesMethod displayName: Remove `Thread.countStackFrames()` method description: > - `Thread.countStackFrames()` has been removed in Java SE 14 and has been changed in this release to unconditionally throw `UnsupportedOperationException` - This recipe removes the usage of this method in your application as long as the method is not assigned to a variable. - For more information on the Java SE 14 deprecation of this method, see https://bugs.java.com/bugdatabase/view_bug?bug_id=8205132. + `Thread.countStackFrames()` has been removed in Java SE 14 and has been changed in this release to unconditionally throw `UnsupportedOperationException` + This recipe removes the usage of this method in your application as long as the method is not assigned to a variable. + For more information on the Java SE 14 deprecation of this method, see https://bugs.java.com/bugdatabase/view_bug?bug_id=8205132. tags: - java17 recipeList: - org.openrewrite.java.migrate.RemoveMethodInvocation: - methodPattern: 'java.lang.Thread countStackFrames()' + methodPattern: 'java.lang.Thread countStackFrames()' --- type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.java.migrate.RemovedFileIOFinalizeMethods 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..f15119a100 --- /dev/null +++ b/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java @@ -0,0 +1,81 @@ +/* + * 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.DocumentExample; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.java.Assertions.java; + +class RemovedToolProviderConstructorTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec.expectedCyclesThatMakeChanges(2).recipe(new RemovedToolProviderConstructor()); + } + + @DocumentExample + @Test + void moveToStaticTest() { + rewriteRun( + //language=java + java( + """ + 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; + JavaCompiler compiler = tp.getSystemJavaCompiler(); + DocumentationTool dT = tp.getSystemDocumentationTool(); + ClassLoader cl = tp.getSystemToolClassLoader(); + System.out.println(ToolProvider.getSystemJavaCompiler()); + ToolProvider.getSystemJavaCompiler().getSourceVersions(); + } + } + """, + """ + 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; + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + DocumentationTool dT = ToolProvider.getSystemDocumentationTool(); + ClassLoader cl = ToolProvider.getSystemToolClassLoader(); + System.out.println(ToolProvider.getSystemJavaCompiler()); + ToolProvider.getSystemJavaCompiler().getSourceVersions(); + } + } + """ + ) + ); + } +} From 9805467b1fd02a29b451236d1b73a38b064f264c Mon Sep 17 00:00:00 2001 From: anuram Date: Fri, 17 May 2024 09:28:24 -0400 Subject: [PATCH 2/3] updated tests --- .../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 f15119a100..4e24ec9d56 100644 --- a/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java +++ b/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java @@ -51,7 +51,7 @@ public void test() throws Exception { DocumentationTool dT = tp.getSystemDocumentationTool(); ClassLoader cl = tp.getSystemToolClassLoader(); System.out.println(ToolProvider.getSystemJavaCompiler()); - ToolProvider.getSystemJavaCompiler().getSourceVersions(); + tp.getSystemJavaCompiler().getSourceVersions(); } } """, From 4e459f56c39470818327a2b9ad089cddd2e27f53 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Mon, 20 May 2024 01:11:57 +0200 Subject: [PATCH 3/3] Implement RemovedToolProviderConstructor as declarative recipe --- .../RemovedToolProviderConstructor.java | 96 ------------------- .../META-INF/rewrite/java-version-17.yml | 13 ++- .../RemovedToolProviderConstructorTest.java | 18 ++-- 3 files changed, 18 insertions(+), 109 deletions(-) delete mode 100644 src/main/java/org/openrewrite/java/migrate/RemovedToolProviderConstructor.java diff --git a/src/main/java/org/openrewrite/java/migrate/RemovedToolProviderConstructor.java b/src/main/java/org/openrewrite/java/migrate/RemovedToolProviderConstructor.java deleted file mode 100644 index 12fdcdc7b3..0000000000 --- a/src/main/java/org/openrewrite/java/migrate/RemovedToolProviderConstructor.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * 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.*; -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 "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."; - - } - - @Override - public TreeVisitor getVisitor() { - - return new JavaVisitor() { - - 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; - 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 (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); - } - } - 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 (J.MethodInvocation) super.visitMethodInvocation(m, ctx); - } - }; - - } -} 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 6d140bd114..a1821791d3 100644 --- a/src/main/resources/META-INF/rewrite/java-version-17.yml +++ b/src/main/resources/META-INF/rewrite/java-version-17.yml @@ -40,6 +40,7 @@ recipeList: methodPattern: java.lang.Runtime traceInstructions(boolean) - org.openrewrite.java.migrate.RemoveMethodInvocation: methodPattern: java.lang.System traceMethodCalls(boolean) + - org.openrewrite.java.migrate.RemovedToolProviderConstructor - org.openrewrite.java.migrate.lang.UseTextBlocks - org.openrewrite.java.migrate.DeprecatedJavaxSecurityCert - org.openrewrite.java.migrate.DeprecatedLogRecordThreadID @@ -59,7 +60,6 @@ recipeList: - org.openrewrite.java.migrate.SunNetSslPackageUnavailable - org.openrewrite.java.migrate.RemovedRMIConnectorServerCredentialTypesConstant - org.openrewrite.java.migrate.RemovedFileIOFinalizeMethods - - org.openrewrite.java.migrate.RemovedToolProviderConstructor --- type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.java.migrate.DeprecatedJavaxSecurityCert @@ -210,3 +210,14 @@ recipeList: methodPattern: "java.io.FileOutputStream finalize()" newMethodName: close ignoreDefinition: true +--- +type: specs.openrewrite.org/v1beta/recipe +name: org.openrewrite.java.migrate.RemovedToolProviderConstructor +displayName: Change `javax.tools.ToolProvider` methods calls to static +description: > + 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. +recipeList: + - org.openrewrite.java.ChangeMethodTargetToStatic: + methodPattern: javax.tools.ToolProvider *() + fullyQualifiedTargetTypeName: javax.tools.ToolProvider diff --git a/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java b/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java index 4e24ec9d56..d359d61f6d 100644 --- a/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java +++ b/src/test/java/org/openrewrite/java/migrate/RemovedToolProviderConstructorTest.java @@ -26,7 +26,7 @@ class RemovedToolProviderConstructorTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { - spec.expectedCyclesThatMakeChanges(2).recipe(new RemovedToolProviderConstructor()); + spec.recipeFromResource("/META-INF/rewrite/java-version-17.yml", "org.openrewrite.java.migrate.RemovedToolProviderConstructor"); } @DocumentExample @@ -36,16 +36,13 @@ void moveToStaticTest() { //language=java java( """ - 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 { + class RemovedToolProviderConstructorApp { + void test() throws Exception { ToolProvider tp = null; JavaCompiler compiler = tp.getSystemJavaCompiler(); DocumentationTool dT = tp.getSystemDocumentationTool(); @@ -56,16 +53,13 @@ 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 { + + class RemovedToolProviderConstructorApp { + void test() throws Exception { ToolProvider tp = null; JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); DocumentationTool dT = ToolProvider.getSystemDocumentationTool();