提交 0fc7537a 编写于 作者: J jjg

6980707: Reduce use of IOException in JavaCompiler

Reviewed-by: darcy
上级 c3961142
...@@ -608,7 +608,7 @@ public class JavaCompiler implements ClassReader.SourceCompleter { ...@@ -608,7 +608,7 @@ public class JavaCompiler implements ClassReader.SourceCompleter {
* @param filename The name of the file to be parsed. * @param filename The name of the file to be parsed.
*/ */
@Deprecated @Deprecated
public JCTree.JCCompilationUnit parse(String filename) throws IOException { public JCTree.JCCompilationUnit parse(String filename) {
JavacFileManager fm = (JavacFileManager)fileManager; JavacFileManager fm = (JavacFileManager)fileManager;
return parse(fm.getJavaFileObjectsFromStrings(List.of(filename)).iterator().next()); return parse(fm.getJavaFileObjectsFromStrings(List.of(filename)).iterator().next());
} }
...@@ -778,7 +778,6 @@ public class JavaCompiler implements ClassReader.SourceCompleter { ...@@ -778,7 +778,6 @@ public class JavaCompiler implements ClassReader.SourceCompleter {
public void compile(List<JavaFileObject> sourceFileObjects, public void compile(List<JavaFileObject> sourceFileObjects,
List<String> classnames, List<String> classnames,
Iterable<? extends Processor> processors) Iterable<? extends Processor> processors)
throws IOException // TODO: temp, from JavacProcessingEnvironment
{ {
if (processors != null && processors.iterator().hasNext()) if (processors != null && processors.iterator().hasNext())
explicitAnnotationProcessingRequested = true; explicitAnnotationProcessingRequested = true;
...@@ -868,7 +867,7 @@ public class JavaCompiler implements ClassReader.SourceCompleter { ...@@ -868,7 +867,7 @@ public class JavaCompiler implements ClassReader.SourceCompleter {
/** /**
* Parses a list of files. * Parses a list of files.
*/ */
public List<JCCompilationUnit> parseFiles(Iterable<JavaFileObject> fileObjects) throws IOException { public List<JCCompilationUnit> parseFiles(Iterable<JavaFileObject> fileObjects) {
if (shouldStop(CompileState.PARSE)) if (shouldStop(CompileState.PARSE))
return List.nil(); return List.nil();
...@@ -950,8 +949,7 @@ public class JavaCompiler implements ClassReader.SourceCompleter { ...@@ -950,8 +949,7 @@ public class JavaCompiler implements ClassReader.SourceCompleter {
* @param processors user provided annotation processors to bypass * @param processors user provided annotation processors to bypass
* discovery, {@code null} means that no processors were provided * discovery, {@code null} means that no processors were provided
*/ */
public void initProcessAnnotations(Iterable<? extends Processor> processors) public void initProcessAnnotations(Iterable<? extends Processor> processors) {
throws IOException {
// Process annotations if processing is not disabled and there // Process annotations if processing is not disabled and there
// is at least one Processor available. // is at least one Processor available.
Options options = Options.instance(context); Options options = Options.instance(context);
...@@ -978,8 +976,7 @@ public class JavaCompiler implements ClassReader.SourceCompleter { ...@@ -978,8 +976,7 @@ public class JavaCompiler implements ClassReader.SourceCompleter {
} }
// TODO: called by JavacTaskImpl // TODO: called by JavacTaskImpl
public JavaCompiler processAnnotations(List<JCCompilationUnit> roots) public JavaCompiler processAnnotations(List<JCCompilationUnit> roots) {
throws IOException {
return processAnnotations(roots, List.<String>nil()); return processAnnotations(roots, List.<String>nil());
} }
...@@ -989,8 +986,7 @@ public class JavaCompiler implements ClassReader.SourceCompleter { ...@@ -989,8 +986,7 @@ public class JavaCompiler implements ClassReader.SourceCompleter {
* @return an instance of the compiler in which to complete the compilation * @return an instance of the compiler in which to complete the compilation
*/ */
public JavaCompiler processAnnotations(List<JCCompilationUnit> roots, public JavaCompiler processAnnotations(List<JCCompilationUnit> roots,
List<String> classnames) List<String> classnames) {
throws IOException { // TODO: see TEMP note in JavacProcessingEnvironment
if (shouldStop(CompileState.PROCESS)) { if (shouldStop(CompileState.PROCESS)) {
// Errors were encountered. // Errors were encountered.
// If log.unrecoverableError is set, the errors were parse errors // If log.unrecoverableError is set, the errors were parse errors
......
...@@ -467,10 +467,13 @@ public class Main { ...@@ -467,10 +467,13 @@ public class Main {
ex.printStackTrace(out); ex.printStackTrace(out);
} }
/** Print a message reporting an fatal error. /** Print a message reporting a fatal error.
*/ */
void feMessage(Throwable ex) { void feMessage(Throwable ex) {
Log.printLines(out, ex.getMessage()); 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. /** Print a message reporting an input/output error.
......
...@@ -67,6 +67,8 @@ import com.sun.tools.javac.tree.JCTree.*; ...@@ -67,6 +67,8 @@ import com.sun.tools.javac.tree.JCTree.*;
import com.sun.tools.javac.util.Abort; import com.sun.tools.javac.util.Abort;
import com.sun.tools.javac.util.Context; import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Convert; 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.List;
import com.sun.tools.javac.util.Log; import com.sun.tools.javac.util.Log;
import com.sun.tools.javac.util.JavacMessages; import com.sun.tools.javac.util.JavacMessages;
...@@ -131,6 +133,10 @@ public class JavacProcessingEnvironment implements ProcessingEnvironment, Closea ...@@ -131,6 +133,10 @@ public class JavacProcessingEnvironment implements ProcessingEnvironment, Closea
*/ */
Log log; Log log;
/** Diagnostic factory.
*/
JCDiagnostic.Factory diags;
/** /**
* Source level of the compile. * Source level of the compile.
*/ */
...@@ -146,10 +152,11 @@ public class JavacProcessingEnvironment implements ProcessingEnvironment, Closea ...@@ -146,10 +152,11 @@ public class JavacProcessingEnvironment implements ProcessingEnvironment, Closea
private Context context; private Context context;
public JavacProcessingEnvironment(Context context, Iterable<? extends Processor> processors) { public JavacProcessingEnvironment(Context context, Iterable<? extends Processor> processors) {
options = Options.instance(context);
this.context = context; this.context = context;
log = Log.instance(context); log = Log.instance(context);
source = Source.instance(context); source = Source.instance(context);
diags = JCDiagnostic.Factory.instance(context);
options = Options.instance(context);
printProcessorInfo = options.get("-XprintProcessorInfo") != null; printProcessorInfo = options.get("-XprintProcessorInfo") != null;
printRounds = options.get("-XprintRounds") != null; printRounds = options.get("-XprintRounds") != null;
verbose = options.get("-verbose") != null; verbose = options.get("-verbose") != null;
...@@ -848,8 +855,7 @@ public class JavacProcessingEnvironment implements ProcessingEnvironment, Closea ...@@ -848,8 +855,7 @@ public class JavacProcessingEnvironment implements ProcessingEnvironment, Closea
/** Create a new round. */ /** Create a new round. */
private Round(Round prev, private Round(Round prev,
Set<JavaFileObject> newSourceFiles, Map<String,JavaFileObject> newClassFiles) Set<JavaFileObject> newSourceFiles, Map<String,JavaFileObject> newClassFiles) {
throws IOException {
this(prev.nextContext(), prev.number+1, prev.compiler.log.nwarnings); this(prev.nextContext(), prev.number+1, prev.compiler.log.nwarnings);
this.genClassFiles = prev.genClassFiles; this.genClassFiles = prev.genClassFiles;
...@@ -882,8 +888,7 @@ public class JavacProcessingEnvironment implements ProcessingEnvironment, Closea ...@@ -882,8 +888,7 @@ public class JavacProcessingEnvironment implements ProcessingEnvironment, Closea
} }
/** Create the next round to be used. */ /** Create the next round to be used. */
Round next(Set<JavaFileObject> newSourceFiles, Map<String, JavaFileObject> newClassFiles) Round next(Set<JavaFileObject> newSourceFiles, Map<String, JavaFileObject> newClassFiles) {
throws IOException {
try { try {
return new Round(this, newSourceFiles, newClassFiles); return new Round(this, newSourceFiles, newClassFiles);
} finally { } finally {
...@@ -1083,8 +1088,7 @@ public class JavacProcessingEnvironment implements ProcessingEnvironment, Closea ...@@ -1083,8 +1088,7 @@ public class JavacProcessingEnvironment implements ProcessingEnvironment, Closea
public JavaCompiler doProcessing(Context context, public JavaCompiler doProcessing(Context context,
List<JCCompilationUnit> roots, List<JCCompilationUnit> roots,
List<ClassSymbol> classSymbols, List<ClassSymbol> classSymbols,
Iterable<? extends PackageSymbol> pckSymbols) Iterable<? extends PackageSymbol> pckSymbols) {
throws IOException {
TaskListener taskListener = context.get(TaskListener.class); TaskListener taskListener = context.get(TaskListener.class);
log = Log.instance(context); log = Log.instance(context);
...@@ -1184,13 +1188,19 @@ public class JavacProcessingEnvironment implements ProcessingEnvironment, Closea ...@@ -1184,13 +1188,19 @@ public class JavacProcessingEnvironment implements ProcessingEnvironment, Closea
/** /**
* Free resources related to annotation processing. * Free resources related to annotation processing.
*/ */
public void close() throws IOException { public void close() {
filer.close(); filer.close();
if (discoveredProcs != null) // Make calling close idempotent if (discoveredProcs != null) // Make calling close idempotent
discoveredProcs.close(); discoveredProcs.close();
discoveredProcs = null; discoveredProcs = null;
if (processorClassLoader != null && processorClassLoader instanceof Closeable) if (processorClassLoader != null && processorClassLoader instanceof Closeable) {
((Closeable) processorClassLoader).close(); try {
((Closeable) processorClassLoader).close();
} catch (IOException e) {
JCDiagnostic msg = diags.fragment("fatal.err.cant.close.loader");
throw new FatalError(msg, e);
}
}
} }
private List<ClassSymbol> getTopLevelClasses(List<? extends JCCompilationUnit> units) { private List<ClassSymbol> getTopLevelClasses(List<? extends JCCompilationUnit> units) {
......
...@@ -550,6 +550,8 @@ compiler.misc.fatal.err.cant.locate.field=\ ...@@ -550,6 +550,8 @@ compiler.misc.fatal.err.cant.locate.field=\
Fatal Error: Unable to find field {0} Fatal Error: Unable to find field {0}
compiler.misc.fatal.err.cant.locate.ctor=\ compiler.misc.fatal.err.cant.locate.ctor=\
Fatal Error: Unable to find constructor for {0} Fatal Error: Unable to find constructor for {0}
compiler.misc.fatal.err.cant.close.loader=\
Fatal Error: Cannot close class loader for annotation processors
##### #####
......
...@@ -37,12 +37,6 @@ package com.sun.tools.javac.util; ...@@ -37,12 +37,6 @@ package com.sun.tools.javac.util;
public class FatalError extends Error { public class FatalError extends Error {
private static final long serialVersionUID = 0; private static final long serialVersionUID = 0;
/** Construct a <code>FatalError</code> with no detail message.
*/
public FatalError() {
super();
}
/** Construct a <code>FatalError</code> with the specified detail message. /** Construct a <code>FatalError</code> with the specified detail message.
* @param d A diagnostic containing the reason for failure. * @param d A diagnostic containing the reason for failure.
*/ */
...@@ -50,6 +44,15 @@ public class FatalError extends Error { ...@@ -50,6 +44,15 @@ public class FatalError extends Error {
super(d.toString()); super(d.toString());
} }
/** Construct a <code>FatalError</code> 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 <code>FatalError</code> with the specified detail message. /** Construct a <code>FatalError</code> with the specified detail message.
* @param s An English(!) string describing the failure, typically because * @param s An English(!) string describing the failure, typically because
* the diagnostic resources are missing. * the diagnostic resources are missing.
......
...@@ -62,6 +62,7 @@ compiler.misc.class.file.wrong.class ...@@ -62,6 +62,7 @@ compiler.misc.class.file.wrong.class
compiler.misc.fatal.err.cant.locate.ctor # Resolve, from Lower 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.field # Resolve, from Lower
compiler.misc.fatal.err.cant.locate.meth # 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.file.does.not.contain.package
compiler.misc.illegal.start.of.class.file 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) 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)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册