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

remove thread destroy method recipe #246

Merged
merged 2 commits into from
Jul 6, 2023
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,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(){
}
}
"""
)
);
}
}