Skip to content

Commit

Permalink
Support annotationProcessors in DevMojo
Browse files Browse the repository at this point in the history
  • Loading branch information
FroMage committed Jan 12, 2024
1 parent 2dfb8dc commit f61ed48
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class Context {
private final boolean ignoreModuleInfo;
private final File generatedSourcesDirectory;
private final Set<File> annotationProcessorPaths;
private final List<String> annotationProcessors;

public Context(
String name,
Expand All @@ -70,6 +71,7 @@ public Context(
List<String> compilerPluginOptions,
File generatedSourcesDirectory,
Set<File> annotationProcessorPaths,
List<String> annotationProcessors,
String ignoreModuleInfo) {
this.name = name;
this.classpath = classpath;
Expand All @@ -87,6 +89,7 @@ public Context(
this.ignoreModuleInfo = Boolean.parseBoolean(ignoreModuleInfo);
this.generatedSourcesDirectory = generatedSourcesDirectory;
this.annotationProcessorPaths = annotationProcessorPaths;
this.annotationProcessors = annotationProcessors;
}

public String getName() {
Expand All @@ -105,6 +108,10 @@ public Set<File> getAnnotationProcessorPaths() {
return annotationProcessorPaths;
}

public List<String> getAnnotationProcessors() {
return annotationProcessors;
}

public File getProjectDirectory() {
return projectDirectory;
}
Expand Down Expand Up @@ -148,7 +155,7 @@ public List<String> getCompilerPluginOptions() {
public boolean ignoreModuleInfo() {
return ignoreModuleInfo;
}

public File getGeneratedSourcesDirectory() {
return generatedSourcesDirectory;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,22 @@ public class CompilerFlags {
private final String releaseJavaVersion; //can be null
private final String sourceJavaVersion; //can be null
private final String targetJavaVersion; //can be null
private final List<String> annotationProcessors; //can be null

public CompilerFlags(
Set<String> defaultFlags,
Collection<String> userFlags,
String releaseJavaVersion,
String sourceJavaVersion,
String targetJavaVersion) {
String targetJavaVersion,
List<String> annotationProcessors) {

this.defaultFlags = defaultFlags == null ? new LinkedHashSet<>() : new LinkedHashSet<>(defaultFlags);
this.userFlags = userFlags == null ? new ArrayList<>() : new ArrayList<>(userFlags);
this.releaseJavaVersion = releaseJavaVersion;
this.sourceJavaVersion = sourceJavaVersion;
this.targetJavaVersion = targetJavaVersion;
this.annotationProcessors = annotationProcessors;
}

public List<String> toList() {
Expand Down Expand Up @@ -61,6 +64,11 @@ public List<String> toList() {
}
}

if (annotationProcessors != null && !annotationProcessors.isEmpty()) {
flagList.add("-processor");
flagList.add(String.join(",", annotationProcessors));
}

flagList.addAll(userFlags);

return flagList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public class DevModeContext implements Serializable {
private final Set<ArtifactKey> localArtifacts = new HashSet<>();

private Set<File> processorPaths;
private List<String> processors;

public boolean isLocalProjectDiscovery() {
return localProjectDiscovery;
Expand Down Expand Up @@ -249,6 +250,14 @@ public Set<File> getAnnotationProcessorPaths() {
return processorPaths;
}

public void setAnnotationProcessors(List<String> processors) {
this.processors = processors;
}

public List<String> getAnnotationProcessors() {
return processors;
}

public static class ModuleInfo implements Serializable {

private static final long serialVersionUID = -1376678003747618410L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public void compile(Set<File> filesToCompile, CompilationProvider.Context contex
context.getCompilerOptions(PROVIDER_KEY),
context.getReleaseJavaVersion(),
context.getSourceJavaVersion(),
context.getTargetJvmVersion()).toList();
context.getTargetJvmVersion(),
context.getAnnotationProcessors()).toList();
}

final JavaCompiler compiler = this.compiler;
Expand All @@ -74,7 +75,7 @@ public void compile(Set<File> filesToCompile, CompilationProvider.Context contex

final QuarkusFileManager.Context sourcesContext = new QuarkusFileManager.Context(
context.getClasspath(), context.getReloadableClasspath(),
context.getOutputDirectory(), context.getGeneratedSourcesDirectory(),
context.getOutputDirectory(), context.getGeneratedSourcesDirectory(),
context.getAnnotationProcessorPaths(),
context.getSourceEncoding(),
context.ignoreModuleInfo());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ public void setupSourceCompilationContext(DevModeContext context,
compilationUnit.getGeneratedSourcesPath() == null ? null
: new File(compilationUnit.getGeneratedSourcesPath()),
context.getAnnotationProcessorPaths(),
context.getAnnotationProcessors(),
context.getBuildSystemProperties().getOrDefault("quarkus.live-reload.ignore-module-info",
"true")));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@ public B annotationProcessorPaths(Set<File> processorPaths) {
return (B) this;
}

@SuppressWarnings("unchecked")
public B annotationProcessors(List<String> processors) {
QuarkusDevModeLauncher.this.processors = processors;
return (B) this;
}

@SuppressWarnings("unchecked")
public B releaseJavaVersion(String releaseJavaVersion) {
QuarkusDevModeLauncher.this.releaseJavaVersion = releaseJavaVersion;
Expand Down Expand Up @@ -324,6 +330,7 @@ public R build() throws Exception {
private List<ModuleInfo> dependencies = new ArrayList<>(0);
private LinkedHashMap<ArtifactKey, File> classpath = new LinkedHashMap<>();
private Set<File> processorPaths;
private List<String> processors;

protected QuarkusDevModeLauncher() {
}
Expand Down Expand Up @@ -452,6 +459,9 @@ protected void prepare() throws Exception {
if (processorPaths != null) {
devModeContext.setAnnotationProcessorPaths(processorPaths);
}
if (processors != null) {
devModeContext.setAnnotationProcessors(processors);
}

devModeContext.setReleaseJavaVersion(releaseJavaVersion);
devModeContext.setSourceJavaVersion(sourceJavaVersion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static class Context {
private final Set<File> annotationProcessorPaths;

public Context(Set<File> classPath, Set<File> reloadableClassPath,
File outputDirectory, File generatedSourcesDirectory, Set<File> annotationProcessorPaths,
File outputDirectory, File generatedSourcesDirectory, Set<File> annotationProcessorPaths,
Charset sourceEncoding, boolean ignoreModuleInfo) {
this.classPath = classPath;
this.reloadableClassPath = reloadableClassPath;
Expand Down Expand Up @@ -95,7 +95,7 @@ public Charset getSourceEncoding() {
public boolean ignoreModuleInfo() {
return ignoreModuleInfo;
}

public File getGeneratedSourcesDirectory() {
return generatedSourcesDirectory;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,77 +16,77 @@ public class CompilerFlagsTest {
void nullHandling() {
assertAll(
() -> assertEquals(
new CompilerFlags(null, null, null, null, null),
new CompilerFlags(setOf(), listOf(), null, null, null)));
new CompilerFlags(null, null, null, null, null, null),
new CompilerFlags(setOf(), listOf(), null, null, null, null)));
}

@Test
void defaulting() {
assertAll(
() -> assertEquals(
new CompilerFlags(setOf("-a", "-b"), listOf(), null, null, null),
new CompilerFlags(setOf(), listOf("-a", "-b"), null, null, null)),
new CompilerFlags(setOf("-a", "-b"), listOf(), null, null, null, null),
new CompilerFlags(setOf(), listOf("-a", "-b"), null, null, null, null)),
() -> assertEquals(
new CompilerFlags(setOf("-a", "-b"), listOf("-c", "-d"), null, null, null),
new CompilerFlags(setOf(), listOf("-a", "-b", "-c", "-d"), null, null, null)));
new CompilerFlags(setOf("-a", "-b"), listOf("-c", "-d"), null, null, null, null),
new CompilerFlags(setOf(), listOf("-a", "-b", "-c", "-d"), null, null, null, null)));
}

@Test
void redundancyReduction() {
assertAll(
() -> assertEquals(
new CompilerFlags(setOf("-a", "-b"), listOf(), null, null, null),
new CompilerFlags(setOf(), listOf("-a", "-b"), null, null, null)),
new CompilerFlags(setOf("-a", "-b"), listOf(), null, null, null, null),
new CompilerFlags(setOf(), listOf("-a", "-b"), null, null, null, null)),
() -> assertEquals(
new CompilerFlags(setOf("-a", "-b", "-c"), listOf("-a", "-b"), null, null, null),
new CompilerFlags(setOf("-c"), listOf("-a", "-b"), null, null, null)));
new CompilerFlags(setOf("-a", "-b", "-c"), listOf("-a", "-b"), null, null, null, null),
new CompilerFlags(setOf("-c"), listOf("-a", "-b"), null, null, null, null)));
}

@Test
void sourceAndTarget() {
assertAll(
() -> assertEquals(
new CompilerFlags(setOf(), listOf(), "1", null, null),
new CompilerFlags(setOf(), listOf("--release", "1"), null, null, null)),
new CompilerFlags(setOf(), listOf(), "1", null, null, null),
new CompilerFlags(setOf(), listOf("--release", "1"), null, null, null, null)),
() -> assertEquals(
new CompilerFlags(setOf(), listOf(), null, "2", null),
new CompilerFlags(setOf(), listOf("-source", "2"), null, null, null)),
new CompilerFlags(setOf(), listOf(), null, "2", null, null),
new CompilerFlags(setOf(), listOf("-source", "2"), null, null, null, null)),
() -> assertEquals(
new CompilerFlags(setOf(), listOf(), null, null, "3"),
new CompilerFlags(setOf(), listOf("-target", "3"), null, null, null)),
new CompilerFlags(setOf(), listOf(), null, null, "3", null),
new CompilerFlags(setOf(), listOf("-target", "3"), null, null, null, null)),
() -> assertEquals(
new CompilerFlags(setOf(), listOf(), "1", "2", "3"),
new CompilerFlags(setOf(), listOf("--release", "1"), null, null, null)),
new CompilerFlags(setOf(), listOf(), "1", "2", "3", null),
new CompilerFlags(setOf(), listOf("--release", "1"), null, null, null, null)),
() -> assertEquals(
new CompilerFlags(setOf(), listOf(), null, "2", "3"),
new CompilerFlags(setOf(), listOf("-source", "2", "-target", "3"), null, null, null)),
new CompilerFlags(setOf(), listOf(), null, "2", "3", null),
new CompilerFlags(setOf(), listOf("-source", "2", "-target", "3"), null, null, null, null)),
() -> assertEquals(
new CompilerFlags(setOf(), listOf("-source", "5", "-target", "6"), null, "2", "3"),
new CompilerFlags(setOf(), listOf("-source", "5", "-target", "6"), null, "2", "3", null),
new CompilerFlags(setOf(), listOf("-source", "2", "-target", "3", "-source", "5", "-target", "6"),
null, null, null)));
null, null, null, null)));
}

@Test
void allFeatures() {
assertAll(
() -> assertEquals(
new CompilerFlags(setOf("-b", "-c", "-d"), listOf("-a", "-b", "-c"), "1", "2", "3"),
new CompilerFlags(setOf(), listOf("-d", "--release", "1", "-a", "-b", "-c"), null, null, null)));
new CompilerFlags(setOf("-b", "-c", "-d"), listOf("-a", "-b", "-c"), "1", "2", "3", null),
new CompilerFlags(setOf(), listOf("-d", "--release", "1", "-a", "-b", "-c"), null, null, null, null)));
assertAll(
() -> assertEquals(
new CompilerFlags(setOf("-b", "-c", "-d"), listOf("-a", "-b", "-c"), null, "2", "3"),
new CompilerFlags(setOf("-b", "-c", "-d"), listOf("-a", "-b", "-c"), null, "2", "3", null),
new CompilerFlags(setOf(), listOf("-d", "-source", "2", "-target", "3", "-a", "-b", "-c"),
null, null, null)));
null, null, null, null)));
}

