Skip to content

Commit 109976e

Browse files
authored
remove thread destroy method recipe (#246)
* remove thread destroy method recipe * Fixed recipe based on review comments
1 parent 927b01d commit 109976e

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.openrewrite.java.migrate.lang;
18+
19+
import lombok.EqualsAndHashCode;
20+
import lombok.Value;
21+
import org.openrewrite.ExecutionContext;
22+
import org.openrewrite.Preconditions;
23+
import org.openrewrite.Recipe;
24+
import org.openrewrite.TreeVisitor;
25+
import org.openrewrite.java.JavaIsoVisitor;
26+
import org.openrewrite.java.search.UsesType;
27+
import org.openrewrite.java.tree.J;
28+
import org.openrewrite.java.tree.TypeUtils;
29+
30+
import java.util.Collections;
31+
import java.util.Objects;
32+
import java.util.Set;
33+
34+
@Value
35+
@EqualsAndHashCode(callSuper = true)
36+
public class RemoveThreadDestroyMethod extends Recipe {
37+
38+
public static final String JAVA_LANG_THREAD = "java.lang.Thread";
39+
40+
@Override
41+
public String getDisplayName() {
42+
return "Remove deprecated `Thread.destroy()`";
43+
}
44+
45+
@Override
46+
public String getDescription() {
47+
return "Remove deprecated invocations of `Thread.destroy()` which have no alternatives needed.";
48+
}
49+
50+
@Override
51+
public Set<String> getTags() {
52+
return Collections.singleton("JDK-8204260");
53+
}
54+
55+
@Override
56+
public TreeVisitor<?, ExecutionContext> getVisitor() {
57+
58+
return Preconditions.check(new UsesType<>(JAVA_LANG_THREAD, false),
59+
new JavaIsoVisitor<ExecutionContext>() {
60+
@Override
61+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
62+
J.MethodInvocation mi = super.visitMethodInvocation(method, ctx);
63+
if (Objects.nonNull(mi.getSelect())
64+
&& TypeUtils.isAssignableTo(JAVA_LANG_THREAD, mi.getSelect().getType())
65+
&& mi.getSimpleName().equals("destroy"))
66+
return null;
67+
return mi;
68+
}
69+
});
70+
}
71+
72+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.openrewrite.java.migrate.lang;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.openrewrite.test.RecipeSpec;
21+
import org.openrewrite.test.RewriteTest;
22+
23+
import static org.openrewrite.java.Assertions.java;
24+
import static org.openrewrite.java.Assertions.javaVersion;
25+
26+
class RemoveThreadDestroyMethodTest implements RewriteTest {
27+
28+
@Override
29+
public void defaults(RecipeSpec spec) {
30+
spec.recipe(new RemoveThreadDestroyMethod());//.allSources(s -> s.markers(javaVersion(8)));
31+
}
32+
33+
@Test
34+
void removeDestroyCall() {
35+
//language=java
36+
rewriteRun(
37+
java(
38+
"""
39+
class FooBar{
40+
public void test() {
41+
Thread thread = Thread.currentThread();
42+
thread.setName("Main Thread");
43+
thread.destroy();
44+
}
45+
}
46+
""",
47+
"""
48+
class FooBar{
49+
public void test() {
50+
Thread thread = Thread.currentThread();
51+
thread.setName("Main Thread");
52+
}
53+
}
54+
"""
55+
)
56+
);
57+
}
58+
59+
@Test
60+
void noChangeWithoutDestroy() {
61+
//language=java
62+
rewriteRun(
63+
java(
64+
"""
65+
class FooBar{
66+
public void test() {
67+
Thread thread = Thread.currentThread();
68+
thread.setName("Main Thread");
69+
}
70+
}
71+
"""
72+
)
73+
);
74+
}
75+
76+
@Test
77+
void noChangeWithoutThreadDestroy() {
78+
//language=java
79+
rewriteRun(
80+
java(
81+
"""
82+
class FooBar{
83+
public void test() {
84+
FooBar f = new FooBar();
85+
f.destroy();
86+
}
87+
public void destroy(){
88+
}
89+
}
90+
"""
91+
)
92+
);
93+
}
94+
}

0 commit comments

Comments
 (0)