Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recipe to call ToolProvider methods as static methods, not on instances #477

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* 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.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<?, ExecutionContext> getVisitor() {

return new JavaVisitor<ExecutionContext>() {

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<Flag> 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);
}
};

}
}
9 changes: 5 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 @@ -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
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.migrate.DeprecatedJavaxSecurityCert
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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.expectedCyclesThatMakeChanges(2).recipe(new RemovedToolProviderConstructor());
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
}

@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());
tp.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();
}
}
"""
)
);
}
}