Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import static java.util.Collections.singleton;

public class NestedEnumsAreNotStatic extends Recipe {

@Override
public String getDisplayName() {
return "Nested enums are not static";
Expand Down Expand Up @@ -58,27 +59,40 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, ctx);
if (cd.getKind() == J.ClassDeclaration.Kind.Type.Enum && cd.getType() != null && cd.getType().getOwningClass() != null) {
if (J.Modifier.hasModifier(cd.getModifiers(), J.Modifier.Type.Static)) {
cd = maybeAutoFormat(cd,
cd.withModifiers(ListUtils.map(cd.getModifiers(), mod ->
mod.getType() == J.Modifier.Type.Static ? null : mod)),
cd.getName(),
ctx,
getCursor().getParent());
}
if (cd.getBody() != null &&
cd.getKind() == J.ClassDeclaration.Kind.Type.Enum &&
cd.getType() != null &&
cd.getType().getOwningClass() != null &&
J.Modifier.hasModifier(cd.getModifiers(), J.Modifier.Type.Static)) {

cd = maybeAutoFormat(
cd,
cd.withModifiers(ListUtils.map(cd.getModifiers(), mod ->
mod.getType() == J.Modifier.Type.Static ? null : mod)),
cd.getName(),
ctx,
getCursor().getParent()
);
}
return cd;
}
});
}


// Preconditions visitor: detects presence of nested enums.
// Must not mutate AST.

private static class HasNestedEnum extends JavaIsoVisitor<ExecutionContext> {

@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, ctx);
if (cd.getKind() == J.ClassDeclaration.Kind.Type.Enum && cd.getType() != null && cd.getType().getOwningClass() != null) {
cd = SearchResult.found(cd);
if (cd.getBody() != null &&
cd.getKind() == J.ClassDeclaration.Kind.Type.Enum &&
cd.getType() != null &&
cd.getType().getOwningClass() != null) {
return SearchResult.found(cd);
}
return cd;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@ static enum ABC {
);
}

@Test
void issue796_nullClassBodyDoesNotThrow() {
rewriteRun(
//language=java
java(
"""
class Outer {
enum Inner {}
}
"""
)
);
}
Comment on lines +81 to +93
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test also passes on main without any recipe changes, so I don't think it accurately reflects the issue we're intending to fix here. I also don't see the outer or inner class body parsed as null, so I'm not sure the conditionals added here would do anything in practice. What lead you to these changes?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also since the nested inner enum isn't static, we wouldn't expect any action from the recipe either.



@Test
void nestedEnumIsNotStatic() {
rewriteRun(
Expand Down