From 5a391c1c77320128e50ce92a81688fc663930734 Mon Sep 17 00:00:00 2001 From: Dinar Shagaliev <45518871+Dinozavvvr@users.noreply.github.com> Date: Tue, 21 May 2024 13:50:00 +0300 Subject: [PATCH] `RemoveUnusedPrivateMethods` should ignore class annotated with `@SupressWarning("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 --- .../RemoveUnusedPrivateMethods.java | 29 ++++- .../RemoveUnusedPrivateMethodsTest.java | 104 +++++++++++++++++- 2 files changed, 128 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/openrewrite/staticanalysis/RemoveUnusedPrivateMethods.java b/src/main/java/org/openrewrite/staticanalysis/RemoveUnusedPrivateMethods.java index de4cf3090..4b2da88f4 100644 --- a/src/main/java/org/openrewrite/staticanalysis/RemoveUnusedPrivateMethods.java +++ b/src/main/java/org/openrewrite/staticanalysis/RemoveUnusedPrivateMethods.java @@ -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 { @@ -53,8 +55,32 @@ public Duration getEstimatedEffortPerOccurrence() { @Override public TreeVisitor getVisitor() { return Preconditions.check(new NoMissingTypes(), new JavaIsoVisitor() { + @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 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) && @@ -104,4 +130,5 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex } }); } + } diff --git a/src/test/java/org/openrewrite/staticanalysis/RemoveUnusedPrivateMethodsTest.java b/src/test/java/org/openrewrite/staticanalysis/RemoveUnusedPrivateMethodsTest.java index 8e076ce8b..35ff9360c 100644 --- a/src/test/java/org/openrewrite/staticanalysis/RemoveUnusedPrivateMethodsTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/RemoveUnusedPrivateMethodsTest.java @@ -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() { } } @@ -127,4 +127,100 @@ private static 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 unused() { + return null; + } + + class InnerTest { + void test(String input) { + } + private Stream 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 unused() { + return null; + } + private Stream 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 unused() { + return null; + } + + class InnerTest { + void test(String input) { + } + private Stream unused() { + return null; + } + } + } + """ + ) + ); + } }