Skip to content

Commit

Permalink
Fix some errors that occur when importing o.e.j.c incubator
Browse files Browse the repository at this point in the history
- Do not reuse the context when compiling to multiple output directories
- Implement Alex's idea of a separate file of javac-specific error codes

Signed-off-by: David Thompson <[email protected]>
  • Loading branch information
datho7561 authored and mickaelistria committed Nov 13, 2024
1 parent 89eb53f commit b491c7f
Showing 1 changed file with 31 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,26 +72,7 @@ public JavacCompiler(INameEnvironment environment, IErrorHandlingPolicy policy,

@Override
public void compile(ICompilationUnit[] sourceUnits) {
Context javacContext = new Context();
Map<ICompilationUnit, List<IProblem>> javacProblems = new HashMap<>();
JavacProblemConverter problemConverter = new JavacProblemConverter(this.compilerConfig.compilerOptions(), javacContext);
javacContext.put(DiagnosticListener.class, diagnostic -> {
if (diagnostic.getSource() instanceof JavaFileObject fileObject) {
JavacProblem javacProblem = problemConverter.createJavacProblem(diagnostic);
if (javacProblem != null) {
ICompilationUnit originalUnit = this.fileObjectToCUMap.get(fileObject);
if (originalUnit == null) {
return;
}
List<IProblem> previous = javacProblems.get(originalUnit);
if (previous == null) {
previous = new ArrayList<>();
javacProblems.put(originalUnit, previous);
}
previous.add(javacProblem);
}
}
});

IJavaProject javaProject = Stream.of(sourceUnits).filter(SourceFile.class::isInstance).map(
SourceFile.class::cast).map(source -> source.resource).map(IResource::getProject).filter(
Expand Down Expand Up @@ -120,18 +101,39 @@ public void compile(ICompilationUnit[] sourceUnits) {

// Register listener to intercept intermediate results from Javac task.
JavacTaskListener javacListener = new JavacTaskListener(this.compilerConfig, outputSourceMapping, this.problemFactory, this.fileObjectToCUMap);
MultiTaskListener mtl = MultiTaskListener.instance(javacContext);
mtl.add(javacListener);
mtl.add(new TaskListener() {
@Override
public void finished(TaskEvent e) {
if (e.getSourceFile() != null && fileObjectToCUMap.get(e.getSourceFile()) instanceof JCCompilationUnit u) {
problemConverter.registerUnit(e.getSourceFile(), u);
}
}
});

for (Entry<IContainer, List<ICompilationUnit>> outputSourceSet : outputSourceMapping.entrySet()) {

Context javacContext = new Context();
JavacProblemConverter problemConverter = new JavacProblemConverter(this.compilerConfig.compilerOptions(), javacContext);
javacContext.put(DiagnosticListener.class, diagnostic -> {
if (diagnostic.getSource() instanceof JavaFileObject fileObject) {
JavacProblem javacProblem = problemConverter.createJavacProblem(diagnostic);
if (javacProblem != null) {
ICompilationUnit originalUnit = this.fileObjectToCUMap.get(fileObject);
if (originalUnit == null) {
return;
}
List<IProblem> previous = javacProblems.get(originalUnit);
if (previous == null) {
previous = new ArrayList<>();
javacProblems.put(originalUnit, previous);
}
previous.add(javacProblem);
}
}
});
MultiTaskListener mtl = MultiTaskListener.instance(javacContext);
mtl.add(javacListener);
mtl.add(new TaskListener() {
@Override
public void finished(TaskEvent e) {
if (e.getSourceFile() != null && fileObjectToCUMap.get(e.getSourceFile()) instanceof JCCompilationUnit u) {
problemConverter.registerUnit(e.getSourceFile(), u);
}
}
});

// Configure Javac to generate the class files in a mapped temporary location
var outputDir = JavacClassFile.getMappedTempOutput(outputSourceSet.getKey()).toFile();
javacListener.setOutputDir(outputSourceSet.getKey());
Expand Down Expand Up @@ -175,7 +177,6 @@ public int errorCount() {
return this.isInGeneration ? 0 : super.errorCount();
}
};
javacContext.put(JavaCompiler.compilerKey, javac);
javac.shouldStopPolicyIfError = CompileState.GENERATE;
JavacFileManager fileManager = (JavacFileManager)javacContext.get(JavaFileManager.class);
try {
Expand Down

0 comments on commit b491c7f

Please sign in to comment.