Skip to content

Commit 737b003

Browse files
cushongoogle-java-format Team
authored andcommitted
Support Instance Main Methods in google-java-format
#1216 PiperOrigin-RevId: 858436442
1 parent 4b4d087 commit 737b003

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,15 @@ protected void dropEmptyDeclarations() {
453453
}
454454
}
455455

456+
// Replace with Flags.IMPLICIT_CLASS once JDK 25 is the minimum supported version
457+
private static final int IMPLICIT_CLASS = 1 << 19;
458+
456459
@Override
457460
public Void visitClass(ClassTree tree, Void unused) {
461+
if ((TreeInfo.flags((JCTree) tree) & IMPLICIT_CLASS) == IMPLICIT_CLASS) {
462+
visitImplicitClass(tree);
463+
return null;
464+
}
458465
switch (tree.getKind()) {
459466
case ANNOTATION_TYPE -> visitAnnotationType(tree);
460467
case CLASS, INTERFACE -> visitClassDeclaration(tree);
@@ -465,6 +472,12 @@ public Void visitClass(ClassTree tree, Void unused) {
465472
return null;
466473
}
467474

475+
private void visitImplicitClass(ClassTree node) {
476+
builder.open(minusTwo);
477+
addBodyDeclarations(node.getMembers(), BracesOrNot.NO, FirstDeclarationsOrNot.YES);
478+
builder.close();
479+
}
480+
468481
public void visitAnnotationType(ClassTree node) {
469482
sync(node);
470483
builder.open(ZERO);

core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public class FormatterIntegrationTest {
5959
"I981",
6060
"I1020",
6161
"I1037")
62-
.putAll(25, "ModuleImport")
62+
.putAll(25, "ModuleImport", "InstanceMain")
6363
.build();
6464

6565
@Parameters(name = "{index}: {0}")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
String greeting = "Hello, World!";
2+
3+
void main() {
4+
System.out.println(greeting);
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
String greeting = "Hello, World!";
2+
3+
void main() {
4+
System.out.println(greeting);
5+
}

0 commit comments

Comments
 (0)