Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2091,14 +2091,17 @@ public Void visitTry(TryTree node, Void unused) {
builder.space();
if (!node.getResources().isEmpty()) {
token("(");
builder.open(node.getResources().size() > 1 ? plusFour : ZERO);
boolean multiVariable = node.getResources().size() > 1;
builder.open(multiVariable ? plusFour : ZERO);
if (multiVariable) {
builder.forcedBreak();
}
boolean afterFirstToken = false;
for (Tree resource : node.getResources()) {
if (afterFirstToken) {
builder.forcedBreak();
}
if (resource instanceof VariableTree variableTree) {

declareOne(
DeclarationKind.PARAMETER,
fieldAnnotationDirection(variableTree.getModifiers()),
Expand All @@ -2112,7 +2115,6 @@ public Void visitTry(TryTree node, Void unused) {
/* receiverExpression= */ Optional.empty(),
/* typeWithDims= */ Optional.empty());
} else {
// TODO(cushon): think harder about what to do with `try (resource1; resource2) {}`
scan(resource, null);
}
if (builder.peekToken().equals(Optional.of(";"))) {
Expand All @@ -2121,12 +2123,11 @@ public Void visitTry(TryTree node, Void unused) {
}
afterFirstToken = true;
}
if (builder.peekToken().equals(Optional.of(";"))) {
token(";");
builder.space();
builder.close();
if (multiVariable) {
builder.forcedBreak();
}
token(")");
builder.close();
builder.space();
}
// An empty try-with-resources body can collapse to "{}" if there are no trailing catch or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,34 @@ void f() {
""");
}

@Test
public void multivariableTryWithResources() throws Exception {
String input =
"class Test {\n"
+ " void m() {\n"
+ " try (var input = Files.newInputStream(Path.of(\"./input\"));\n"
+ " var output = Files.newOutputStream(Path.of(\"./output\"));) {\n"
+ " output.write(input.read());\n"
+ " } catch (IOException _) {\n"
+ " }\n"
+ " }\n"
+ "}\n";
String expected =
"class Test {\n"
+ " void m() {\n"
+ " try (\n"
+ " var input = Files.newInputStream(Path.of(\"./input\"));\n"
+ " var output = Files.newOutputStream(Path.of(\"./output\"));\n"
+ " ) {\n"
+ " output.write(input.read());\n"
+ " } catch (IOException _) {\n"
+ " }\n"
+ " }\n"
+ "}\n";

assertThat(new Formatter().formatSource(input)).isEqualTo(expected);
}

@Test
public void testI1205() throws Exception {
String input =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
class B21465217 {
void m() {
try (JimfsOutputStream out2 = newOutputStream(false);
try (
JimfsOutputStream out2 = newOutputStream(false);
BufferedOutputStream bout = new BufferedOutputStream(out2);
OutputStreamWriter writer =
new OutputStreamWriter(bout, UTF_8___________________________)) {}
OutputStreamWriter writer = new OutputStreamWriter(bout, UTF_8___________________________)
) {}

try (Writer sourceWriter =
env.getFiler().createSourceFile(qualifiedNamezzzzzzzz).openWriter()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
class B26159561 {
{
try (A a = a();
B b = b()) {}
try (
A a = a();
B b = b()
) {}
try (A a = a(); ) {}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class TryWtihResources {
class TryWithResources {
{
try (@A C c = c(); ) {}
try (final @A C c = c(); ) {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class TryWtihResources {
class TryWithResources {
{
try (@A C c = c(); ) {}
try (final @A C c = c(); ) {}
Expand Down
Loading