diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java index a2a32e79c..2f0373ec6 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java @@ -453,8 +453,15 @@ protected void dropEmptyDeclarations() { } } + // Replace with Flags.IMPLICIT_CLASS once JDK 25 is the minimum supported version + private static final int IMPLICIT_CLASS = 1 << 19; + @Override public Void visitClass(ClassTree tree, Void unused) { + if ((TreeInfo.flags((JCTree) tree) & IMPLICIT_CLASS) == IMPLICIT_CLASS) { + visitImplicitClass(tree); + return null; + } switch (tree.getKind()) { case ANNOTATION_TYPE -> visitAnnotationType(tree); case CLASS, INTERFACE -> visitClassDeclaration(tree); @@ -465,6 +472,12 @@ public Void visitClass(ClassTree tree, Void unused) { return null; } + private void visitImplicitClass(ClassTree node) { + builder.open(minusTwo); + addBodyDeclarations(node.getMembers(), BracesOrNot.NO, FirstDeclarationsOrNot.YES); + builder.close(); + } + public void visitAnnotationType(ClassTree node) { sync(node); builder.open(ZERO); diff --git a/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java b/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java index a406487f3..7b3c3e889 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java @@ -59,7 +59,7 @@ public class FormatterIntegrationTest { "I981", "I1020", "I1037") - .putAll(25, "ModuleImport") + .putAll(25, "ModuleImport", "InstanceMain") .build(); @Parameters(name = "{index}: {0}") diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/InstanceMain.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/InstanceMain.input new file mode 100644 index 000000000..5967d6c66 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/InstanceMain.input @@ -0,0 +1,5 @@ +String greeting = "Hello, World!"; + +void main() { + System.out.println(greeting); +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/InstanceMain.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/InstanceMain.output new file mode 100644 index 000000000..6f152da61 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/InstanceMain.output @@ -0,0 +1,5 @@ +String greeting = "Hello, World!"; + +void main() { + System.out.println(greeting); +}