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

adding recipe RemovedToolProviderConstructor #454

Closed
Show file tree
Hide file tree
Changes from 13 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,93 @@
/*
* 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.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 m;
}
};
}
}
2 changes: 2 additions & 0 deletions src/main/resources/META-INF/rewrite/java-version-17.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,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.JavaVersion17
Expand Down Expand Up @@ -202,3 +203,4 @@ recipeList:
methodPattern: "java.io.FileOutputStream finalize()"
newMethodName: close
ignoreDefinition: true

Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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;
ranuradh marked this conversation as resolved.
Show resolved Hide resolved
import org.openrewrite.DocumentExample;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
ranuradh marked this conversation as resolved.
Show resolved Hide resolved
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
ranuradh marked this conversation as resolved.
Show resolved Hide resolved
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
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;

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();
Copy link
Contributor

@cjobinabo cjobinabo Apr 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to broaden this test case to capture more ways these calls can be make that better illustrate real world scenarios(for example take a look at how ToolProvider.getSystemJavaCompiler() is called here http://www.java2s.com/example/java-api/javax/tools/toolprovider/getsystemjavacompiler-0-8.html). I think some of the logic in your recipe can be stripped but we need better test coverage to confirm this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added in the next commit

ToolProvider.getSystemDocumentationTool();
ToolProvider.getSystemToolClassLoader();
System.out.println(ToolProvider.getSystemJavaCompiler());
}
}
"""
)
);
}
}
timtebeek marked this conversation as resolved.
Show resolved Hide resolved