Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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,65 @@
/*
* Copyright 2023 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.lang;

import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.J;

import java.util.Collections;
import java.util.Set;

public class RemoveRuntimeTraceMethods extends Recipe {
private static final MethodMatcher TRACE_INSTRUCTIONS = new MethodMatcher("java.lang.Runtime traceInstructions(boolean)");
private static final MethodMatcher TRACE_METHOD_CALLS = new MethodMatcher("java.lang.Runtime traceMethodCalls(boolean)");

@Override
public String getDisplayName() {
return "Remove deprecated statements from Runtime module";
}

@Override
public String getDescription() {
return "Remove deprecated invocations of `Runtime.traceInstructions()` and `Runtime.traceMethodCalls()` which have no alternatives needed.";
}

@Override
public Set<String> getTags() {
return Collections.singleton("JDK-8225330");
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(Preconditions.or(
new UsesMethod<>(TRACE_INSTRUCTIONS),
new UsesMethod<>(TRACE_METHOD_CALLS)),
new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation mi = super.visitMethodInvocation(method, ctx);
if (TRACE_INSTRUCTIONS.matches(mi) || TRACE_METHOD_CALLS.matches(mi)) {
return null;
}
return mi;
}
});
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/rewrite/java-version-17.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ recipeList:
- org.openrewrite.github.SetupJavaUpgradeJavaVersion:
minimumJavaMajorVersion: 17
- org.openrewrite.staticanalysis.InstanceOfPatternMatch
- org.openrewrite.java.migrate.lang.RemoveRuntimeTraceMethods
- org.openrewrite.java.migrate.lang.UseTextBlocks
- org.openrewrite.java.migrate.lombok.LombokValueToRecord
- org.openrewrite.java.migrate.DeprecatedJavaxSecurityCert
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright 2023 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.lang;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;

@EnabledForJreRange(min = JRE.JAVA_8, max = JRE.JAVA_11)
class RemoveDeprecatedRuntimeTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new RemoveRuntimeTraceMethods());
}

@Test
void traceInstructionsRemove() {
//language=java
rewriteRun(
java(
"""
class FooBar{
void test(Runtime r) {
r.traceInstructions();
}
}
""",
"""
class FooBar{
void test(Runtime r) {
}
}
"""
)
);
}

@Test
void traceMethodCallsRemove() {
//language=java
rewriteRun(
java(
"""
class FooBar{
void test(Runtime r) {
r.traceMethodCalls();
}
}
""",
"""
class FooBar{
void test(Runtime r) {
}
}
"""
)
);
}

@Test
void noChange() {
//language=java
rewriteRun(
java(
"""
class FooBar{
void test(Runtime r) {
r.gc();
}
}
"""
)
);
}

@Test
void noChanges() {
//language=java
rewriteRun(
java(
"""
class FooBar{
public static void main(String[] args){
Runtime r = Runtime.getRuntime();
test(r);
}
public void test(Runtime r) {
r.gc();
}
}
"""
)
);
}
}