Skip to content

Commit

Permalink
Merge pull request #37044 from FroMage/apt
Browse files Browse the repository at this point in the history
Better support for APT in dev mode
  • Loading branch information
FroMage authored Jan 16, 2024
2 parents 1a1bce7 + b1f9947 commit 3d093f0
Show file tree
Hide file tree
Showing 35 changed files with 1,055 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class Context {
private final List<String> compilePluginArtifacts;
private final List<String> compilerPluginOptions;
private final boolean ignoreModuleInfo;
private final File generatedSourcesDirectory;
private final Set<File> annotationProcessorPaths;
private final List<String> annotationProcessors;

public Context(
String name,
Expand All @@ -65,7 +68,11 @@ public Context(
String sourceJavaVersion,
String targetJvmVersion,
List<String> compilePluginArtifacts,
List<String> compilerPluginOptions, String ignoreModuleInfo) {
List<String> compilerPluginOptions,
File generatedSourcesDirectory,
Set<File> annotationProcessorPaths,
List<String> annotationProcessors,
String ignoreModuleInfo) {
this.name = name;
this.classpath = classpath;
this.reloadableClasspath = reloadableClasspath;
Expand All @@ -80,6 +87,9 @@ public Context(
this.compilePluginArtifacts = compilePluginArtifacts;
this.compilerPluginOptions = compilerPluginOptions;
this.ignoreModuleInfo = Boolean.parseBoolean(ignoreModuleInfo);
this.generatedSourcesDirectory = generatedSourcesDirectory;
this.annotationProcessorPaths = annotationProcessorPaths;
this.annotationProcessors = annotationProcessors;
}

public String getName() {
Expand All @@ -94,6 +104,14 @@ public Set<File> getReloadableClasspath() {
return reloadableClasspath;
}

public Set<File> getAnnotationProcessorPaths() {
return annotationProcessorPaths;
}

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

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

public File getGeneratedSourcesDirectory() {
return generatedSourcesDirectory;
}
}
}
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 @@ -30,7 +30,7 @@ public class DevModeContext implements Serializable {

private static final long serialVersionUID = 4688502145533897982L;

public static final CompilationUnit EMPTY_COMPILATION_UNIT = new CompilationUnit(PathList.of(), null, null, null);
public static final CompilationUnit EMPTY_COMPILATION_UNIT = new CompilationUnit(PathList.of(), null, null, null, null);

public static final String ENABLE_PREVIEW_FLAG = "--enable-preview";

Expand Down Expand Up @@ -64,6 +64,9 @@ public class DevModeContext implements Serializable {
private String baseName;
private final Set<ArtifactKey> localArtifacts = new HashSet<>();

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

public boolean isLocalProjectDiscovery() {
return localProjectDiscovery;
}
Expand Down Expand Up @@ -239,6 +242,22 @@ public Set<ArtifactKey> getLocalArtifacts() {
return localArtifacts;
}

public void setAnnotationProcessorPaths(Set<File> processorPaths) {
this.processorPaths = processorPaths;
}

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 All @@ -259,11 +278,13 @@ public static class ModuleInfo implements Serializable {
this.projectDirectory = builder.projectDirectory;
this.main = new CompilationUnit(builder.sourcePaths, builder.classesPath,
builder.resourcePaths,
builder.resourcesOutputPath);
builder.resourcesOutputPath,
builder.generatedSourcesPath);

if (builder.testClassesPath != null) {
// FIXME: do tests have generated sources?
this.test = new CompilationUnit(builder.testSourcePaths,
builder.testClassesPath, builder.testResourcePaths, builder.testResourcesOutputPath);
builder.testClassesPath, builder.testResourcePaths, builder.testResourcesOutputPath, null);
} else {
this.test = null;
}
Expand Down Expand Up @@ -328,6 +349,7 @@ public static class Builder {
private String classesPath;
private PathCollection resourcePaths = PathList.of();
private String resourcesOutputPath;
private String generatedSourcesPath;

private String preBuildOutputDir;
private PathCollection sourceParents = PathList.of();
Expand Down Expand Up @@ -408,6 +430,11 @@ public Builder setTestResourcesOutputPath(String testResourcesOutputPath) {
return this;
}

public Builder setGeneratedSourcesPath(String generatedSourcesPath) {
this.generatedSourcesPath = generatedSourcesPath;
return this;
}

public ModuleInfo build() {
return new ModuleInfo(this);
}
Expand All @@ -422,13 +449,15 @@ public static class CompilationUnit implements Serializable {
private final String classesPath;
private final PathCollection resourcePaths;
private final String resourcesOutputPath;
private final String generatedSourcesPath;

public CompilationUnit(PathCollection sourcePaths, String classesPath, PathCollection resourcePaths,
String resourcesOutputPath) {
String resourcesOutputPath, String generatedSourcesPath) {
this.sourcePaths = sourcePaths;
this.classesPath = classesPath;
this.resourcePaths = resourcePaths;
this.resourcesOutputPath = resourcesOutputPath;
this.generatedSourcesPath = generatedSourcesPath;
}

public PathCollection getSourcePaths() {
Expand All @@ -446,6 +475,10 @@ public PathCollection getResourcePaths() {
public String getResourcesOutputPath() {
return resourcesOutputPath;
}

public String getGeneratedSourcesPath() {
return generatedSourcesPath;
}
}

public boolean isEnablePreview() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ private void terminateIfRunning() {
private DevModeContext.ModuleInfo toModule(ResolvedDependency module) throws BootstrapGradleException {

String classesDir = null;
String generatedSourcesDir = null;
final Set<Path> sourceParents = new LinkedHashSet<>();
final PathList.Builder srcPaths = PathList.builder();
final ArtifactSources sources = module.getSources();
Expand All @@ -99,6 +100,9 @@ private DevModeContext.ModuleInfo toModule(ResolvedDependency module) throws Boo
if (classesDir == null) {
classesDir = src.getOutputDir().toString();
}
if (generatedSourcesDir == null && src.getAptSourcesDir() != null) {
generatedSourcesDir = src.getAptSourcesDir().toString();
}
}

String resourceDirectory = null;
Expand All @@ -120,6 +124,7 @@ private DevModeContext.ModuleInfo toModule(ResolvedDependency module) throws Boo
.setProjectDirectory(module.getWorkspaceModule().getModuleDir().getPath())
.setSourcePaths(srcPaths.build())
.setClassesPath(classesDir)
.setGeneratedSourcesPath(generatedSourcesDir)
.setResourcePaths(resourcesPaths.build())
.setResourcesOutputPath(resourceDirectory)
.setSourceParents(PathList.from(sourceParents))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,14 @@ public void run() {
}, "Quarkus Shutdown Thread");
Runtime.getRuntime().addShutdownHook(shutdownThread);
} catch (Exception e) {
close();
throw (e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e));
RuntimeException toThrow = (e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e));
try {
close();
} catch (IllegalStateException x) {
// not sure which is the most important, but let's not silence the original exception
toThrow.addSuppressed(x);
}
throw toThrow;
}
}
}
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,9 @@ public void compile(Set<File> filesToCompile, CompilationProvider.Context contex

final QuarkusFileManager.Context sourcesContext = new QuarkusFileManager.Context(
context.getClasspath(), context.getReloadableClasspath(),
context.getOutputDirectory(), context.getSourceEncoding(),
context.getOutputDirectory(), context.getGeneratedSourcesDirectory(),
context.getAnnotationProcessorPaths(),
context.getSourceEncoding(),
context.ignoreModuleInfo());

if (this.fileManager == null) {
Expand Down Expand Up @@ -183,4 +186,4 @@ public Path getSourceFileForClass(final Path classFilePath) {
return null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ public void setupSourceCompilationContext(DevModeContext context,
context.getTargetJvmVersion(),
context.getCompilerPluginArtifacts(),
context.getCompilerPluginsOptions(),
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 @@ -181,6 +181,18 @@ public B compilerPluginOptions(List<String> options) {
return (B) this;
}

@SuppressWarnings("unchecked")
public B annotationProcessorPaths(Set<File> processorPaths) {
QuarkusDevModeLauncher.this.processorPaths = 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 @@ -317,6 +329,8 @@ public R build() throws Exception {
private ModuleInfo main;
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 @@ -442,6 +456,12 @@ protected void prepare() throws Exception {
if (compilerPluginOptions != null) {
devModeContext.setCompilerPluginsOptions(compilerPluginOptions);
}
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 @@ -18,6 +18,12 @@ protected QuarkusFileManager(StandardJavaFileManager fileManager, Context contex
try {
this.fileManager.setLocation(StandardLocation.CLASS_PATH, context.getClassPath());
this.fileManager.setLocation(StandardLocation.CLASS_OUTPUT, List.of(context.getOutputDirectory()));
if (context.getGeneratedSourcesDirectory() != null) {
this.fileManager.setLocation(StandardLocation.SOURCE_OUTPUT, List.of(context.getGeneratedSourcesDirectory()));
}
if (context.getAnnotationProcessorPaths() != null) {
this.fileManager.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH, context.getAnnotationProcessorPaths());
}
} catch (IOException e) {
throw new RuntimeException("Cannot initialize file manager", e);
}
Expand All @@ -29,6 +35,12 @@ public void reset(Context context) {
try {
this.fileManager.setLocation(StandardLocation.CLASS_PATH, context.getClassPath());
this.fileManager.setLocation(StandardLocation.CLASS_OUTPUT, List.of(context.getOutputDirectory()));
if (context.getGeneratedSourcesDirectory() != null) {
this.fileManager.setLocation(StandardLocation.SOURCE_OUTPUT, List.of(context.getGeneratedSourcesDirectory()));
}
if (context.getAnnotationProcessorPaths() != null) {
this.fileManager.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH, context.getAnnotationProcessorPaths());
}
} catch (IOException e) {
throw new RuntimeException("Cannot reset file manager", e);
}
Expand All @@ -45,14 +57,23 @@ public static class Context {
private final File outputDirectory;
private final Charset sourceEncoding;
private final boolean ignoreModuleInfo;
private final File generatedSourcesDirectory;
private final Set<File> annotationProcessorPaths;

public Context(Set<File> classPath, Set<File> reloadableClassPath,
File outputDirectory, Charset sourceEncoding, boolean ignoreModuleInfo) {
File outputDirectory, File generatedSourcesDirectory, Set<File> annotationProcessorPaths,
Charset sourceEncoding, boolean ignoreModuleInfo) {
this.classPath = classPath;
this.reloadableClassPath = reloadableClassPath;
this.outputDirectory = outputDirectory;
this.sourceEncoding = sourceEncoding;
this.ignoreModuleInfo = ignoreModuleInfo;
this.generatedSourcesDirectory = generatedSourcesDirectory;
this.annotationProcessorPaths = annotationProcessorPaths;
}

public Set<File> getAnnotationProcessorPaths() {
return annotationProcessorPaths;
}

public Set<File> getClassPath() {
Expand All @@ -74,5 +95,9 @@ public Charset getSourceEncoding() {
public boolean ignoreModuleInfo() {
return ignoreModuleInfo;
}

public File getGeneratedSourcesDirectory() {
return generatedSourcesDirectory;
}
}
}
Loading

0 comments on commit 3d093f0

Please sign in to comment.