8250217: com.sun.tools.javac.api.JavacTaskImpl swallows compiler exceptions potentially producing false positive test results

Reviewed-by: jlahoda
This commit is contained in:
Vicente Romero 2020-09-09 14:05:00 +00:00
parent 5166094647
commit 433394203d
2 changed files with 27 additions and 10 deletions

View File

@ -97,13 +97,18 @@ public class JavacTaskImpl extends BasicJavacTask {
/* Internal version of call exposing Main.Result. */ /* Internal version of call exposing Main.Result. */
public Main.Result doCall() { public Main.Result doCall() {
try { try {
return handleExceptions(() -> { Pair<Main.Result, Throwable> result = invocationHelper(() -> {
prepareCompiler(false); prepareCompiler(false);
if (compiler.errorCount() > 0) if (compiler.errorCount() > 0)
return Main.Result.ERROR; return Main.Result.ERROR;
compiler.compile(args.getFileObjects(), args.getClassNames(), processors, addModules); compiler.compile(args.getFileObjects(), args.getClassNames(), processors, addModules);
return (compiler.errorCount() > 0) ? Main.Result.ERROR : Main.Result.OK; // FIXME? 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 { } finally {
try { try {
cleanup(); cleanup();
@ -141,10 +146,10 @@ public class JavacTaskImpl extends BasicJavacTask {
this.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); Handler prevDeferredHandler = dcfh.setHandler(dcfh.javacCodeHandler);
try { try {
return c.call(); return new Pair<>(c.call(), null);
} catch (FatalError ex) { } catch (FatalError ex) {
Log log = Log.instance(context); Log log = Log.instance(context);
Options options = Options.instance(context); Options options = Options.instance(context);
@ -152,7 +157,7 @@ public class JavacTaskImpl extends BasicJavacTask {
if (ex.getCause() != null && options.isSet("dev")) { if (ex.getCause() != null && options.isSet("dev")) {
ex.getCause().printStackTrace(log.getWriter(WriterKind.NOTICE)); ex.getCause().printStackTrace(log.getWriter(WriterKind.NOTICE));
} }
return sysErrorResult; return new Pair<>(null, ex);
} catch (AnnotationProcessingError | ClientCodeException e) { } catch (AnnotationProcessingError | ClientCodeException e) {
// AnnotationProcessingError is thrown from JavacProcessingEnvironment, // AnnotationProcessingError is thrown from JavacProcessingEnvironment,
// to forward errors thrown from an annotation processor // to forward errors thrown from an annotation processor
@ -175,7 +180,7 @@ public class JavacTaskImpl extends BasicJavacTask {
log.printLines(PrefixKind.JAVAC, "msg.bug", JavaCompiler.version()); log.printLines(PrefixKind.JAVAC, "msg.bug", JavaCompiler.version());
ex.printStackTrace(log.getWriter(WriterKind.NOTICE)); ex.printStackTrace(log.getWriter(WriterKind.NOTICE));
} }
return abnormalErrorResult; return new Pair<>(null, ex);
} finally { } finally {
dcfh.setHandler(prevDeferredHandler); dcfh.setHandler(prevDeferredHandler);
} }
@ -240,7 +245,11 @@ public class JavacTaskImpl extends BasicJavacTask {
@Override @DefinedBy(Api.COMPILER_TREE) @Override @DefinedBy(Api.COMPILER_TREE)
public Iterable<? extends CompilationUnitTree> parse() { 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() { private Iterable<? extends CompilationUnitTree> parseInternal() {
@ -367,7 +376,11 @@ public class JavacTaskImpl extends BasicJavacTask {
@Override @DefinedBy(Api.COMPILER_TREE) @Override @DefinedBy(Api.COMPILER_TREE)
public Iterable<? extends Element> analyze() { 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);
} }
/** /**
@ -429,7 +442,11 @@ public class JavacTaskImpl extends BasicJavacTask {
@Override @DefinedBy(Api.COMPILER_TREE) @Override @DefinedBy(Api.COMPILER_TREE)
public Iterable<? extends JavaFileObject> generate() { 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);
} }
/** /**

View File

@ -189,7 +189,7 @@ public class Processor extends AbstractProcessor {
} }
@Override public boolean isNameCompatible(String simpleName, Kind kind) { @Override public boolean isNameCompatible(String simpleName, Kind kind) {
return true; return simpleName.equals("Source") && kind == Kind.SOURCE;
} }
} }