Skip to content

Commit cdc11e7

Browse files
author
Vincent Potucek
committed
try can use automatic resource management
1 parent 64e3eeb commit cdc11e7

File tree

5 files changed

+63
-69
lines changed

5 files changed

+63
-69
lines changed

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

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -487,41 +487,40 @@ public void substitute(UnaryOperator<String> callback) {
487487
* @throws IOException if an error occurs
488488
*/
489489
protected void saveLayout(Writer out, boolean typed) throws IOException {
490-
try(PropertiesWriter writer = new PropertiesWriter(out, typed)) {
490+
try (PropertiesWriter writer = new PropertiesWriter(out, typed)) {
491491

492-
493-
if (header != null) {
494-
for (String s : header) {
495-
writer.writeln(s);
496-
}
497-
}
498-
499-
for (String key : storage.keySet()) {
500-
Layout l = layout.get(key);
501-
if (l != null && l.getCommentLines() != null) {
502-
for (String s : l.getCommentLines()) {
492+
if (header != null) {
493+
for (String s : header) {
503494
writer.writeln(s);
504495
}
505496
}
506-
if (l != null && l.getValueLines() != null) {
507-
for (int i = 0; i < l.getValueLines().size(); i++) {
508-
String s = l.getValueLines().get(i);
509-
if (i < l.getValueLines().size() - 1) {
510-
writer.writeln(s + "\\");
511-
} else {
497+
498+
for (String key : storage.keySet()) {
499+
Layout l = layout.get(key);
500+
if (l != null && l.getCommentLines() != null) {
501+
for (String s : l.getCommentLines()) {
512502
writer.writeln(s);
513503
}
514504
}
515-
} else {
516-
writer.writeProperty(key, storage.get(key));
505+
if (l != null && l.getValueLines() != null) {
506+
for (int i = 0; i < l.getValueLines().size(); i++) {
507+
String s = l.getValueLines().get(i);
508+
if (i < l.getValueLines().size() - 1) {
509+
writer.writeln(s + "\\");
510+
} else {
511+
writer.writeln(s);
512+
}
513+
}
514+
} else {
515+
writer.writeProperty(key, storage.get(key));
516+
}
517517
}
518-
}
519-
if (footer != null) {
520-
for (String s : footer) {
521-
writer.writeln(s);
518+
if (footer != null) {
519+
for (String s : footer) {
520+
writer.writeln(s);
521+
}
522522
}
523523
}
524-
}
525524
}
526525

527526
/**

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

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@
2020

2121
import java.io.PrintWriter;
2222
import java.io.StringWriter;
23-
import java.util.LinkedHashSet;
24-
import java.util.Map;
25-
import java.util.Optional;
26-
import java.util.Set;
23+
import java.util.*;
2724
import java.util.function.Consumer;
2825

2926
import org.apache.commons.cli.CommandLine;
@@ -474,20 +471,18 @@ public void displayHelp(String command, Consumer<String> pw) {
474471

475472
StringWriter sw = new StringWriter();
476473
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-
}
488-
for (String s : sw.toString().split(System.lineSeparator())) {
489-
pw.accept(s);
474+
formatter.printHelp(
475+
writer,
476+
width,
477+
commandLineSyntax(command),
478+
System.lineSeparator() + "Options:",
479+
options,
480+
HelpFormatter.DEFAULT_LEFT_PAD,
481+
HelpFormatter.DEFAULT_DESC_PAD,
482+
System.lineSeparator(),
483+
false);
490484
}
485+
Arrays.stream(sw.toString().split(System.lineSeparator())).forEach(pw);
491486
}
492487

493488
protected String commandLineSyntax(String command) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ protected Consumer<String> doDetermineWriter(C context) {
409409
// do not pass directly the terminal writer
410410
return msg -> {
411411
try (PrintWriter pw = context.terminal.writer()) {
412-
pw.println(msg);
412+
pw.println(msg);
413413
}
414414
};
415415
}

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

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -486,39 +486,39 @@ public void substitute(UnaryOperator<String> callback) {
486486
*/
487487
protected void saveLayout(Writer out, boolean typed) throws IOException {
488488
try (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()) {
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
}
520521
}
521-
}
522522

523523
/**
524524
* Checks if parts of the passed in comment can be used as header comment.

impl/maven-executor/src/main/java/org/apache/maven/cling/executor/forked/ForkedMavenExecutor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ protected CountDownLatch pump(Process p, ExecutorRequest executorRequest) {
163163
CountDownLatch latch = new CountDownLatch(3);
164164
String suffix = "-pump-" + p.pid();
165165
Thread stdoutPump = new Thread(() -> {
166-
try (OutputStream stdout = executorRequest.stdOut().orElse(OutputStream.nullOutputStream())){
166+
try (OutputStream stdout = executorRequest.stdOut().orElse(OutputStream.nullOutputStream())) {
167167
p.getInputStream().transferTo(stdout);
168168
} catch (IOException e) {
169169
throw new UncheckedIOException(e);
@@ -174,7 +174,7 @@ protected CountDownLatch pump(Process p, ExecutorRequest executorRequest) {
174174
stdoutPump.setName("stdout" + suffix);
175175
stdoutPump.start();
176176
Thread stderrPump = new Thread(() -> {
177-
try (OutputStream stderr = executorRequest.stdErr().orElse(OutputStream.nullOutputStream())){
177+
try (OutputStream stderr = executorRequest.stdErr().orElse(OutputStream.nullOutputStream())) {
178178
p.getErrorStream().transferTo(stderr);
179179
} catch (IOException e) {
180180
throw new UncheckedIOException(e);
@@ -185,7 +185,7 @@ protected CountDownLatch pump(Process p, ExecutorRequest executorRequest) {
185185
stderrPump.setName("stderr" + suffix);
186186
stderrPump.start();
187187
Thread stdinPump = new Thread(() -> {
188-
try (OutputStream in = p.getOutputStream()){
188+
try (OutputStream in = p.getOutputStream()) {
189189
executorRequest.stdIn().orElse(InputStream.nullInputStream()).transferTo(in);
190190
} catch (IOException e) {
191191
throw new UncheckedIOException(e);

0 commit comments

Comments
 (0)