From 737b0032b3a18eb6e458271ea440098c166f6c2d Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Mon, 19 Jan 2026 23:38:25 -0800 Subject: [PATCH] Support Instance Main Methods in google-java-format https://github.com/google/google-java-format/issues/1216 PiperOrigin-RevId: 858436442 --- .../googlejavaformat/java/JavaInputAstVisitor.java | 13 +++++++++++++ .../java/FormatterIntegrationTest.java | 2 +- .../java/testdata/InstanceMain.input | 5 +++++ .../java/testdata/InstanceMain.output | 5 +++++ 4 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/InstanceMain.input create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/InstanceMain.output 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); +}