Skip to content

Commit 386244c

Browse files
committed
Support effectively final variables in try-with-resources
Fixes #155 MOE_MIGRATED_REVID=165991267
1 parent 1651bf6 commit 386244c

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

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

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,21 +1757,26 @@ public Void visitTry(TryTree node, Void unused) {
17571757
if (!first) {
17581758
builder.forcedBreak();
17591759
}
1760-
VariableTree variableTree = (VariableTree) resource;
1761-
declareOne(
1762-
DeclarationKind.PARAMETER,
1763-
fieldAnnotationDirection(variableTree.getModifiers()),
1764-
Optional.of(variableTree.getModifiers()),
1765-
variableTree.getType(),
1766-
VarArgsOrNot.NO,
1767-
ImmutableList.<AnnotationTree>of(),
1768-
variableTree.getName(),
1769-
"",
1770-
"=",
1771-
Optional.fromNullable(variableTree.getInitializer()),
1772-
Optional.<String>absent(),
1773-
Optional.<ExpressionTree>absent(),
1774-
Optional.<TypeWithDims>absent());
1760+
if (resource instanceof VariableTree) {
1761+
VariableTree variableTree = (VariableTree) resource;
1762+
declareOne(
1763+
DeclarationKind.PARAMETER,
1764+
fieldAnnotationDirection(variableTree.getModifiers()),
1765+
Optional.of(variableTree.getModifiers()),
1766+
variableTree.getType(),
1767+
VarArgsOrNot.NO,
1768+
ImmutableList.<AnnotationTree>of(),
1769+
variableTree.getName(),
1770+
"",
1771+
"=",
1772+
Optional.fromNullable(variableTree.getInitializer()),
1773+
Optional.<String>absent(),
1774+
Optional.<ExpressionTree>absent(),
1775+
Optional.<TypeWithDims>absent());
1776+
} else {
1777+
// TODO(cushon): think harder about what to do with `try (resource1; resource2) {}`
1778+
scan(resource, null);
1779+
}
17751780
if (builder.peekToken().equals(Optional.of(";"))) {
17761781
token(";");
17771782
builder.space();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class I155 {
2+
void walkAndClose(Stream<?> stream) {
3+
try (stream) {}
4+
}
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class I155 {
2+
void walkAndClose(Stream<?> stream) {
3+
try (stream) {}
4+
}
5+
}

0 commit comments

Comments
 (0)