Skip to content

Commit 4333942

Browse files
author
Vicente Romero
committed
8250217: com.sun.tools.javac.api.JavacTaskImpl swallows compiler exceptions potentially producing false positive test results
Reviewed-by: jlahoda
1 parent 5166094 commit 4333942

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,18 @@ public Boolean call() {
9797
/* Internal version of call exposing Main.Result. */
9898
public Main.Result doCall() {
9999
try {
100-
return handleExceptions(() -> {
100+
Pair<Main.Result, Throwable> result = invocationHelper(() -> {
101101
prepareCompiler(false);
102102
if (compiler.errorCount() > 0)
103103
return Main.Result.ERROR;
104104
compiler.compile(args.getFileObjects(), args.getClassNames(), processors, addModules);
105105
return (compiler.errorCount() > 0) ? Main.Result.ERROR : Main.Result.OK; // FIXME?
106-
}, Main.Result.SYSERR, Main.Result.ABNORMAL);
106+
});
107+
if (result.snd == null) {
108+
return result.fst;
109+
} else {
110+
return (result.snd instanceof FatalError) ? Main.Result.SYSERR : Main.Result.ABNORMAL;
111+
}
107112
} finally {
108113
try {
109114
cleanup();
@@ -141,18 +146,18 @@ public void setLocale(Locale locale) {
141146
this.locale = locale;
142147
}
143148

144-
private <T> T handleExceptions(Callable<T> c, T sysErrorResult, T abnormalErrorResult) {
149+
private <T> Pair<T, Throwable> invocationHelper(Callable<T> c) {
145150
Handler prevDeferredHandler = dcfh.setHandler(dcfh.javacCodeHandler);
146151
try {
147-
return c.call();
152+
return new Pair<>(c.call(), null);
148153
} catch (FatalError ex) {
149154
Log log = Log.instance(context);
150155
Options options = Options.instance(context);
151156
log.printRawLines(ex.getMessage());
152157
if (ex.getCause() != null && options.isSet("dev")) {
153158
ex.getCause().printStackTrace(log.getWriter(WriterKind.NOTICE));
154159
}
155-
return sysErrorResult;
160+
return new Pair<>(null, ex);
156161
} catch (AnnotationProcessingError | ClientCodeException e) {
157162
// AnnotationProcessingError is thrown from JavacProcessingEnvironment,
158163
// to forward errors thrown from an annotation processor
@@ -175,7 +180,7 @@ private <T> T handleExceptions(Callable<T> c, T sysErrorResult, T abnormalErrorR
175180
log.printLines(PrefixKind.JAVAC, "msg.bug", JavaCompiler.version());
176181
ex.printStackTrace(log.getWriter(WriterKind.NOTICE));
177182
}
178-
return abnormalErrorResult;
183+
return new Pair<>(null, ex);
179184
} finally {
180185
dcfh.setHandler(prevDeferredHandler);
181186
}
@@ -240,7 +245,11 @@ void cleanup() {
240245

241246
@Override @DefinedBy(Api.COMPILER_TREE)
242247
public Iterable<? extends CompilationUnitTree> parse() {
243-
return handleExceptions(this::parseInternal, List.nil(), List.nil());
248+
Pair<Iterable<? extends CompilationUnitTree>, Throwable> result = invocationHelper(this::parseInternal);
249+
if (result.snd == null) {
250+
return result.fst;
251+
}
252+
throw new IllegalStateException(result.snd);
244253
}
245254

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

368377
@Override @DefinedBy(Api.COMPILER_TREE)
369378
public Iterable<? extends Element> analyze() {
370-
return handleExceptions(() -> analyze(null), List.nil(), List.nil());
379+
Pair<Iterable<? extends Element>, Throwable> result = invocationHelper(() -> analyze(null));
380+
if (result.snd == null) {
381+
return result.fst;
382+
}
383+
throw new IllegalStateException(result.snd);
371384
}
372385

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

430443
@Override @DefinedBy(Api.COMPILER_TREE)
431444
public Iterable<? extends JavaFileObject> generate() {
432-
return handleExceptions(() -> generate(null), List.nil(), List.nil());
445+
Pair<Iterable<? extends JavaFileObject>, Throwable> result = invocationHelper(() -> generate(null));
446+
if (result.snd == null) {
447+
return result.fst;
448+
}
449+
throw new IllegalStateException(result.snd);
433450
}
434451

435452
/**

test/langtools/tools/javac/processing/errors/CrashOnNonExistingAnnotation/Processor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public TestFO(URI uri, String content) {
189189
}
190190

191191
@Override public boolean isNameCompatible(String simpleName, Kind kind) {
192-
return true;
192+
return simpleName.equals("Source") && kind == Kind.SOURCE;
193193
}
194194
}
195195

0 commit comments

Comments
 (0)