Skip to content

Commit 93cf90c

Browse files
committed
Move the formatting of options in the Options class.
1 parent 8e67368 commit 93cf90c

File tree

2 files changed

+62
-32
lines changed

2 files changed

+62
-32
lines changed

src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,7 +1451,7 @@ && getIncrementalExcludes().isEmpty())) {
14511451
if (!success || logger.isDebugEnabled()) {
14521452
IOException suppressed = null;
14531453
try {
1454-
writeDebugFile(compilerConfiguration.options, dependencies, sourceFiles);
1454+
writeDebugFile(compilerConfiguration, dependencies, sourceFiles);
14551455
if (success && tipForCommandLineCompilation != null) {
14561456
logger.debug(tipForCommandLineCompilation);
14571457
tipForCommandLineCompilation = null;
@@ -1767,13 +1767,13 @@ private void writePlugin(MessageBuilder mb, String option, String value) {
17671767
* If a file name contains embedded spaces, then the whole file name must be between double quotation marks.
17681768
* The -J options are not supported.
17691769
*
1770-
* @param options the compiler options
1770+
* @param compilerConfiguration options to provide to the compiler
17711771
* @param dependencies the dependencies
17721772
* @param sourceFiles all files to compile
17731773
* @throws IOException if an error occurred while writing the debug file
17741774
*/
17751775
private void writeDebugFile(
1776-
List<String> options, Map<PathType, List<Path>> dependencies, List<SourceFile> sourceFiles)
1776+
Options compilerConfiguration, Map<PathType, List<Path>> dependencies, List<SourceFile> sourceFiles)
17771777
throws IOException {
17781778
final Path path = getDebugFilePath();
17791779
if (path == null) {
@@ -1784,36 +1784,8 @@ private void writeDebugFile(
17841784
.append(System.lineSeparator())
17851785
.append(" ")
17861786
.append(executable != null ? executable : compilerId);
1787-
boolean hasOptions = false;
17881787
try (BufferedWriter out = Files.newBufferedWriter(path)) {
1789-
for (String option : options) {
1790-
if (option.isBlank()) {
1791-
continue;
1792-
}
1793-
if (option.startsWith("-J")) {
1794-
commandLine.append(' ').append(option);
1795-
continue;
1796-
}
1797-
if (hasOptions) {
1798-
if (option.charAt(0) == '-') {
1799-
out.newLine();
1800-
} else {
1801-
out.write(' ');
1802-
}
1803-
}
1804-
boolean needsQuote = option.indexOf(' ') >= 0;
1805-
if (needsQuote) {
1806-
out.write('"');
1807-
}
1808-
out.write(option);
1809-
if (needsQuote) {
1810-
out.write('"');
1811-
}
1812-
hasOptions = true;
1813-
}
1814-
if (hasOptions) {
1815-
out.newLine();
1816-
}
1788+
compilerConfiguration.format(commandLine, out);
18171789
for (Map.Entry<PathType, List<Path>> entry : dependencies.entrySet()) {
18181790
String separator = "";
18191791
for (String element : entry.getKey().option(entry.getValue())) {

src/main/java/org/apache/maven/plugin/compiler/Options.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import javax.tools.OptionChecker;
2222

23+
import java.io.IOException;
24+
import java.io.UncheckedIOException;
2325
import java.util.ArrayList;
2426
import java.util.Arrays;
2527
import java.util.Collection;
@@ -337,4 +339,60 @@ void addUnchecked(String arguments) {
337339
addUnchecked(Arrays.asList(arguments.split(" ")));
338340
}
339341
}
342+
343+
/**
344+
* Formats the options for debugging purposes.
345+
*
346+
* @param commandLine the prefix where to put the {@code -J} options before all other options
347+
* @param out where to put all options other than {@code -J}
348+
* @throws IOException if an error occurred while writing an option
349+
*/
350+
void format(final StringBuilder commandLine, final Appendable out) throws IOException {
351+
boolean hasOptions = false;
352+
for (String option : options) {
353+
if (option.isBlank()) {
354+
continue;
355+
}
356+
if (option.startsWith("-J")) {
357+
if (commandLine.length() != 0) {
358+
commandLine.append(' ');
359+
}
360+
commandLine.append(option);
361+
continue;
362+
}
363+
if (hasOptions) {
364+
if (option.charAt(0) == '-') {
365+
out.append(System.lineSeparator());
366+
} else {
367+
out.append(' ');
368+
}
369+
}
370+
boolean needsQuote = option.indexOf(' ') >= 0;
371+
if (needsQuote) {
372+
out.append('"');
373+
}
374+
out.append(option);
375+
if (needsQuote) {
376+
out.append('"');
377+
}
378+
hasOptions = true;
379+
}
380+
if (hasOptions) {
381+
out.append(System.lineSeparator());
382+
}
383+
}
384+
385+
/**
386+
* {@return a string representatation of the options for debugging purposes}.
387+
*/
388+
@Override
389+
public String toString() {
390+
var out = new StringBuilder(40);
391+
try {
392+
format(out, out);
393+
} catch (IOException e) {
394+
throw new UncheckedIOException(e);
395+
}
396+
return out.toString();
397+
}
340398
}

0 commit comments

Comments
 (0)