Skip to content

Commit 047c19f

Browse files
ranuradhAnuRam123timtebeek
authored
Recipe to call ToolProvider methods as static methods, not on instances (#477)
* adding recipe RemovedToolProvider * updated tests * Implement RemovedToolProviderConstructor as declarative recipe --------- Co-authored-by: anuram <ranuradh@us.ibm.com> Co-authored-by: Tim te Beek <tim@moderne.io>
1 parent 64f8669 commit 047c19f

File tree

2 files changed

+91
-4
lines changed

2 files changed

+91
-4
lines changed

src/main/resources/META-INF/rewrite/java-version-17.yml

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ recipeList:
3434
methodPattern: java.lang.Runtime traceInstructions(boolean)
3535
- org.openrewrite.java.migrate.RemoveMethodInvocation:
3636
methodPattern: java.lang.System traceMethodCalls(boolean)
37+
- org.openrewrite.java.migrate.RemovedToolProviderConstructor
3738
- org.openrewrite.java.migrate.lang.UseTextBlocks
3839
- org.openrewrite.java.migrate.DeprecatedJavaxSecurityCert
3940
- org.openrewrite.java.migrate.DeprecatedLogRecordThreadID
@@ -203,14 +204,14 @@ type: specs.openrewrite.org/v1beta/recipe
203204
name: org.openrewrite.java.migrate.DeprecatedCountStackFramesMethod
204205
displayName: Remove `Thread.countStackFrames()` method
205206
description: >
206-
`Thread.countStackFrames()` has been removed in Java SE 14 and has been changed in this release to unconditionally throw `UnsupportedOperationException`
207-
This recipe removes the usage of this method in your application as long as the method is not assigned to a variable.
208-
For more information on the Java SE 14 deprecation of this method, see https://bugs.java.com/bugdatabase/view_bug?bug_id=8205132.
207+
`Thread.countStackFrames()` has been removed in Java SE 14 and has been changed in this release to unconditionally throw `UnsupportedOperationException`
208+
This recipe removes the usage of this method in your application as long as the method is not assigned to a variable.
209+
For more information on the Java SE 14 deprecation of this method, see https://bugs.java.com/bugdatabase/view_bug?bug_id=8205132.
209210
tags:
210211
- java17
211212
recipeList:
212213
- org.openrewrite.java.migrate.RemoveMethodInvocation:
213-
methodPattern: 'java.lang.Thread countStackFrames()'
214+
methodPattern: 'java.lang.Thread countStackFrames()'
214215
---
215216
type: specs.openrewrite.org/v1beta/recipe
216217
name: org.openrewrite.java.migrate.RemovedFileIOFinalizeMethods
@@ -228,3 +229,14 @@ recipeList:
228229
methodPattern: "java.io.FileOutputStream finalize()"
229230
newMethodName: close
230231
ignoreDefinition: true
232+
---
233+
type: specs.openrewrite.org/v1beta/recipe
234+
name: org.openrewrite.java.migrate.RemovedToolProviderConstructor
235+
displayName: Change `javax.tools.ToolProvider` methods calls to static
236+
description: >
237+
The `javax.tools.ToolProvider()` constructor has been removed in Java SE 16 since the class only contains static methods.
238+
The recipe converts `javax.tools.ToolProvider getSystemJavaCompiler()`, `javax.tools.ToolProvider getSystemDocumentationTool()` and `javax.tools.ToolProvider getSystemToolClassLoader()` to static methods.
239+
recipeList:
240+
- org.openrewrite.java.ChangeMethodTargetToStatic:
241+
methodPattern: javax.tools.ToolProvider *()
242+
fullyQualifiedTargetTypeName: javax.tools.ToolProvider
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.migrate;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.DocumentExample;
20+
import org.openrewrite.test.RecipeSpec;
21+
import org.openrewrite.test.RewriteTest;
22+
23+
import static org.openrewrite.java.Assertions.java;
24+
25+
class RemovedToolProviderConstructorTest implements RewriteTest {
26+
27+
@Override
28+
public void defaults(RecipeSpec spec) {
29+
spec.recipeFromResource("/META-INF/rewrite/java-version-17.yml", "org.openrewrite.java.migrate.RemovedToolProviderConstructor");
30+
}
31+
32+
@DocumentExample
33+
@Test
34+
void moveToStaticTest() {
35+
rewriteRun(
36+
//language=java
37+
java(
38+
"""
39+
import javax.tools.ToolProvider;
40+
import javax.tools.JavaCompiler;
41+
import javax.tools.DocumentationTool;
42+
import java.lang.ClassLoader;
43+
44+
class RemovedToolProviderConstructorApp {
45+
void test() throws Exception {
46+
ToolProvider tp = null;
47+
JavaCompiler compiler = tp.getSystemJavaCompiler();
48+
DocumentationTool dT = tp.getSystemDocumentationTool();
49+
ClassLoader cl = tp.getSystemToolClassLoader();
50+
System.out.println(ToolProvider.getSystemJavaCompiler());
51+
tp.getSystemJavaCompiler().getSourceVersions();
52+
}
53+
}
54+
""",
55+
"""
56+
import javax.tools.ToolProvider;
57+
import javax.tools.JavaCompiler;
58+
import javax.tools.DocumentationTool;
59+
import java.lang.ClassLoader;
60+
61+
class RemovedToolProviderConstructorApp {
62+
void test() throws Exception {
63+
ToolProvider tp = null;
64+
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
65+
DocumentationTool dT = ToolProvider.getSystemDocumentationTool();
66+
ClassLoader cl = ToolProvider.getSystemToolClassLoader();
67+
System.out.println(ToolProvider.getSystemJavaCompiler());
68+
ToolProvider.getSystemJavaCompiler().getSourceVersions();
69+
}
70+
}
71+
"""
72+
)
73+
);
74+
}
75+
}

0 commit comments

Comments
 (0)