提交 c30deff1 编写于 作者: J jjg

6437138: JSR 199: Compiler doesn't diagnose crash in user code

6482554: uncaught exception from annotation processor not reported through JavaCompiler.CompilationTask.call
Reviewed-by: mcimadamore
上级 b7879044
......@@ -65,7 +65,7 @@ import com.sun.tools.javac.main.JavaCompiler;
* @author Jonathan Gibbons
*/
public class JavacTaskImpl extends JavacTask {
private JavacTool tool;
private ClientCodeWrapper ccw;
private Main compilerMain;
private JavaCompiler compiler;
private Locale locale;
......@@ -80,12 +80,11 @@ public class JavacTaskImpl extends JavacTask {
private Integer result = null;
JavacTaskImpl(JavacTool tool,
Main compilerMain,
JavacTaskImpl(Main compilerMain,
String[] args,
Context context,
List<JavaFileObject> fileObjects) {
this.tool = tool;
this.ccw = ClientCodeWrapper.instance(context);
this.compilerMain = compilerMain;
this.args = args;
this.context = context;
......@@ -94,17 +93,15 @@ public class JavacTaskImpl extends JavacTask {
// null checks
compilerMain.getClass();
args.getClass();
context.getClass();
fileObjects.getClass();
}
JavacTaskImpl(JavacTool tool,
Main compilerMain,
JavacTaskImpl(Main compilerMain,
Iterable<String> flags,
Context context,
Iterable<String> classes,
Iterable<? extends JavaFileObject> fileObjects) {
this(tool, compilerMain, toArray(flags, classes), context, toList(fileObjects));
this(compilerMain, toArray(flags, classes), context, toList(fileObjects));
}
static private String[] toArray(Iterable<String> flags, Iterable<String> classes) {
......@@ -131,7 +128,7 @@ public class JavacTaskImpl extends JavacTask {
if (!used.getAndSet(true)) {
initContext();
notYetEntered = new HashMap<JavaFileObject, JCCompilationUnit>();
compilerMain.setFatalErrors(true);
compilerMain.setAPIMode(true);
result = compilerMain.compile(args, context, fileObjects, processors);
cleanup();
return result == 0;
......@@ -185,32 +182,10 @@ public class JavacTaskImpl extends JavacTask {
if (context.get(TaskListener.class) != null)
context.put(TaskListener.class, (TaskListener)null);
if (taskListener != null)
context.put(TaskListener.class, wrap(taskListener));
context.put(TaskListener.class, ccw.wrap(taskListener));
//initialize compiler's default locale
context.put(Locale.class, locale);
}
// where
private TaskListener wrap(final TaskListener tl) {
tl.getClass(); // null check
return new TaskListener() {
public void started(TaskEvent e) {
try {
tl.started(e);
} catch (Throwable t) {
throw new ClientCodeException(t);
}
}
public void finished(TaskEvent e) {
try {
tl.finished(e);
} catch (Throwable t) {
throw new ClientCodeException(t);
}
}
};
}
void cleanup() {
if (compiler != null)
......
......@@ -49,6 +49,7 @@ import com.sun.tools.javac.main.JavacOption;
import com.sun.tools.javac.main.Main;
import com.sun.tools.javac.main.RecognizedOptions.GrumpyHelper;
import com.sun.tools.javac.main.RecognizedOptions;
import com.sun.tools.javac.util.ClientCodeException;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Log;
import com.sun.tools.javac.util.Options;
......@@ -162,38 +163,45 @@ public final class JavacTool implements JavaCompiler {
Iterable<String> classes,
Iterable<? extends JavaFileObject> compilationUnits)
{
final String kindMsg = "All compilation units must be of SOURCE kind";
if (options != null)
for (String option : options)
option.getClass(); // null check
if (classes != null) {
for (String cls : classes)
if (!SourceVersion.isName(cls)) // implicit null check
throw new IllegalArgumentException("Not a valid class name: " + cls);
}
if (compilationUnits != null) {
for (JavaFileObject cu : compilationUnits) {
if (cu.getKind() != JavaFileObject.Kind.SOURCE) // implicit null check
throw new IllegalArgumentException(kindMsg);
}
}
try {
Context context = new Context();
ClientCodeWrapper ccw = ClientCodeWrapper.instance(context);
Context context = new Context();
final String kindMsg = "All compilation units must be of SOURCE kind";
if (options != null)
for (String option : options)
option.getClass(); // null check
if (classes != null) {
for (String cls : classes)
if (!SourceVersion.isName(cls)) // implicit null check
throw new IllegalArgumentException("Not a valid class name: " + cls);
}
if (compilationUnits != null) {
compilationUnits = ccw.wrapJavaFileObjects(compilationUnits); // implicit null check
for (JavaFileObject cu : compilationUnits) {
if (cu.getKind() != JavaFileObject.Kind.SOURCE)
throw new IllegalArgumentException(kindMsg);
}
}
if (diagnosticListener != null)
context.put(DiagnosticListener.class, diagnosticListener);
if (diagnosticListener != null)
context.put(DiagnosticListener.class, ccw.wrap(diagnosticListener));
if (out == null)
context.put(Log.outKey, new PrintWriter(System.err, true));
else
context.put(Log.outKey, new PrintWriter(out, true));
if (out == null)
context.put(Log.outKey, new PrintWriter(System.err, true));
else
context.put(Log.outKey, new PrintWriter(out, true));
if (fileManager == null)
fileManager = getStandardFileManager(diagnosticListener, null, null);
context.put(JavaFileManager.class, fileManager);
processOptions(context, fileManager, options);
Main compiler = new Main("javacTask", context.get(Log.outKey));
return new JavacTaskImpl(this, compiler, options, context, classes, compilationUnits);
if (fileManager == null)
fileManager = getStandardFileManager(diagnosticListener, null, null);
fileManager = ccw.wrap(fileManager);
context.put(JavaFileManager.class, fileManager);
processOptions(context, fileManager, options);
Main compiler = new Main("javacTask", context.get(Log.outKey));
return new JavacTaskImpl(compiler, options, context, classes, compilationUnits);
} catch (ClientCodeException ex) {
throw new RuntimeException(ex.getCause());
}
}
private static void processOptions(Context context,
......
......@@ -65,9 +65,11 @@ public class Main {
PrintWriter out;
/**
* If true, any command line arg errors will cause an exception.
* If true, certain errors will cause an exception, such as command line
* arg errors, or exceptions in user provided code.
*/
boolean fatalErrors;
boolean apiMode;
/** Result codes.
*/
......@@ -163,7 +165,7 @@ public class Main {
/** Report a usage error.
*/
void error(String key, Object... args) {
if (fatalErrors) {
if (apiMode) {
String msg = getLocalizedString(key, args);
throw new PropagatedException(new IllegalStateException(msg));
}
......@@ -192,8 +194,8 @@ public class Main {
this.options = options;
}
public void setFatalErrors(boolean fatalErrors) {
this.fatalErrors = fatalErrors;
public void setAPIMode(boolean apiMode) {
this.apiMode = apiMode;
}
/** Process command line arguments: store all command line options
......@@ -440,7 +442,9 @@ public class Main {
} catch (FatalError ex) {
feMessage(ex);
return EXIT_SYSERR;
} catch(AnnotationProcessingError ex) {
} catch (AnnotationProcessingError ex) {
if (apiMode)
throw new RuntimeException(ex.getCause());
apMessage(ex);
return EXIT_SYSERR;
} catch (ClientCodeException ex) {
......@@ -458,7 +462,13 @@ public class Main {
bugMessage(ex);
return EXIT_ABNORMAL;
} finally {
if (comp != null) comp.close();
if (comp != null) {
try {
comp.close();
} catch (ClientCodeException ex) {
throw new RuntimeException(ex.getCause());
}
}
filenames = null;
options = null;
}
......
......@@ -67,6 +67,7 @@ import com.sun.tools.javac.tree.*;
import com.sun.tools.javac.tree.JCTree.*;
import com.sun.tools.javac.util.Abort;
import com.sun.tools.javac.util.Assert;
import com.sun.tools.javac.util.ClientCodeException;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Convert;
import com.sun.tools.javac.util.FatalError;
......@@ -432,6 +433,8 @@ public class JavacProcessingEnvironment implements ProcessingEnvironment, Closea
log.error("proc.processor.cant.instantiate", processorName);
return false;
}
} catch(ClientCodeException e) {
throw e;
} catch(Throwable t) {
throw new AnnotationProcessingError(t);
}
......@@ -527,6 +530,8 @@ public class JavacProcessingEnvironment implements ProcessingEnvironment, Closea
supportedOptionNames.add(optionName);
}
} catch (ClientCodeException e) {
throw e;
} catch (Throwable t) {
throw new AnnotationProcessingError(t);
}
......@@ -790,6 +795,8 @@ public class JavacProcessingEnvironment implements ProcessingEnvironment, Closea
ex.printStackTrace(new PrintWriter(out));
log.error("proc.cant.access", ex.sym, ex.getDetailValue(), out.toString());
return false;
} catch (ClientCodeException e) {
throw e;
} catch (Throwable t) {
throw new AnnotationProcessingError(t);
}
......
......@@ -425,13 +425,8 @@ public class Log extends AbstractLog {
*/
protected void writeDiagnostic(JCDiagnostic diag) {
if (diagListener != null) {
try {
diagListener.report(diag);
return;
}
catch (Throwable t) {
throw new ClientCodeException(t);
}
diagListener.report(diag);
return;
}
PrintWriter writer = getWriterForDiagnosticType(diag.getType());
......
/*
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 6437138
* @summary JSR 199: Compiler doesn't diagnose crash in user code
*/
import java.net.URI;
import java.util.Arrays;
import javax.tools.*;
import static javax.tools.JavaFileObject.Kind.*;
public class T6437138 {
static class JFO extends SimpleJavaFileObject {
public JFO(URI uri, JavaFileObject.Kind kind) {
super(uri, kind);
}
// getCharContent not impl, will throw UnsupportedOperationException
}
public static void main(String... arg) throws Exception {
try {
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
JavaFileObject jfo = new JFO(new URI("JFOTest04.java"),SOURCE);
JavaCompiler.CompilationTask ct = javac.getTask(null,null,null,null,
null, Arrays.asList(jfo));
ct.call();
throw new Exception("no exception thrown by JavaCompiler.CompilationTask");
} catch (RuntimeException e) {
if (e.getCause() instanceof UnsupportedOperationException) {
System.err.println("RuntimeException(UnsupportedOperationException) caught as expected");
return;
}
throw new Exception("unexpected exception caught", e);
}
}
}
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册