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 @@ -295,4 +295,31 @@ private static String getRelativeWorkingDirectory(CompilerConfiguration config)
}
return to;
}

/**
* Joins the configured annotation processor names, skipping blank entries.
* <p>
* Maven maps an explicitly empty {@code <annotationProcessors/>} element to an array holding
* blank strings rather than to an empty array. Passing those on yields {@code -processor} with
* an empty processor name, which no compiler can resolve. Callers should omit the option
* entirely when this returns an empty string.
*
* @param annotationProcessors the configured names, possibly {@code null}
* @return the non-blank names joined by commas, or an empty string if none remain
* @since 2.17.1
*/
protected static String joinAnnotationProcessors(String[] annotationProcessors) {
StringBuilder buffer = new StringBuilder();
if (annotationProcessors != null) {
for (String annotationProcessor : annotationProcessors) {
if (annotationProcessor != null && !annotationProcessor.trim().isEmpty()) {
if (buffer.length() > 0) {
buffer.append(',');
}
buffer.append(annotationProcessor);
}
}
}
return buffer.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,23 +173,16 @@ public CompilerResult performCompile(CompilerConfiguration config) throws Compil
}

// now add jdk 1.6 annotation processing related parameters
String[] annotationProcessors = config.getAnnotationProcessors();
String annotationProcessors = joinAnnotationProcessors(config.getAnnotationProcessors());
List<String> processorPathEntries = config.getProcessorPathEntries();
List<String> processorModulePathEntries = config.getProcessorModulePathEntries();

if ((annotationProcessors != null && annotationProcessors.length > 0)
if (!annotationProcessors.isEmpty()
|| (processorPathEntries != null && processorPathEntries.size() > 0)
|| (processorModulePathEntries != null && processorModulePathEntries.size() > 0)) {
if (annotationProcessors != null && annotationProcessors.length > 0) {
if (!annotationProcessors.isEmpty()) {
args.add("-processor");
StringBuilder sb = new StringBuilder();
for (String ap : annotationProcessors) {
if (sb.length() > 0) {
sb.append(',');
}
sb.append(ap);
}
args.add(sb.toString());
args.add(annotationProcessors);
}

if (processorPathEntries != null && processorPathEntries.size() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,17 +421,10 @@ public static String[] buildCompilerArguments(
if (config.getProc() != null) {
args.add("-proc:" + config.getProc());
}
if (config.getAnnotationProcessors() != null) {
String annotationProcessors = joinAnnotationProcessors(config.getAnnotationProcessors());
if (!annotationProcessors.isEmpty()) {
args.add("-processor");
String[] procs = config.getAnnotationProcessors();
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < procs.length; i++) {
if (i > 0) {
buffer.append(",");
}
buffer.append(procs[i]);
}
args.add(buffer.toString());
args.add(annotationProcessors);
}
if (config.getProcessorPathEntries() != null
&& !config.getProcessorPathEntries().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* @author <a href="mailto:jason@plexus.org">Jason van Zyl</a>
Expand Down Expand Up @@ -200,6 +203,61 @@ public void internalTest(
assertArrayEquals(actualArguments, expectedArguments.toArray(new String[0]));
}

@Test
public void testBuildCompilerArgsEmptyAnnotationProcessors() {
CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
compilerConfiguration.setOutputLocation("/output");
compilerConfiguration.setAnnotationProcessors(new String[0]);

String[] actualArguments = JavacCompiler.buildCompilerArguments(compilerConfiguration, new String[0], "17");

assertFalse(
Arrays.asList(actualArguments).contains("-processor"),
"an empty processor array must not produce -processor with an empty argument");
}

@Test
public void testBuildCompilerArgsBlankAnnotationProcessors() {
CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
compilerConfiguration.setOutputLocation("/output");
compilerConfiguration.setAnnotationProcessors(new String[] {"", ""});

String[] actualArguments = JavacCompiler.buildCompilerArguments(compilerConfiguration, new String[0], "17");

assertFalse(
Arrays.asList(actualArguments).contains("-processor"),
"an array of blank processor names must not produce -processor");
}

@Test
public void testBuildCompilerArgsMixedBlankAnnotationProcessors() {
CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
compilerConfiguration.setOutputLocation("/output");
compilerConfiguration.setAnnotationProcessors(
new String[] {"", "com.example.First", " ", "com.example.Second"});

List<String> actualArguments =
Arrays.asList(JavacCompiler.buildCompilerArguments(compilerConfiguration, new String[0], "17"));

int index = actualArguments.indexOf("-processor");
assertTrue(index >= 0, "-processor must be present when at least one name is not blank");
assertEquals("com.example.First,com.example.Second", actualArguments.get(index + 1));
}

@Test
public void testBuildCompilerArgsAnnotationProcessors() {
CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
compilerConfiguration.setOutputLocation("/output");
compilerConfiguration.setAnnotationProcessors(new String[] {"com.example.First", "com.example.Second"});

List<String> actualArguments =
Arrays.asList(JavacCompiler.buildCompilerArguments(compilerConfiguration, new String[0], "17"));

int index = actualArguments.indexOf("-processor");
assertTrue(index >= 0, "-processor must be present for a non-empty processor array");
assertEquals("com.example.First,com.example.Second", actualArguments.get(index + 1));
}

@Test
public void testBuildCompilerArgs13() {
List<String> expectedArguments = new ArrayList<>();
Expand Down
Loading