Skip to content

Commit 736160d

Browse files
committed
Minimize duplicate logic & traversal in NestedEnumsAreNotStatic
Closes #802
1 parent 2a88748 commit 736160d

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

src/main/java/org/openrewrite/staticanalysis/NestedEnumsAreNotStatic.java

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
import org.openrewrite.internal.ListUtils;
2323
import org.openrewrite.java.JavaIsoVisitor;
2424
import org.openrewrite.java.tree.J;
25-
import org.openrewrite.marker.SearchResult;
2625
import org.openrewrite.staticanalysis.csharp.CSharpFileChecker;
2726

2827
import java.time.Duration;
2928
import java.util.Set;
3029

3130
import static java.util.Collections.singleton;
31+
import static org.openrewrite.java.tree.J.Modifier.Type.Static;
3232

3333
public 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
}

src/test/java/org/openrewrite/staticanalysis/NestedEnumsAreNotStaticTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,27 @@ public enum testEnum {
125125
)
126126
);
127127
}
128+
129+
@Test
130+
void emptyInnerEnum() {
131+
rewriteRun(
132+
//language=java
133+
java(
134+
"""
135+
class Outer {
136+
static enum Inner {
137+
A, B, C
138+
}
139+
}
140+
""",
141+
"""
142+
class Outer {
143+
enum Inner {
144+
A, B, C
145+
}
146+
}
147+
"""
148+
)
149+
);
150+
}
128151
}

0 commit comments

Comments
 (0)