Skip to content

Commit

Permalink
remove thread destroy method recipe (#246)
Browse files Browse the repository at this point in the history
* remove thread destroy method recipe

* Fixed recipe based on review comments
  • Loading branch information
satvika-eda authored Jul 6, 2023
1 parent 927b01d commit 109976e
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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 lombok.EqualsAndHashCode;
import lombok.Value;
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.search.UsesType;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.TypeUtils;

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

@Value
@EqualsAndHashCode(callSuper = true)
public class RemoveThreadDestroyMethod extends Recipe {

public static final String JAVA_LANG_THREAD = "java.lang.Thread";

@Override
public String getDisplayName() {
return "Remove deprecated `Thread.destroy()`";
}

@Override
public String getDescription() {
return "Remove deprecated invocations of `Thread.destroy()` which have no alternatives needed.";
}

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

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {

return Preconditions.check(new UsesType<>(JAVA_LANG_THREAD, false),
new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation mi = super.visitMethodInvocation(method, ctx);
if (Objects.nonNull(mi.getSelect())
&& TypeUtils.isAssignableTo(JAVA_LANG_THREAD, mi.getSelect().getType())
&& mi.getSimpleName().equals("destroy"))
return null;
return mi;
}
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

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

class RemoveThreadDestroyMethodTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new RemoveThreadDestroyMethod());//.allSources(s -> s.markers(javaVersion(8)));
}

@Test
void removeDestroyCall() {
//language=java
rewriteRun(
java(
"""
class FooBar{
public void test() {
Thread thread = Thread.currentThread();
thread.setName("Main Thread");
thread.destroy();
}
}
""",
"""
class FooBar{
public void test() {
Thread thread = Thread.currentThread();
thread.setName("Main Thread");
}
}
"""
)
);
}

@Test
void noChangeWithoutDestroy() {
//language=java
rewriteRun(
java(
"""
class FooBar{
public void test() {
Thread thread = Thread.currentThread();
thread.setName("Main Thread");
}
}
"""
)
);
}

@Test
void noChangeWithoutThreadDestroy() {
//language=java
rewriteRun(
java(
"""
class FooBar{
public void test() {
FooBar f = new FooBar();
f.destroy();
}
public void destroy(){
}
}
"""
)
);
}
}

0 comments on commit 109976e

Please sign in to comment.