Skip to content

Commit

Permalink
Enable compilation progress reporting and cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
mickaelistria committed Nov 29, 2024
1 parent f8014fb commit 7cbf75d
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*******************************************************************************
* Copyright (c) 2024 Red Hat, Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.jdt.internal.javac;

import java.lang.reflect.Field;

import org.eclipse.core.runtime.ILog;
import org.eclipse.jdt.core.compiler.CompilationProgress;
import org.eclipse.jdt.internal.compiler.ICompilerRequestor;
import org.eclipse.jdt.internal.core.builder.AbstractImageBuilder;
import org.eclipse.jdt.internal.core.builder.BuildNotifier;

/*******************************************************************************
* Copyright (c) 2024 Red Hat, Inc. and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
public class BuildNotifierCompilationProgress extends CompilationProgress {

private BuildNotifier buildNotifier;

public BuildNotifierCompilationProgress(ICompilerRequestor requestor) {
this.buildNotifier = findBuildNotifier(requestor);
}

private BuildNotifier findBuildNotifier(ICompilerRequestor requestor) {
if (requestor instanceof AbstractImageBuilder) {
try {
Field notifierField = AbstractImageBuilder.class.getDeclaredField("notifier");
notifierField.setAccessible(true);
return notifierField.get(requestor) instanceof BuildNotifier notifier ? notifier : null;
} catch (Exception ex) {
ILog.get().warn(ex.getMessage(), ex);
}
}
return null;
}

@Override
public void begin(int remainingWork) {
if (this.buildNotifier != null) {
this.buildNotifier.checkCancelWithinCompiler();
}
}

@Override
public void done() {
// do not forward as done() is sent via requestor
}

@Override
public boolean isCanceled() {
if (this.buildNotifier != null) {
this.buildNotifier.checkCancel();
}
return false;
}

@Override
public void setTaskName(String name) {
if (this.buildNotifier != null) {
this.buildNotifier.subTask(name);
}
}

@Override
public void worked(int workIncrement, int remainingWork) {
// TODO Auto-generated method stub
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.eclipse.jdt.internal.compiler.IProblemFactory;
import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
import org.eclipse.jdt.internal.compiler.env.INameEnvironment;
import org.eclipse.jdt.internal.compiler.problem.AbortCompilation;
import org.eclipse.jdt.internal.core.JavaProject;
import org.eclipse.jdt.internal.core.builder.SourceFile;

Expand Down Expand Up @@ -70,6 +71,8 @@ public JavacCompiler(INameEnvironment environment, IErrorHandlingPolicy policy,
super(environment, policy, compilerConfig.compilerOptions(), requestor, problemFactory);
this.compilerConfig = JavacConfig.createFrom(compilerConfig);
this.problemFactory = problemFactory;
// next is ugly workaround for https://github.com/eclipse-jdt/eclipse.jdt.core/issues/3370
this.progress = new BuildNotifierCompilationProgress(requestor);
}

@Override
Expand Down Expand Up @@ -102,7 +105,7 @@ public void compile(ICompilationUnit[] sourceUnits) {
.collect(Collectors.groupingBy(this::computeOutputDirectory));

// Register listener to intercept intermediate results from Javac task.
JavacTaskListener javacListener = new JavacTaskListener(this.compilerConfig, outputSourceMapping, this.problemFactory, this.fileObjectToCUMap);
JavacTaskListener javacListener = new JavacTaskListener(this, this.compilerConfig, outputSourceMapping, this.problemFactory, this.fileObjectToCUMap);
int unitIndex = 0;
for (Entry<IContainer, List<ICompilationUnit>> outputSourceSet : outputSourceMapping.entrySet()) {
Context javacContext = new Context();
Expand Down Expand Up @@ -149,11 +152,14 @@ public void finished(TaskEvent e) {
JavaCompiler javac = new JavaCompiler(javacContext) {
boolean isInGeneration = false;


@Override
public void generate(Queue<Pair<Env<AttrContext>, JCClassDecl>> queue, Queue<JavaFileObject> results) {
try {
this.isInGeneration = true;
super.generate(queue, results);
} catch (AbortCompilation abort) {
throw abort;
} catch (Throwable ex) {
ILog.get().error(ex.getMessage(), ex);
} finally {
Expand All @@ -165,6 +171,8 @@ public void generate(Queue<Pair<Env<AttrContext>, JCClassDecl>> queue, Queue<Jav
protected void desugar(Env<AttrContext> env, Queue<Pair<Env<AttrContext>, JCClassDecl>> results) {
try {
super.desugar(env, results);
} catch (AbortCompilation abort) {
throw abort;
} catch (Throwable ex) {
ILog.get().error(ex.getMessage(), ex);
}
Expand Down Expand Up @@ -260,4 +268,9 @@ private IContainer computeOutputDirectory(ICompilationUnit unit) {
}
return null;
}

@Override
public void reportProgress(String taskDecription) {
super.reportProgress(taskDecription);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public class JavacTaskListener implements TaskListener {
private JavacConfig config;
private IContainer outputDir;
private final Map<JavaFileObject, ICompilationUnit> fileObjectToCUMap;
private final JavacCompiler javacCompiler;
private static final Set<String> PRIMITIVE_TYPES = new HashSet<String>(Arrays.asList(
"byte",
"short",
Expand All @@ -82,8 +83,9 @@ public class JavacTaskListener implements TaskListener {

private static final char[] MODULE_INFO_NAME = "module-info".toCharArray();

public JavacTaskListener(JavacConfig config, Map<IContainer, List<ICompilationUnit>> outputSourceMapping,
public JavacTaskListener(JavacCompiler javacCompiler, JavacConfig config, Map<IContainer, List<ICompilationUnit>> outputSourceMapping,
IProblemFactory problemFactory, Map<JavaFileObject, ICompilationUnit> fileObjectToCUMap) {
this.javacCompiler = javacCompiler;
this.config = config;
this.problemFactory = new UnusedProblemFactory(problemFactory, config.compilerOptions());
this.fileObjectToCUMap = fileObjectToCUMap;
Expand Down Expand Up @@ -339,6 +341,12 @@ private IContainer createFolder(IPath packagePath, IContainer outputFolder) thro
return folder;
}

@Override
public void started(TaskEvent e) {
this.javacCompiler.reportProgress(e.toString());
TaskListener.super.started(e);
}

public void setOutputDir(IContainer outputDir) {
this.outputDir = outputDir;
}
Expand Down

0 comments on commit 7cbf75d

Please sign in to comment.