Skip to content

Commit

Permalink
8250217: com.sun.tools.javac.api.JavacTaskImpl swallows compiler exce…
Browse files Browse the repository at this point in the history
…ptions potentially producing false positive test results

Reviewed-by: jlahoda
  • Loading branch information
Vicente Romero committed Sep 9, 2020
1 parent 5166094 commit 4333942
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,18 @@ public Boolean call() {
/* Internal version of call exposing Main.Result. */
public Main.Result doCall() {
try {
return handleExceptions(() -> {
Pair<Main.Result, Throwable> result = invocationHelper(() -> {
prepareCompiler(false);
if (compiler.errorCount() > 0)
return Main.Result.ERROR;
compiler.compile(args.getFileObjects(), args.getClassNames(), processors, addModules);
return (compiler.errorCount() > 0) ? Main.Result.ERROR : Main.Result.OK; // FIXME?
}, Main.Result.SYSERR, Main.Result.ABNORMAL);
});
if (result.snd == null) {
return result.fst;
} else {
return (result.snd instanceof FatalError) ? Main.Result.SYSERR : Main.Result.ABNORMAL;
}
} finally {
try {
cleanup();
Expand Down Expand Up @@ -141,18 +146,18 @@ public void setLocale(Locale locale) {
this.locale = locale;
}

private <T> T handleExceptions(Callable<T> c, T sysErrorResult, T abnormalErrorResult) {
private <T> Pair<T, Throwable> invocationHelper(Callable<T> c) {
Handler prevDeferredHandler = dcfh.setHandler(dcfh.javacCodeHandler);
try {
return c.call();
return new Pair<>(c.call(), null);
} catch (FatalError ex) {
Log log = Log.instance(context);
Options options = Options.instance(context);
log.printRawLines(ex.getMessage());
if (ex.getCause() != null && options.isSet("dev")) {
ex.getCause().printStackTrace(log.getWriter(WriterKind.NOTICE));
}
return sysErrorResult;
return new Pair<>(null, ex);
} catch (AnnotationProcessingError | ClientCodeException e) {
// AnnotationProcessingError is thrown from JavacProcessingEnvironment,
// to forward errors thrown from an annotation processor
Expand All @@ -175,7 +180,7 @@ private <T> T handleExceptions(Callable<T> c, T sysErrorResult, T abnormalErrorR
log.printLines(PrefixKind.JAVAC, "msg.bug", JavaCompiler.version());
ex.printStackTrace(log.getWriter(WriterKind.NOTICE));
}
return abnormalErrorResult;
return new Pair<>(null, ex);
} finally {
dcfh.setHandler(prevDeferredHandler);
}
Expand Down Expand Up @@ -240,7 +245,11 @@ void cleanup() {

@Override @DefinedBy(Api.COMPILER_TREE)
public Iterable<? extends CompilationUnitTree> parse() {
return handleExceptions(this::parseInternal, List.nil(), List.nil());
Pair<Iterable<? extends CompilationUnitTree>, Throwable> result = invocationHelper(this::parseInternal);
if (result.snd == null) {
return result.fst;
}
throw new IllegalStateException(result.snd);
}

private Iterable<? extends CompilationUnitTree> parseInternal() {
Expand Down Expand Up @@ -367,7 +376,11 @@ public Iterable<? extends Element> enter(Iterable<? extends CompilationUnitTree>

@Override @DefinedBy(Api.COMPILER_TREE)
public Iterable<? extends Element> analyze() {
return handleExceptions(() -> analyze(null), List.nil(), List.nil());
Pair<Iterable<? extends Element>, Throwable> result = invocationHelper(() -> analyze(null));
if (result.snd == null) {
return result.fst;
}
throw new IllegalStateException(result.snd);
}

/**
Expand Down Expand Up @@ -429,7 +442,11 @@ private void handleFlowResults(Queue<Env<AttrContext>> queue, ListBuffer<Element

@Override @DefinedBy(Api.COMPILER_TREE)
public Iterable<? extends JavaFileObject> generate() {
return handleExceptions(() -> generate(null), List.nil(), List.nil());
Pair<Iterable<? extends JavaFileObject>, Throwable> result = invocationHelper(() -> generate(null));
if (result.snd == null) {
return result.fst;
}
throw new IllegalStateException(result.snd);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public TestFO(URI uri, String content) {
}

@Override public boolean isNameCompatible(String simpleName, Kind kind) {
return true;
return simpleName.equals("Source") && kind == Kind.SOURCE;
}
}

Expand Down

0 comments on commit 4333942

Please sign in to comment.