2222import org .openrewrite .internal .ListUtils ;
2323import org .openrewrite .java .JavaIsoVisitor ;
2424import org .openrewrite .java .tree .J ;
25- import org .openrewrite .marker .SearchResult ;
2625import org .openrewrite .staticanalysis .csharp .CSharpFileChecker ;
2726
2827import java .time .Duration ;
2928import java .util .Set ;
3029
3130import static java .util .Collections .singleton ;
31+ import static org .openrewrite .java .tree .J .Modifier .Type .Static ;
3232
3333public class NestedEnumsAreNotStatic extends Recipe {
3434 @ Override
@@ -53,34 +53,27 @@ public Duration getEstimatedEffortPerOccurrence() {
5353
5454 @ Override
5555 public TreeVisitor <?, ExecutionContext > getVisitor () {
56- TreeVisitor <?, ExecutionContext > preconditions = Preconditions .and (new HasNestedEnum (), Preconditions .not (new CSharpFileChecker <>()));
57- return Preconditions .check (preconditions , new JavaIsoVisitor <ExecutionContext >() {
56+ return Preconditions .check (Preconditions .not (new CSharpFileChecker <>()), new JavaIsoVisitor <ExecutionContext >() {
5857 @ Override
5958 public J .ClassDeclaration visitClassDeclaration (J .ClassDeclaration classDecl , ExecutionContext ctx ) {
6059 J .ClassDeclaration cd = super .visitClassDeclaration (classDecl , ctx );
61- if (cd .getKind () == J .ClassDeclaration .Kind .Type .Enum && cd .getType () != null && cd .getType ().getOwningClass () != null ) {
62- if (J .Modifier .hasModifier (cd .getModifiers (), J .Modifier .Type .Static )) {
63- cd = maybeAutoFormat (cd ,
64- cd .withModifiers (ListUtils .map (cd .getModifiers (), mod ->
65- mod .getType () == J .Modifier .Type .Static ? null : mod )),
66- cd .getName (),
67- ctx ,
68- getCursor ().getParent ());
69- }
60+ if (shouldRemoveStaticModifierFromClass (cd )) {
61+ return maybeAutoFormat (
62+ cd ,
63+ cd .withModifiers (ListUtils .filter (cd .getModifiers (), mod -> mod .getType () != Static )),
64+ cd .getName (),
65+ ctx ,
66+ getCursor ().getParent ());
7067 }
7168 return cd ;
7269 }
73- });
74- }
7570
76- private static class HasNestedEnum extends JavaIsoVisitor <ExecutionContext > {
77- @ Override
78- public J .ClassDeclaration visitClassDeclaration (J .ClassDeclaration classDecl , ExecutionContext ctx ) {
79- J .ClassDeclaration cd = super .visitClassDeclaration (classDecl , ctx );
80- if (cd .getKind () == J .ClassDeclaration .Kind .Type .Enum && cd .getType () != null && cd .getType ().getOwningClass () != null ) {
81- cd = SearchResult .found (cd );
71+ private boolean shouldRemoveStaticModifierFromClass (J .ClassDeclaration cd ) {
72+ return cd .getKind () == J .ClassDeclaration .Kind .Type .Enum &&
73+ cd .getType () != null &&
74+ cd .getType ().getOwningClass () != null &&
75+ J .Modifier .hasModifier (cd .getModifiers (), Static );
8276 }
83- return cd ;
84- }
77+ });
8578 }
8679}
0 commit comments