Skip to content
Merged
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 @@ -61,6 +61,7 @@
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticListener;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
Expand Down Expand Up @@ -90,6 +91,8 @@ public class JdkInitializer
*/
private Predicate<URI> registrationFilter = null;

private DiagnosticListener<JavaFileObject> diagnosticListener = d -> {};

private boolean initialized = false;

// Set at the start of initialize() for use by helper methods
Expand Down Expand Up @@ -156,6 +159,11 @@ public static JdkInitializer ofDirectory(final Path directory) {
return new JdkInitializer(List.of(), List.of(directory), List.of());
}

public JdkInitializer withDiagnosticListener(final DiagnosticListener<JavaFileObject> listener) {
this.diagnosticListener = listener;
return this;
}

/**
* Restricts descriptor registration to compilation units whose source URI satisfies the given
* predicate. Units that do not match are still compiled (providing type resolution context)
Expand Down Expand Up @@ -185,15 +193,14 @@ public void initialize(final CodeModel codeModel) {
this.codeModel = codeModel;
this.nameProvider = codeModel.getNameProvider();
final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null)) {
try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticListener, null, null)) {

final List<JavaFileObject> combined = collectSources(fileManager);
if (combined.isEmpty()) {
return;
}

final var task = compiler.getTask(null, fileManager, diagnostic -> {
}, buildOptions(), null, combined);
final var task = compiler.getTask(null, fileManager, diagnosticListener, buildOptions(), null, combined);
final var javacTask = (JavacTask) task;
final var compilationUnits = javacTask.parse();
javacTask.analyze();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;
import javax.tools.ToolProvider;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -191,6 +194,27 @@ public class Foo {
assertThat(field.type()).isInstanceOf(UnknownTypeUsage.class);
}

@Test
void shouldForwardDiagnosticsToCustomListener() {
final var source = JavaFileObjects.forSourceString(
"com.example.Broken",
"""
package com.example;
public class Broken {
private com.example.Missing dep;
}
""");

final List<Diagnostic<? extends JavaFileObject>> captured = new ArrayList<>();
runInternal(
new JdkInitializer(List.of(), List.of(), List.of(source))
.withDiagnosticListener(captured::add));

assertThat(captured)
.as("listener installed via withDiagnosticListener must receive javac diagnostics")
.isNotEmpty();
}

@Test
void shouldResolveTypesFromClasspath() throws Exception {
// Compile a helper class to a temp directory, then pass that directory as the classpath.
Expand Down