Skip to content

Commit

Permalink
RemoveUnusedPrivateMethods should ignore class annotated with `@Sup…
Browse files Browse the repository at this point in the history
…ressWarning("unused")` (#293)

* Fix #4188 org.openrewrite.staticanalysis.RemoveUnusedPrivateMethods issue. Added test

* Fix #4188 org.openrewrite.staticanalysis.RemoveUnusedPrivateMethods issue. Added solution

* Fix #4188 org.openrewrite.staticanalysis.RemoveUnusedPrivateMethods issue. Renamed tests

* Fix #4188 org.openrewrite.staticanalysis.RemoveUnusedPrivateMethods issue. Some changes

* Minor polish

* Fix imports

---------

Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
Dinozavvvr and timtebeek authored May 21, 2024
1 parent 9eca518 commit 5a391c1
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.NoMissingTypes;
import org.openrewrite.java.search.FindAnnotations;
import org.openrewrite.java.service.AnnotationService;
import org.openrewrite.java.tree.*;

import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.Set;

public class RemoveUnusedPrivateMethods extends Recipe {
Expand Down Expand Up @@ -53,8 +55,32 @@ public Duration getEstimatedEffortPerOccurrence() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new NoMissingTypes(), new JavaIsoVisitor<ExecutionContext>() {

@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDeclaration, ExecutionContext ctx) {
if (unusedWarningsSuppressed(classDeclaration)) {
return classDeclaration;
}
return super.visitClassDeclaration(classDeclaration, ctx);
}

private boolean unusedWarningsSuppressed(J classDeclaration) {
for (J.Annotation annotation : FindAnnotations.find(classDeclaration, "java.lang.SuppressWarnings")) {
List<Expression> arguments = annotation.getArguments();
if (arguments != null) {
for (Expression argument : arguments) {
if (J.Literal.isLiteralValue(argument, "unused")) {
return true;
}
}
}
}
return false;
}

@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method,
ExecutionContext ctx) {
J.MethodDeclaration m = super.visitMethodDeclaration(method, ctx);
JavaType.Method methodType = method.getMethodType();
if (methodType != null && methodType.hasFlags(Flag.Private) &&
Expand Down Expand Up @@ -104,4 +130,5 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,22 @@ void removeUnusedPrivateMethods() {
class Test {
private void unused() {
}
public void dontRemove() {
dontRemove2();
}
private void dontRemove2() {
}
}
""",
"""
class Test {
public void dontRemove() {
dontRemove2();
}
private void dontRemove2() {
}
}
Expand Down Expand Up @@ -127,4 +127,100 @@ private static <T> void checkMethodInUse(String arg0, T arg1) {
)
);
}

@Test
void removeMethodsOnNestedClass() {
rewriteRun(
//language=java
java(
"""
import java.util.stream.Stream;
class Test {
void test(String input) {
}
private Stream<Object> unused() {
return null;
}
class InnerTest {
void test(String input) {
}
private Stream<Object> unused() {
return null;
}
}
}
""",
"""
import java.util.stream.Stream;
class Test {
void test(String input) {
}
class InnerTest {
void test(String input) {
}
}
}
"""
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite/issues/4076")
void doNotRemoveMethodsWithUnusedSuppressWarningsOnClass() {
rewriteRun(
//language=java
java(
"""
import java.util.stream.Stream;
@SuppressWarnings("unused")
class Test {
void test(String input) {
}
private Stream<Object> unused() {
return null;
}
private Stream<Object> anotherUnused() {
return null;
}
}
"""
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite/issues/4076")
void doNotRemoveMethodsWithUnusedSuppressWarningsOnClassNestedClass() {
rewriteRun(
//language=java
java(
"""
import java.util.stream.Stream;
@SuppressWarnings("unused")
class Test {
void test(String input) {
}
private Stream<Object> unused() {
return null;
}
class InnerTest {
void test(String input) {
}
private Stream<Object> unused() {
return null;
}
}
}
"""
)
);
}
}

0 comments on commit 5a391c1

Please sign in to comment.