Skip to content

Commit cc3b23b

Browse files
author
Vincent Potucek
committed
try can use automatic resource management 1/2
1 parent e19aa49 commit cc3b23b

File tree

4 files changed

+61
-64
lines changed

4 files changed

+61
-64
lines changed

compat/maven-embedder/src/main/java/org/apache/maven/cli/props/MavenProperties.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -486,39 +486,40 @@ public void substitute(UnaryOperator<String> callback) {
486486
* @throws IOException if an error occurs
487487
*/
488488
protected void saveLayout(Writer out, boolean typed) throws IOException {
489-
PropertiesWriter writer = new PropertiesWriter(out, typed);
490-
if (header != null) {
491-
for (String s : header) {
492-
writer.writeln(s);
493-
}
494-
}
489+
try (PropertiesWriter writer = new PropertiesWriter(out, typed)) {
495490

496-
for (String key : storage.keySet()) {
497-
Layout l = layout.get(key);
498-
if (l != null && l.getCommentLines() != null) {
499-
for (String s : l.getCommentLines()) {
491+
if (header != null) {
492+
for (String s : header) {
500493
writer.writeln(s);
501494
}
502495
}
503-
if (l != null && l.getValueLines() != null) {
504-
for (int i = 0; i < l.getValueLines().size(); i++) {
505-
String s = l.getValueLines().get(i);
506-
if (i < l.getValueLines().size() - 1) {
507-
writer.writeln(s + "\\");
508-
} else {
496+
497+
for (String key : storage.keySet()) {
498+
Layout l = layout.get(key);
499+
if (l != null && l.getCommentLines() != null) {
500+
for (String s : l.getCommentLines()) {
509501
writer.writeln(s);
510502
}
511503
}
512-
} else {
513-
writer.writeProperty(key, storage.get(key));
504+
if (l != null && l.getValueLines() != null) {
505+
for (int i = 0; i < l.getValueLines().size(); i++) {
506+
String s = l.getValueLines().get(i);
507+
if (i < l.getValueLines().size() - 1) {
508+
writer.writeln(s + "\\");
509+
} else {
510+
writer.writeln(s);
511+
}
512+
}
513+
} else {
514+
writer.writeProperty(key, storage.get(key));
515+
}
514516
}
515-
}
516-
if (footer != null) {
517-
for (String s : footer) {
518-
writer.writeln(s);
517+
if (footer != null) {
518+
for (String s : footer) {
519+
writer.writeln(s);
520+
}
519521
}
520522
}
521-
writer.flush();
522523
}
523524

524525
/**

impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/CommonsCliOptions.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -473,18 +473,18 @@ public void displayHelp(String command, Consumer<String> pw) {
473473
pw.accept("");
474474

475475
StringWriter sw = new StringWriter();
476-
PrintWriter pw2 = new PrintWriter(sw);
477-
formatter.printHelp(
478-
pw2,
479-
width,
480-
commandLineSyntax(command),
481-
System.lineSeparator() + "Options:",
482-
options,
483-
HelpFormatter.DEFAULT_LEFT_PAD,
484-
HelpFormatter.DEFAULT_DESC_PAD,
485-
System.lineSeparator(),
486-
false);
487-
pw2.flush();
476+
try (PrintWriter writer = new PrintWriter(sw)) {
477+
formatter.printHelp(
478+
writer,
479+
width,
480+
commandLineSyntax(command),
481+
System.lineSeparator() + "Options:",
482+
options,
483+
HelpFormatter.DEFAULT_LEFT_PAD,
484+
HelpFormatter.DEFAULT_DESC_PAD,
485+
System.lineSeparator(),
486+
false);
487+
}
488488
for (String s : sw.toString().split(System.lineSeparator())) {
489489
pw.accept(s);
490490
}

impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,7 @@ protected Consumer<String> doDetermineWriter(C context) {
407407
} else {
408408
// Given the terminal creation has been offloaded to a different thread,
409409
// do not pass directly the terminal writer
410-
return msg -> {
411-
PrintWriter pw = context.terminal.writer();
412-
pw.println(msg);
413-
pw.flush();
414-
};
410+
return msg -> context.terminal.writer().println(msg);
415411
}
416412
}
417413

impl/maven-cli/src/main/java/org/apache/maven/cling/props/MavenProperties.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -485,39 +485,39 @@ public void substitute(UnaryOperator<String> callback) {
485485
* @throws IOException if an error occurs
486486
*/
487487
protected void saveLayout(Writer out, boolean typed) throws IOException {
488-
PropertiesWriter writer = new PropertiesWriter(out, typed);
489-
if (header != null) {
490-
for (String s : header) {
491-
writer.writeln(s);
492-
}
493-
}
494-
495-
for (String key : storage.keySet()) {
496-
Layout l = layout.get(key);
497-
if (l != null && l.getCommentLines() != null) {
498-
for (String s : l.getCommentLines()) {
488+
try (PropertiesWriter writer = new PropertiesWriter(out, typed)) {
489+
if (header != null) {
490+
for (String s : header) {
499491
writer.writeln(s);
500492
}
501493
}
502-
if (l != null && l.getValueLines() != null) {
503-
for (int i = 0; i < l.getValueLines().size(); i++) {
504-
String s = l.getValueLines().get(i);
505-
if (i < l.getValueLines().size() - 1) {
506-
writer.writeln(s + "\\");
507-
} else {
494+
495+
for (String key : storage.keySet()) {
496+
Layout l = layout.get(key);
497+
if (l != null && l.getCommentLines() != null) {
498+
for (String s : l.getCommentLines()) {
508499
writer.writeln(s);
509500
}
510501
}
511-
} else {
512-
writer.writeProperty(key, storage.get(key));
502+
if (l != null && l.getValueLines() != null) {
503+
for (int i = 0; i < l.getValueLines().size(); i++) {
504+
String s = l.getValueLines().get(i);
505+
if (i < l.getValueLines().size() - 1) {
506+
writer.writeln(s + "\\");
507+
} else {
508+
writer.writeln(s);
509+
}
510+
}
511+
} else {
512+
writer.writeProperty(key, storage.get(key));
513+
}
513514
}
514-
}
515-
if (footer != null) {
516-
for (String s : footer) {
517-
writer.writeln(s);
515+
if (footer != null) {
516+
for (String s : footer) {
517+
writer.writeln(s);
518+
}
518519
}
519520
}
520-
writer.flush();
521521
}
522522

523523
/**

0 commit comments

Comments
 (0)