Skip to content

Commit

Permalink
Recipe to call ToolProvider methods as static methods, not on instanc…
Browse files Browse the repository at this point in the history
…es (#477)

* adding recipe RemovedToolProvider

* updated tests

* Implement RemovedToolProviderConstructor as declarative recipe

---------

Co-authored-by: anuram <[email protected]>
Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
3 people authored May 21, 2024
1 parent 64f8669 commit 047c19f
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/main/resources/META-INF/rewrite/java-version-17.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,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
Expand Down Expand Up @@ -203,14 +204,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
Expand All @@ -228,3 +229,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
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.recipeFromResource("/META-INF/rewrite/java-version-17.yml", "org.openrewrite.java.migrate.RemovedToolProviderConstructor");
}

@DocumentExample
@Test
void moveToStaticTest() {
rewriteRun(
//language=java
java(
"""
import javax.tools.ToolProvider;
import javax.tools.JavaCompiler;
import javax.tools.DocumentationTool;
import java.lang.ClassLoader;
class RemovedToolProviderConstructorApp {
void test() throws Exception {
ToolProvider tp = null;
JavaCompiler compiler = tp.getSystemJavaCompiler();
DocumentationTool dT = tp.getSystemDocumentationTool();
ClassLoader cl = tp.getSystemToolClassLoader();
System.out.println(ToolProvider.getSystemJavaCompiler());
tp.getSystemJavaCompiler().getSourceVersions();
}
}
""",
"""
import javax.tools.ToolProvider;
import javax.tools.JavaCompiler;
import javax.tools.DocumentationTool;
import java.lang.ClassLoader;
class RemovedToolProviderConstructorApp {
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();
}
}
"""
)
);
}
}

0 comments on commit 047c19f

Please sign in to comment.