From 075e025c94a4bea2db8e0fb6d4b5c05d0d810cad Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Fri, 27 Feb 2026 07:29:30 -0800 Subject: [PATCH] Prepare to changes to `var` in AST After https://bugs.openjdk.org/browse/JDK-8268850, `VariableTree#getType` returns a new AST node `VarTypeTree` for variables declared with `var`. The new node could be handled in the visitor by implementing `visitVarType`, but that API is only available on the latest JDK versions using var, which would require creating a new version-specific visitor implementation. To defer doing that, this fix re-orders the logic that handles variable types to check if the next token is `var` first, to avoid needing to handle the new AST node. PiperOrigin-RevId: 876244813 --- .../google/googlejavaformat/java/JavaInputAstVisitor.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 7a0dde3cd..9ff42cce6 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java @@ -3686,15 +3686,15 @@ protected int declareOne( builder.open(ZERO); { visitAnnotations(annotations, BreakOrNot.NO, BreakOrNot.YES); - if (typeWithDims.isPresent() && typeWithDims.get().node != null) { + if (isVar) { + token("var"); + } else if (typeWithDims.isPresent() && typeWithDims.get().node != null) { scan(typeWithDims.get().node, null); int totalDims = dims.size(); builder.open(plusFour); maybeAddDims(dims); builder.close(); baseDims = totalDims - dims.size(); - } else if (isVar) { - token("var"); } else { scan(type, null); }