From c7a76c1d90d3610c115b5e6124291f9d1f448c23 Mon Sep 17 00:00:00 2001 From: Mark Chesney Date: Tue, 8 Oct 2024 07:09:58 -0700 Subject: [PATCH] Fix issues #842 and #843 --- .../mutators/UnnecessaryModifier.java | 19 ++++-- .../TestUnnecessaryModifierCases.java | 60 +++++++++++++++++++ 2 files changed, 73 insertions(+), 6 deletions(-) diff --git a/java/src/main/java/eu/solven/cleanthat/engine/java/refactorer/mutators/UnnecessaryModifier.java b/java/src/main/java/eu/solven/cleanthat/engine/java/refactorer/mutators/UnnecessaryModifier.java index 91533f5c1..488107073 100644 --- a/java/src/main/java/eu/solven/cleanthat/engine/java/refactorer/mutators/UnnecessaryModifier.java +++ b/java/src/main/java/eu/solven/cleanthat/engine/java/refactorer/mutators/UnnecessaryModifier.java @@ -27,6 +27,7 @@ import com.github.javaparser.ast.body.AnnotationDeclaration; import com.github.javaparser.ast.body.AnnotationMemberDeclaration; import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; +import com.github.javaparser.ast.body.EnumDeclaration; import com.github.javaparser.ast.body.FieldDeclaration; import com.github.javaparser.ast.body.MethodDeclaration; import com.github.javaparser.ast.nodeTypes.NodeWithModifiers; @@ -109,6 +110,8 @@ protected boolean processNotRecursively(NodeAndSymbolSolver nodeAndSymbolSolv var parentNode = modifier.getParentNode().get(); if (!(parentNode instanceof MethodDeclaration) && !(parentNode instanceof FieldDeclaration) && !(parentNode instanceof ClassOrInterfaceDeclaration) + && !(parentNode instanceof AnnotationDeclaration) + && !(parentNode instanceof EnumDeclaration) && !(parentNode instanceof AnnotationMemberDeclaration)) { return false; } @@ -124,9 +127,11 @@ protected boolean processNotRecursively(NodeAndSymbolSolver nodeAndSymbolSolv return false; } - // We are considering a modifier from an interface method|field|classOrInterface - if (modifier.getKeyword() == Keyword.PUBLIC || modifier.getKeyword() == Keyword.ABSTRACT - || modifier.getKeyword() == Keyword.FINAL + // We are considering a modifier from an interface method|field|classOrInterface|annotation|enum + if (modifier.getKeyword() == Keyword.PUBLIC + || (modifier.getKeyword() == Keyword.ABSTRACT || modifier.getKeyword() == Keyword.FINAL) + && !(parentNode instanceof ClassOrInterfaceDeclaration + && !((ClassOrInterfaceDeclaration) parentNode).isInterface()) || modifier.getKeyword() == Keyword.STATIC && !(parentNode instanceof MethodDeclaration)) { // https://github.com/javaparser/javaparser/issues/3935 @@ -150,9 +155,11 @@ protected boolean processNotRecursively(NodeAndSymbolSolver nodeAndSymbolSolv return false; } else if (grandParentNode instanceof AnnotationDeclaration) { - // We are considering a modifier from an annotation method|field|classOrInterface - if (modifier.getKeyword() == Keyword.PUBLIC || modifier.getKeyword() == Keyword.ABSTRACT - || modifier.getKeyword() == Keyword.FINAL + // We are considering a modifier from an annotation classOrInterface|annotation|enum|annotationMember + if (modifier.getKeyword() == Keyword.PUBLIC + || (modifier.getKeyword() == Keyword.ABSTRACT || modifier.getKeyword() == Keyword.FINAL) + && !(parentNode instanceof ClassOrInterfaceDeclaration + && !((ClassOrInterfaceDeclaration) parentNode).isInterface()) || modifier.getKeyword() == Keyword.STATIC) { return modifier.remove(); } diff --git a/java/src/test/java/eu/solven/cleanthat/engine/java/refactorer/cases/do_not_format_me/TestUnnecessaryModifierCases.java b/java/src/test/java/eu/solven/cleanthat/engine/java/refactorer/cases/do_not_format_me/TestUnnecessaryModifierCases.java index 511c72430..aad8961b0 100644 --- a/java/src/test/java/eu/solven/cleanthat/engine/java/refactorer/cases/do_not_format_me/TestUnnecessaryModifierCases.java +++ b/java/src/test/java/eu/solven/cleanthat/engine/java/refactorer/cases/do_not_format_me/TestUnnecessaryModifierCases.java @@ -37,6 +37,21 @@ public static interface Baz { static String parse() { return "parsed"; } + + // public, static, abstract are redundant + public static abstract interface MyInterface {} + + // public and static are redundant, but not abstract + public static abstract class MyAbstractClass implements MyInterface {} + + // public and static are redundant, but not final + public static final class MyFinalClass extends MyAbstractClass {} + + // public, static, and abstract are redundant + public static abstract @interface MyAnnotation {} + + // public and static are redundant + public static enum MyEnum {} } public interface Post { @@ -57,6 +72,21 @@ interface Baz { static String parse() { return "parsed"; } + + // public, static, abstract are redundant + interface MyInterface {} + + // public and static are redundant, but not abstract + abstract class MyAbstractClass implements MyInterface {} + + // public and static are redundant, but not final + final class MyFinalClass extends MyAbstractClass {} + + // public, static, and abstract are redundant + @interface MyAnnotation {} + + // public and static are redundant + enum MyEnum {} } } @@ -77,6 +107,21 @@ public static class Bar { // ditto public static interface Baz { } + + // public, static, abstract are redundant + public static abstract interface MyInterface {} + + // public and static are redundant, but not abstract + public static abstract class MyAbstractClass implements MyInterface {} + + // public and static are redundant, but not final + public static final class MyFinalClass extends MyAbstractClass {} + + // public, static, and abstract are redundant + public static abstract @interface MyAnnotation {} + + // public and static are redundant + public static enum MyEnum {} } public @interface Post { @@ -93,6 +138,21 @@ class Bar { // ditto interface Baz { } + + // public, static, abstract are redundant + interface MyInterface {} + + // public and static are redundant, but not abstract + abstract class MyAbstractClass implements MyInterface {} + + // public and static are redundant, but not final + final class MyFinalClass extends MyAbstractClass {} + + // public, static, and abstract are redundant + @interface MyAnnotation {} + + // public and static are redundant + enum MyEnum {} } }