Skip to content

Commit 188917a

Browse files
author
Vincent Potucek
committed
try can use automatic resource management
1 parent 32d9512 commit 188917a

File tree

8 files changed

+83
-117
lines changed

8 files changed

+83
-117
lines changed

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

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.util.Set;
4545
import java.util.function.UnaryOperator;
4646

47+
import ch.qos.logback.core.Layout;
4748
import org.apache.maven.impl.model.DefaultInterpolator;
4849

4950
/**
@@ -486,39 +487,40 @@ public void substitute(UnaryOperator<String> callback) {
486487
* @throws IOException if an error occurs
487488
*/
488489
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-
}
490+
try (PropertiesWriter writer = new PropertiesWriter(out, typed)) {
495491

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()) {
492+
if (header != null) {
493+
for (String s : header) {
500494
writer.writeln(s);
501495
}
502496
}
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 {
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()) {
509502
writer.writeln(s);
510503
}
511504
}
512-
} else {
513-
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+
}
514517
}
515-
}
516-
if (footer != null) {
517-
for (String s : footer) {
518-
writer.writeln(s);
518+
if (footer != null) {
519+
for (String s : footer) {
520+
writer.writeln(s);
521+
}
519522
}
520523
}
521-
writer.flush();
522524
}
523525

524526
/**

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.io.PrintWriter;
2222
import java.io.StringWriter;
23+
import java.util.Arrays;
2324
import java.util.LinkedHashSet;
2425
import java.util.Map;
2526
import java.util.Optional;
@@ -473,21 +474,19 @@ public void displayHelp(String command, Consumer<String> pw) {
473474
pw.accept("");
474475

475476
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();
488-
for (String s : sw.toString().split(System.lineSeparator())) {
489-
pw.accept(s);
477+
try (PrintWriter writer = new PrintWriter(sw)) {
478+
formatter.printHelp(
479+
writer,
480+
width,
481+
commandLineSyntax(command),
482+
System.lineSeparator() + "Options:",
483+
options,
484+
HelpFormatter.DEFAULT_LEFT_PAD,
485+
HelpFormatter.DEFAULT_DESC_PAD,
486+
System.lineSeparator(),
487+
false);
490488
}
489+
Arrays.stream(sw.toString().split(System.lineSeparator())).forEach(pw);
491490
}
492491

493492
protected String commandLineSyntax(String command) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,9 @@ protected Consumer<String> doDetermineWriter(C context) {
408408
// Given the terminal creation has been offloaded to a different thread,
409409
// do not pass directly the terminal writer
410410
return msg -> {
411-
PrintWriter pw = context.terminal.writer();
412-
pw.println(msg);
413-
pw.flush();
411+
try (PrintWriter pw = context.terminal.writer()) {
412+
pw.println(msg);
413+
}
414414
};
415415
}
416416
}

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
/**

impl/maven-cli/src/main/java/org/apache/maven/cling/transfer/ConsoleMavenTransferListener.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ public ConsoleMavenTransferListener(
5050

5151
@Override
5252
public void transferInitiated(TransferEvent event) {
53-
overridePreviousTransfer(event);
53+
overridePreviousTransfer();
5454

5555
super.transferInitiated(event);
5656
}
5757

5858
@Override
5959
public void transferCorrupted(TransferEvent event) throws TransferCancelledException {
60-
overridePreviousTransfer(event);
60+
overridePreviousTransfer();
6161

6262
super.transferCorrupted(event);
6363
}
@@ -123,20 +123,20 @@ private void pad(StringBuilder buffer, int spaces) {
123123
@Override
124124
public void transferSucceeded(TransferEvent event) {
125125
transfers.remove(new TransferResourceIdentifier(event.getResource()));
126-
overridePreviousTransfer(event);
126+
overridePreviousTransfer();
127127

128128
super.transferSucceeded(event);
129129
}
130130

131131
@Override
132132
public void transferFailed(TransferEvent event) {
133133
transfers.remove(new TransferResourceIdentifier(event.getResource()));
134-
overridePreviousTransfer(event);
134+
overridePreviousTransfer();
135135

136136
super.transferFailed(event);
137137
}
138138

139-
private void overridePreviousTransfer(TransferEvent event) {
139+
private void overridePreviousTransfer() {
140140
if (lastLength > 0) {
141141
pad(buffer, lastLength);
142142
buffer.append('\r');

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,8 @@ 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 {
167-
OutputStream stdout = executorRequest.stdOut().orElse(OutputStream.nullOutputStream());
166+
try (OutputStream stdout = executorRequest.stdOut().orElse(OutputStream.nullOutputStream())) {
168167
p.getInputStream().transferTo(stdout);
169-
stdout.flush();
170168
} catch (IOException e) {
171169
throw new UncheckedIOException(e);
172170
} finally {
@@ -176,10 +174,8 @@ protected CountDownLatch pump(Process p, ExecutorRequest executorRequest) {
176174
stdoutPump.setName("stdout" + suffix);
177175
stdoutPump.start();
178176
Thread stderrPump = new Thread(() -> {
179-
try {
180-
OutputStream stderr = executorRequest.stdErr().orElse(OutputStream.nullOutputStream());
177+
try (OutputStream stderr = executorRequest.stdErr().orElse(OutputStream.nullOutputStream())) {
181178
p.getErrorStream().transferTo(stderr);
182-
stderr.flush();
183179
} catch (IOException e) {
184180
throw new UncheckedIOException(e);
185181
} finally {
@@ -189,10 +185,8 @@ protected CountDownLatch pump(Process p, ExecutorRequest executorRequest) {
189185
stderrPump.setName("stderr" + suffix);
190186
stderrPump.start();
191187
Thread stdinPump = new Thread(() -> {
192-
try {
193-
OutputStream in = p.getOutputStream();
188+
try (OutputStream in = p.getOutputStream()) {
194189
executorRequest.stdIn().orElse(InputStream.nullInputStream()).transferTo(in);
195-
in.flush();
196190
} catch (IOException e) {
197191
throw new UncheckedIOException(e);
198192
} finally {

its/core-it-support/core-it-plugins/maven-it-plugin-context-passing/src/main/java/org/apache/maven/plugin/coreit/CatchMojo.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,10 @@ public void execute() throws MojoExecutionException {
5858

5959
File outfile = new File(outDir, value);
6060

61-
Writer writer = null;
62-
try {
63-
writer = new FileWriter(outfile);
64-
61+
try (Writer writer = new FileWriter(outfile)) {
6562
writer.write(value);
66-
67-
writer.flush();
6863
} catch (IOException e) {
6964
throw new MojoExecutionException("Cannot write output file: " + outfile, e);
70-
} finally {
71-
if (writer != null) {
72-
try {
73-
writer.close();
74-
} catch (IOException e) {
75-
// ignore
76-
}
77-
}
7865
}
7966
}
8067
}

its/core-it-support/core-it-plugins/maven-it-plugin-log-file/src/main/java/org/apache/maven/plugin/coreit/AbstractLogMojo.java

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -78,25 +78,13 @@ protected void append(Object value) throws MojoExecutionException {
7878
File file = getLogFile();
7979
getLog().info("[MAVEN-CORE-IT-LOG] Updating log file: " + file);
8080
getLog().info("[MAVEN-CORE-IT-LOG] " + value);
81-
try {
82-
file.getParentFile().mkdirs();
83-
OutputStream out = new FileOutputStream(file, true);
84-
try {
85-
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, encoding));
86-
if (value != null) {
87-
writer.write(value.toString());
88-
writer.newLine();
89-
writer.flush();
90-
}
91-
} finally {
92-
try {
93-
out.close();
94-
} catch (IOException e) {
95-
// just ignore, we tried our best to clean up
96-
}
81+
if (value != null && file.getParentFile().mkdirs()) {
82+
try (BufferedWriter w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), encoding))) {
83+
w.write(value.toString());
84+
w.newLine();
85+
} catch (IOException e) {
86+
throw new MojoExecutionException("Failed to update log file " + logFile, e);
9787
}
98-
} catch (IOException e) {
99-
throw new MojoExecutionException("Failed to update log file " + logFile, e);
10088
}
10189
}
10290

@@ -108,19 +96,15 @@ protected void append(Object value) throws MojoExecutionException {
10896
protected void reset() throws MojoExecutionException {
10997
File file = getLogFile();
11098
getLog().info("[MAVEN-CORE-IT-LOG] Resetting log file: " + file);
99+
if(file.getParentFile().mkdirs()){
111100
try {
112101
/*
113102
* NOTE: Intentionally don't delete the file but create a new empty one to check the plugin was executed.
114103
*/
115-
file.getParentFile().mkdirs();
116-
OutputStream out = new FileOutputStream(file);
117-
try {
118-
out.close();
119-
} catch (IOException e) {
120-
// just ignore, we tried our best to clean up
121-
}
104+
new FileOutputStream(file).close();
122105
} catch (IOException e) {
123106
throw new MojoExecutionException("Failed to reset log file " + logFile, e);
124107
}
108+
}
125109
}
126110
}

0 commit comments

Comments
 (0)