Skip to content

Commit 9c315d6

Browse files
committed
Print var from its token, now that JDK 27 gives it a type node
JDK 27 (JDK-8268850) gives a variable declared with `var` a VarTypeTree as its type, where earlier JDKs have none. This visitor compiles against JDK 21 and has no visitVarType, so it scanned the new node to nothing and every `var` declaration failed on JDK 27: six golden files in CI, among them FlexibleConstructor, I959 and B380299722. declareOne now looks for the `var` token before the type node, the same reordering google-java-format made in google/google-java-format@075e025c94. A binding pattern such as `Nested(var i)` has a method of its own, which now treats a VAR_TYPE node like the missing type of the older JDKs.
1 parent 97358ba commit 9c315d6

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

‎open-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3576,15 +3576,17 @@ int declareOne(
35763576
{
35773577
builder.open(ZERO);
35783578
{
3579-
if (typeWithDims.isPresent() && typeWithDims.get().node != null) {
3579+
// `var` first: from JDK 27 (JDK-8268850) its type is a VarTypeTree rather than null,
3580+
// and this visitor, compiled against JDK 21, has no visitVarType to print it.
3581+
if (isVar) {
3582+
token("var");
3583+
} else if (typeWithDims.isPresent() && typeWithDims.get().node != null) {
35803584
scan(typeWithDims.get().node, null);
35813585
int totalDims = dims.size();
35823586
builder.open(plusFour);
35833587
maybeAddDims(dims);
35843588
builder.close();
35853589
baseDims = totalDims - dims.size();
3586-
} else if (isVar) {
3587-
token("var");
35883590
} else {
35893591
scan(type, null);
35903592
}

‎open-java-format/src/main/java/com/palantir/javaformat/java/java14/Java14InputAstVisitor.java‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,20 @@ public Void visitBindingPattern(BindingPatternTree node, Void unused) {
113113
return null;
114114
}
115115

116+
/**
117+
* Is {@code type} the {@code var} of a binding pattern? Older JDKs leave such a pattern without a type, JDK 27
118+
* (JDK-8268850) gives it a VarTypeTree, which does not exist on JDK 21, so it is matched by {@link Tree.Kind} name.
119+
*/
120+
private static boolean isVarType(Tree type) {
121+
return type.getKind().name().equals("VAR_TYPE");
122+
}
123+
116124
private void visitBindingPattern(ModifiersTree modifiers, Tree type, Name name) {
117125
builder.open(plusFour);
118126
if (modifiers != null) {
119127
builder.addAll(visitModifiers(modifiers, Direction.HORIZONTAL, Optional.empty()));
120128
}
121-
if (type == null) {
129+
if (type == null || isVarType(type)) {
122130
token("var");
123131
} else {
124132
scan(type, null);

0 commit comments

Comments
 (0)