Skip to content

Commit 8a25d00

Browse files
cushoncgruber
authored andcommitted
Don't crash on empty type declarations.
JLS 7.6 says: > Extra ";" tokens appearing at the level of type declarations in a compilation > unit have no effect on the meaning of the compilation unit. Stray semicolons > are permitted in the Java programming language solely as a concession to C++ > programmers who are used to placing ";" after a class declaration. They > should not be used in new Java code. The formatted output for this is not pretty, but we don't care. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=106540484
1 parent 79b7f0f commit 8a25d00

File tree

5 files changed

+41
-0
lines changed

5 files changed

+41
-0
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
@@ -329,6 +329,8 @@ private static final ImmutableList<Op> forceBreakList(Optional<BreakTag> breakTa
329329
private static final int MAX_FILLED_INFIX_LINES = 1;
330330
private static final int MAX_LINES_FOR_FORMAL_LIST = 1;
331331

332+
private static final int MAX_EMPTY_DECLS = 10;
333+
332334
/**
333335
* The {@code Visitor} constructor.
334336
* @param builder the {@link OpsBuilder}
@@ -388,16 +390,25 @@ public boolean visit(CompilationUnit node) {
388390
if (!first) {
389391
builder.blankLineWanted(BlankLineWanted.YES);
390392
}
393+
dropEmptyDeclarations();
391394
markForPartialFormat();
392395
type.accept(this);
393396
builder.forcedBreak();
394397
first = false;
395398
}
399+
dropEmptyDeclarations();
396400
// set a partial format marker at EOF to make sure we can format the entire file
397401
markForPartialFormat();
398402
return false;
399403
}
400404

405+
/** Skips over extra semi-colon at the top-level, or in a class member declaration lists. */
406+
private void dropEmptyDeclarations() {
407+
for (int times = 0; times < MAX_EMPTY_DECLS; times++) {
408+
builder.guessToken(";");
409+
}
410+
}
411+
401412
/** Visitor method for {@link AnnotationTypeDeclaration}s. */
402413
@Override
403414
public boolean visit(AnnotationTypeDeclaration node) {
@@ -3166,6 +3177,7 @@ void addBodyDeclarations(
31663177
boolean first = first0.isYes();
31673178
boolean lastOneGotBlankLineBefore = false;
31683179
for (BodyDeclaration bodyDeclaration : bodyDeclarations) {
3180+
dropEmptyDeclarations();
31693181
builder.forcedBreak();
31703182
boolean thisOneGetsBlankLineBefore =
31713183
bodyDeclaration.getNodeType() != ASTNode.FIELD_DECLARATION
@@ -3184,6 +3196,7 @@ void addBodyDeclarations(
31843196
builder.forcedBreak();
31853197
markForPartialFormat();
31863198
if (braces.isYes()) {
3199+
dropEmptyDeclarations();
31873200
builder.blankLineWanted(BlankLineWanted.NO);
31883201
token("}", plusTwo);
31893202
builder.close();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package test;
2+
3+
import java.util.List;;
4+
5+
;
6+
class Test {
7+
;
8+
public int x = 42;;
9+
;;;
10+
{
11+
int x = 1;;;
12+
}
13+
;
14+
}
15+
;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package test;
2+
3+
import java.util.List;
4+
5+
;;class Test {;
6+
public int x = 42;;;;;
7+
8+
{
9+
int x = 1;
10+
;
11+
;
12+
};
13+
};

idea_plugin/src/main/scripts/install-idea-jars.sh

100755100644
File mode changed.

scripts/google-java-format-diff.py

100755100644
File mode changed.

0 commit comments

Comments
 (0)