@Test
void listConversion() {
assertAll(
() -> assertEquals(
new CompilerFlags(null, null, null, null, null).toList(),
new CompilerFlags(null, null, null, null, null, null).toList(),
listOf()),
() -> assertEquals(
new CompilerFlags(setOf(), listOf("-a", "-b", "-c", "-d"), null, null, null).toList(),
new CompilerFlags(setOf(), listOf("-a", "-b", "-c", "-d"), null, null, null, null).toList(),
listOf("-a", "-b", "-c", "-d")));
}

Expand Down
21 changes: 21 additions & 0 deletions devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,22 @@ private void executeGoal(PluginExec pluginExec, String goal, Map<String, String>
pluginManager));
}

private List<String> readAnnotationProcessors(Xpp3Dom pluginConfig) {
Xpp3Dom annotationProcessors = pluginConfig.getChild("annotationProcessors");
if (annotationProcessors == null) {
return Collections.emptyList();
}
Xpp3Dom[] processors = annotationProcessors.getChildren("annotationProcessor");
if (processors.length == 0) {
return Collections.emptyList();
}
List<String> ret = new ArrayList<>(processors.length);
for (Xpp3Dom processor : processors) {
ret.add(processor.getValue());
}
return ret;
}

private Set<File> readAnnotationProcessorPaths(Xpp3Dom pluginConfig) throws MojoExecutionException {
Xpp3Dom annotationProcessorPaths = pluginConfig.getChild("annotationProcessorPaths");
if (annotationProcessorPaths == null) {
Expand Down Expand Up @@ -1507,6 +1523,11 @@ private void setAnnotationProcessorFlags(MavenDevModeLauncher.Builder builder) {
} catch (MojoExecutionException e) {
throw new RuntimeException(e);
}
List<String> processors = this.readAnnotationProcessors(compilerPluginConfiguration);
getLog().debug("Found processors: " + processors);
if (!processors.isEmpty()) {
builder.annotationProcessors(processors);
}
builder.compilerPluginOptions(options);
}

Expand Down

0 comments on commit f61ed48

Please sign in to comment.