-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove thread destroy method recipe (#246)
* remove thread destroy method recipe * Fixed recipe based on review comments
- Loading branch information
1 parent
927b01d
commit 109976e
Showing
2 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
src/main/java/org/openrewrite/java/migrate/lang/RemoveThreadDestroyMethod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}); | ||
} | ||
|
||
} |
94 changes: 94 additions & 0 deletions
94
src/test/java/org/openrewrite/java/migrate/lang/RemoveThreadDestroyMethodTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(){ | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} |