# HG changeset patch # User jjg # Date 1282957148 25200 # Node ID f58f0ce45802fb477998bc0225670e321ce4649f # Parent d3d578d22cc7b821a602b9b16a3d36916d07edfc 6980707: Reduce use of IOException in JavaCompiler Reviewed-by: darcy diff -r d3d578d22cc7 -r f58f0ce45802 langtools/src/share/classes/com/sun/tools/javac/main/JavaCompiler.java --- a/langtools/src/share/classes/com/sun/tools/javac/main/JavaCompiler.java Fri Aug 27 17:21:17 2010 -0700 +++ b/langtools/src/share/classes/com/sun/tools/javac/main/JavaCompiler.java Fri Aug 27 17:59:08 2010 -0700 @@ -608,7 +608,7 @@ * @param filename The name of the file to be parsed. */ @Deprecated - public JCTree.JCCompilationUnit parse(String filename) throws IOException { + public JCTree.JCCompilationUnit parse(String filename) { JavacFileManager fm = (JavacFileManager)fileManager; return parse(fm.getJavaFileObjectsFromStrings(List.of(filename)).iterator().next()); } @@ -778,7 +778,6 @@ public void compile(List sourceFileObjects, List classnames, Iterable processors) - throws IOException // TODO: temp, from JavacProcessingEnvironment { if (processors != null && processors.iterator().hasNext()) explicitAnnotationProcessingRequested = true; @@ -868,7 +867,7 @@ /** * Parses a list of files. */ - public List parseFiles(Iterable fileObjects) throws IOException { + public List parseFiles(Iterable fileObjects) { if (shouldStop(CompileState.PARSE)) return List.nil(); @@ -950,8 +949,7 @@ * @param processors user provided annotation processors to bypass * discovery, {@code null} means that no processors were provided */ - public void initProcessAnnotations(Iterable processors) - throws IOException { + public void initProcessAnnotations(Iterable processors) { // Process annotations if processing is not disabled and there // is at least one Processor available. Options options = Options.instance(context); @@ -978,8 +976,7 @@ } // TODO: called by JavacTaskImpl - public JavaCompiler processAnnotations(List roots) - throws IOException { + public JavaCompiler processAnnotations(List roots) { return processAnnotations(roots, List.nil()); } @@ -989,8 +986,7 @@ * @return an instance of the compiler in which to complete the compilation */ public JavaCompiler processAnnotations(List roots, - List classnames) - throws IOException { // TODO: see TEMP note in JavacProcessingEnvironment + List classnames) { if (shouldStop(CompileState.PROCESS)) { // Errors were encountered. // If log.unrecoverableError is set, the errors were parse errors diff -r d3d578d22cc7 -r f58f0ce45802 langtools/src/share/classes/com/sun/tools/javac/main/Main.java --- a/langtools/src/share/classes/com/sun/tools/javac/main/Main.java Fri Aug 27 17:21:17 2010 -0700 +++ b/langtools/src/share/classes/com/sun/tools/javac/main/Main.java Fri Aug 27 17:59:08 2010 -0700 @@ -467,10 +467,13 @@ ex.printStackTrace(out); } - /** Print a message reporting an fatal error. + /** Print a message reporting a fatal error. */ void feMessage(Throwable ex) { Log.printLines(out, ex.getMessage()); + if (ex.getCause() != null && options.get("dev") != null) { + ex.getCause().printStackTrace(out); + } } /** Print a message reporting an input/output error. diff -r d3d578d22cc7 -r f58f0ce45802 langtools/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java --- a/langtools/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java Fri Aug 27 17:21:17 2010 -0700 +++ b/langtools/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java Fri Aug 27 17:59:08 2010 -0700 @@ -67,6 +67,8 @@ import com.sun.tools.javac.util.Abort; import com.sun.tools.javac.util.Context; import com.sun.tools.javac.util.Convert; +import com.sun.tools.javac.util.FatalError; +import com.sun.tools.javac.util.JCDiagnostic; import com.sun.tools.javac.util.List; import com.sun.tools.javac.util.Log; import com.sun.tools.javac.util.JavacMessages; @@ -131,6 +133,10 @@ */ Log log; + /** Diagnostic factory. + */ + JCDiagnostic.Factory diags; + /** * Source level of the compile. */ @@ -146,10 +152,11 @@ private Context context; public JavacProcessingEnvironment(Context context, Iterable processors) { - options = Options.instance(context); this.context = context; log = Log.instance(context); source = Source.instance(context); + diags = JCDiagnostic.Factory.instance(context); + options = Options.instance(context); printProcessorInfo = options.get("-XprintProcessorInfo") != null; printRounds = options.get("-XprintRounds") != null; verbose = options.get("-verbose") != null; @@ -848,8 +855,7 @@ /** Create a new round. */ private Round(Round prev, - Set newSourceFiles, Map newClassFiles) - throws IOException { + Set newSourceFiles, Map newClassFiles) { this(prev.nextContext(), prev.number+1, prev.compiler.log.nwarnings); this.genClassFiles = prev.genClassFiles; @@ -882,8 +888,7 @@ } /** Create the next round to be used. */ - Round next(Set newSourceFiles, Map newClassFiles) - throws IOException { + Round next(Set newSourceFiles, Map newClassFiles) { try { return new Round(this, newSourceFiles, newClassFiles); } finally { @@ -1083,8 +1088,7 @@ public JavaCompiler doProcessing(Context context, List roots, List classSymbols, - Iterable pckSymbols) - throws IOException { + Iterable pckSymbols) { TaskListener taskListener = context.get(TaskListener.class); log = Log.instance(context); @@ -1184,13 +1188,19 @@ /** * Free resources related to annotation processing. */ - public void close() throws IOException { + public void close() { filer.close(); if (discoveredProcs != null) // Make calling close idempotent discoveredProcs.close(); discoveredProcs = null; - if (processorClassLoader != null && processorClassLoader instanceof Closeable) - ((Closeable) processorClassLoader).close(); + if (processorClassLoader != null && processorClassLoader instanceof Closeable) { + try { + ((Closeable) processorClassLoader).close(); + } catch (IOException e) { + JCDiagnostic msg = diags.fragment("fatal.err.cant.close.loader"); + throw new FatalError(msg, e); + } + } } private List getTopLevelClasses(List units) { diff -r d3d578d22cc7 -r f58f0ce45802 langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties --- a/langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties Fri Aug 27 17:21:17 2010 -0700 +++ b/langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties Fri Aug 27 17:59:08 2010 -0700 @@ -550,6 +550,8 @@ Fatal Error: Unable to find field {0} compiler.misc.fatal.err.cant.locate.ctor=\ Fatal Error: Unable to find constructor for {0} +compiler.misc.fatal.err.cant.close.loader=\ + Fatal Error: Cannot close class loader for annotation processors ##### diff -r d3d578d22cc7 -r f58f0ce45802 langtools/src/share/classes/com/sun/tools/javac/util/FatalError.java --- a/langtools/src/share/classes/com/sun/tools/javac/util/FatalError.java Fri Aug 27 17:21:17 2010 -0700 +++ b/langtools/src/share/classes/com/sun/tools/javac/util/FatalError.java Fri Aug 27 17:59:08 2010 -0700 @@ -37,12 +37,6 @@ public class FatalError extends Error { private static final long serialVersionUID = 0; - /** Construct a FatalError with no detail message. - */ - public FatalError() { - super(); - } - /** Construct a FatalError with the specified detail message. * @param d A diagnostic containing the reason for failure. */ @@ -50,6 +44,15 @@ super(d.toString()); } + /** Construct a FatalError with the specified detail message + * and cause. + * @param d A diagnostic containing the reason for failure. + * @param t An exception causing the error + */ + public FatalError(JCDiagnostic d, Throwable t) { + super(d.toString(), t); + } + /** Construct a FatalError with the specified detail message. * @param s An English(!) string describing the failure, typically because * the diagnostic resources are missing. diff -r d3d578d22cc7 -r f58f0ce45802 langtools/test/tools/javac/diags/examples.not-yet.txt --- a/langtools/test/tools/javac/diags/examples.not-yet.txt Fri Aug 27 17:21:17 2010 -0700 +++ b/langtools/test/tools/javac/diags/examples.not-yet.txt Fri Aug 27 17:59:08 2010 -0700 @@ -62,6 +62,7 @@ compiler.misc.fatal.err.cant.locate.ctor # Resolve, from Lower compiler.misc.fatal.err.cant.locate.field # Resolve, from Lower compiler.misc.fatal.err.cant.locate.meth # Resolve, from Lower +compiler.misc.fatal.err.cant.close.loader # JavacProcessingEnvironment compiler.misc.file.does.not.contain.package compiler.misc.illegal.start.of.class.file compiler.misc.inferred.do.not.conform.to.params # UNUSED (hard to see if very complex inference scenario might require this though, so leaving it in, as per JLS3)