langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java
changeset 45504 ea7475564d07
parent 44290 202973b2d1ae
child 45862 c6827bac317d
equal deleted inserted replaced
45503:d23ae2d67a5d 45504:ea7475564d07
    79 
    79 
    80 import static com.sun.tools.javac.code.Kinds.Kind.*;
    80 import static com.sun.tools.javac.code.Kinds.Kind.*;
    81 
    81 
    82 import com.sun.tools.javac.code.Symbol.ModuleSymbol;
    82 import com.sun.tools.javac.code.Symbol.ModuleSymbol;
    83 import com.sun.tools.javac.resources.CompilerProperties.Errors;
    83 import com.sun.tools.javac.resources.CompilerProperties.Errors;
       
    84 import com.sun.tools.javac.resources.CompilerProperties.Fragments;
       
    85 import com.sun.tools.javac.resources.CompilerProperties.Notes;
    84 import com.sun.tools.javac.resources.CompilerProperties.Warnings;
    86 import com.sun.tools.javac.resources.CompilerProperties.Warnings;
    85 
    87 
    86 import static com.sun.tools.javac.code.TypeTag.CLASS;
    88 import static com.sun.tools.javac.code.TypeTag.CLASS;
    87 import static com.sun.tools.javac.main.Option.*;
    89 import static com.sun.tools.javac.main.Option.*;
    88 import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag.*;
    90 import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag.*;
   391         try {
   393         try {
   392             // catch completion problems with predefineds
   394             // catch completion problems with predefineds
   393             syms = Symtab.instance(context);
   395             syms = Symtab.instance(context);
   394         } catch (CompletionFailure ex) {
   396         } catch (CompletionFailure ex) {
   395             // inlined Check.completionError as it is not initialized yet
   397             // inlined Check.completionError as it is not initialized yet
   396             log.error("cant.access", ex.sym, ex.getDetailValue());
   398             log.error(Errors.CantAccess(ex.sym, ex.getDetailValue()));
   397             if (ex instanceof ClassFinder.BadClassFile)
   399             if (ex instanceof ClassFinder.BadClassFile)
   398                 throw new Abort();
   400                 throw new Abort();
   399         }
   401         }
   400         source = Source.instance(context);
   402         source = Source.instance(context);
   401         attr = Attr.instance(context);
   403         attr = Attr.instance(context);
   568 
   570 
   569     /** The number of errors reported so far.
   571     /** The number of errors reported so far.
   570      */
   572      */
   571     public int errorCount() {
   573     public int errorCount() {
   572         if (werror && log.nerrors == 0 && log.nwarnings > 0) {
   574         if (werror && log.nerrors == 0 && log.nwarnings > 0) {
   573             log.error("warnings.and.werror");
   575             log.error(Errors.WarningsAndWerror);
   574         }
   576         }
   575         return log.nerrors;
   577         return log.nerrors;
   576     }
   578     }
   577 
   579 
   578     protected final <T> Queue<T> stopIfError(CompileState cs, Queue<T> queue) {
   580     protected final <T> Queue<T> stopIfError(CompileState cs, Queue<T> queue) {
   596     public CharSequence readSource(JavaFileObject filename) {
   598     public CharSequence readSource(JavaFileObject filename) {
   597         try {
   599         try {
   598             inputFiles.add(filename);
   600             inputFiles.add(filename);
   599             return filename.getCharContent(false);
   601             return filename.getCharContent(false);
   600         } catch (IOException e) {
   602         } catch (IOException e) {
   601             log.error("error.reading.file", filename, JavacFileManager.getMessage(e));
   603             log.error(Errors.ErrorReadingFile(filename, JavacFileManager.getMessage(e)));
   602             return null;
   604             return null;
   603         }
   605         }
   604     }
   606     }
   605 
   607 
   606     /** Parse contents of input stream.
   608     /** Parse contents of input stream.
   740     JavaFileObject genCode(Env<AttrContext> env, JCClassDecl cdef) throws IOException {
   742     JavaFileObject genCode(Env<AttrContext> env, JCClassDecl cdef) throws IOException {
   741         try {
   743         try {
   742             if (gen.genClass(env, cdef) && (errorCount() == 0))
   744             if (gen.genClass(env, cdef) && (errorCount() == 0))
   743                 return writer.writeClass(cdef.sym);
   745                 return writer.writeClass(cdef.sym);
   744         } catch (ClassWriter.PoolOverflow ex) {
   746         } catch (ClassWriter.PoolOverflow ex) {
   745             log.error(cdef.pos(), "limit.pool");
   747             log.error(cdef.pos(), Errors.LimitPool);
   746         } catch (ClassWriter.StringOverflow ex) {
   748         } catch (ClassWriter.StringOverflow ex) {
   747             log.error(cdef.pos(), "limit.string.overflow",
   749             log.error(cdef.pos(),
   748                       ex.value.substring(0, 20));
   750                       Errors.LimitStringOverflow(ex.value.substring(0, 20)));
   749         } catch (CompletionFailure ex) {
   751         } catch (CompletionFailure ex) {
   750             chk.completionError(cdef.pos(), ex);
   752             chk.completionError(cdef.pos(), ex);
   751         }
   753         }
   752         return null;
   754         return null;
   753     }
   755     }
   762            = fileManager.getJavaFileForOutput(CLASS_OUTPUT,
   764            = fileManager.getJavaFileForOutput(CLASS_OUTPUT,
   763                                                cdef.sym.flatname.toString(),
   765                                                cdef.sym.flatname.toString(),
   764                                                JavaFileObject.Kind.SOURCE,
   766                                                JavaFileObject.Kind.SOURCE,
   765                                                null);
   767                                                null);
   766         if (inputFiles.contains(outFile)) {
   768         if (inputFiles.contains(outFile)) {
   767             log.error(cdef.pos(), "source.cant.overwrite.input.file", outFile);
   769             log.error(cdef.pos(), Errors.SourceCantOverwriteInputFile(outFile));
   768             return null;
   770             return null;
   769         } else {
   771         } else {
   770             try (BufferedWriter out = new BufferedWriter(outFile.openWriter())) {
   772             try (BufferedWriter out = new BufferedWriter(outFile.openWriter())) {
   771                 new Pretty(out, true).printUnit(env.toplevel, cdef);
   773                 new Pretty(out, true).printUnit(env.toplevel, cdef);
   772                 if (verbose)
   774                 if (verbose)
   789      *              or null if should be parsed from source
   791      *              or null if should be parsed from source
   790      *  @param c    the ClassSymbol to complete
   792      *  @param c    the ClassSymbol to complete
   791      */
   793      */
   792     public void readSourceFile(JCCompilationUnit tree, ClassSymbol c) throws CompletionFailure {
   794     public void readSourceFile(JCCompilationUnit tree, ClassSymbol c) throws CompletionFailure {
   793         if (completionFailureName == c.fullname) {
   795         if (completionFailureName == c.fullname) {
   794             throw new CompletionFailure(c, "user-selected completion failure by class name");
   796             JCDiagnostic msg =
       
   797                     diagFactory.fragment(Fragments.UserSelectedCompletionFailure);
       
   798             throw new CompletionFailure(c, msg);
   795         }
   799         }
   796         JavaFileObject filename = c.classfile;
   800         JavaFileObject filename = c.classfile;
   797         JavaFileObject prev = log.useSource(filename);
   801         JavaFileObject prev = log.useSource(filename);
   798 
   802 
   799         if (tree == null) {
   803         if (tree == null) {
   800             try {
   804             try {
   801                 tree = parse(filename, filename.getCharContent(false));
   805                 tree = parse(filename, filename.getCharContent(false));
   802             } catch (IOException e) {
   806             } catch (IOException e) {
   803                 log.error("error.reading.file", filename, JavacFileManager.getMessage(e));
   807                 log.error(Errors.ErrorReadingFile(filename, JavacFileManager.getMessage(e)));
   804                 tree = make.TopLevel(List.<JCTree>nil());
   808                 tree = make.TopLevel(List.<JCTree>nil());
   805             } finally {
   809             } finally {
   806                 log.useSource(prev);
   810                 log.useSource(prev);
   807             }
   811             }
   808         }
   812         }
   817         // complete a specific symbol, throw CompletionFailure.
   821         // complete a specific symbol, throw CompletionFailure.
   818         // Note that if module resolution failed, we may not even
   822         // Note that if module resolution failed, we may not even
   819         // have enough modules available to access java.lang, and
   823         // have enough modules available to access java.lang, and
   820         // so risk getting FatalError("no.java.lang") from MemberEnter.
   824         // so risk getting FatalError("no.java.lang") from MemberEnter.
   821         if (!modules.enter(List.of(tree), c)) {
   825         if (!modules.enter(List.of(tree), c)) {
   822             throw new CompletionFailure(c, diags.fragment("cant.resolve.modules"));
   826             throw new CompletionFailure(c, diags.fragment(Fragments.CantResolveModules));
   823         }
   827         }
   824 
   828 
   825         enter.complete(List.of(tree), c);
   829         enter.complete(List.of(tree), c);
   826 
   830 
   827         if (!taskListener.isEmpty()) {
   831         if (!taskListener.isEmpty()) {
   837                 tree.sourcefile.isNameCompatible("module-info",
   841                 tree.sourcefile.isNameCompatible("module-info",
   838                                                  JavaFileObject.Kind.SOURCE);
   842                                                  JavaFileObject.Kind.SOURCE);
   839             if (isModuleInfo) {
   843             if (isModuleInfo) {
   840                 if (enter.getEnv(tree.modle) == null) {
   844                 if (enter.getEnv(tree.modle) == null) {
   841                     JCDiagnostic diag =
   845                     JCDiagnostic diag =
   842                         diagFactory.fragment("file.does.not.contain.module");
   846                         diagFactory.fragment(Fragments.FileDoesNotContainModule);
   843                     throw new ClassFinder.BadClassFile(c, filename, diag, diagFactory);
   847                     throw new ClassFinder.BadClassFile(c, filename, diag, diagFactory);
   844                 }
   848                 }
   845             } else if (isPkgInfo) {
   849             } else if (isPkgInfo) {
   846                 if (enter.getEnv(tree.packge) == null) {
   850                 if (enter.getEnv(tree.packge) == null) {
   847                     JCDiagnostic diag =
   851                     JCDiagnostic diag =
   848                         diagFactory.fragment("file.does.not.contain.package",
   852                         diagFactory.fragment(Fragments.FileDoesNotContainPackage(c.location()));
   849                                                  c.location());
       
   850                     throw new ClassFinder.BadClassFile(c, filename, diag, diagFactory);
   853                     throw new ClassFinder.BadClassFile(c, filename, diag, diagFactory);
   851                 }
   854                 }
   852             } else {
   855             } else {
   853                 JCDiagnostic diag =
   856                 JCDiagnostic diag =
   854                         diagFactory.fragment("file.doesnt.contain.class",
   857                         diagFactory.fragment(Fragments.FileDoesntContainClass(c.getQualifiedName()));
   855                                             c.getQualifiedName());
       
   856                 throw new ClassFinder.BadClassFile(c, filename, diag, diagFactory);
   858                 throw new ClassFinder.BadClassFile(c, filename, diag, diagFactory);
   857             }
   859             }
   858         }
   860         }
   859 
   861 
   860         implicitSourceFilesRead = true;
   862         implicitSourceFilesRead = true;
  1175         if (!processAnnotations) {
  1177         if (!processAnnotations) {
  1176             // If there are no annotation processors present, and
  1178             // If there are no annotation processors present, and
  1177             // annotation processing is to occur with compilation,
  1179             // annotation processing is to occur with compilation,
  1178             // emit a warning.
  1180             // emit a warning.
  1179             if (options.isSet(PROC, "only")) {
  1181             if (options.isSet(PROC, "only")) {
  1180                 log.warning("proc.proc-only.requested.no.procs");
  1182                 log.warning(Warnings.ProcProcOnlyRequestedNoProcs);
  1181                 todo.clear();
  1183                 todo.clear();
  1182             }
  1184             }
  1183             // If not processing annotations, classnames must be empty
  1185             // If not processing annotations, classnames must be empty
  1184             if (!classnames.isEmpty()) {
  1186             if (!classnames.isEmpty()) {
  1185                 log.error("proc.no.explicit.annotation.processing.requested",
  1187                 log.error(Errors.ProcNoExplicitAnnotationProcessingRequested(classnames));
  1186                           classnames);
       
  1187             }
  1188             }
  1188             Assert.checkNull(deferredDiagnosticHandler);
  1189             Assert.checkNull(deferredDiagnosticHandler);
  1189             return ; // continue regular compilation
  1190             return ; // continue regular compilation
  1190         }
  1191         }
  1191 
  1192 
  1196             List<PackageSymbol> pckSymbols = List.nil();
  1197             List<PackageSymbol> pckSymbols = List.nil();
  1197             if (!classnames.isEmpty()) {
  1198             if (!classnames.isEmpty()) {
  1198                  // Check for explicit request for annotation
  1199                  // Check for explicit request for annotation
  1199                  // processing
  1200                  // processing
  1200                 if (!explicitAnnotationProcessingRequested()) {
  1201                 if (!explicitAnnotationProcessingRequested()) {
  1201                     log.error("proc.no.explicit.annotation.processing.requested",
  1202                     log.error(Errors.ProcNoExplicitAnnotationProcessingRequested(classnames));
  1202                               classnames);
       
  1203                     deferredDiagnosticHandler.reportDeferredDiagnostics();
  1203                     deferredDiagnosticHandler.reportDeferredDiagnostics();
  1204                     log.popDiagnosticHandler(deferredDiagnosticHandler);
  1204                     log.popDiagnosticHandler(deferredDiagnosticHandler);
  1205                     return ; // TODO: Will this halt compilation?
  1205                     return ; // TODO: Will this halt compilation?
  1206                 } else {
  1206                 } else {
  1207                     boolean errors = false;
  1207                     boolean errors = false;
  1250                 // doProcessing will have handled deferred diagnostics
  1250                 // doProcessing will have handled deferred diagnostics
  1251             } finally {
  1251             } finally {
  1252                 procEnvImpl.close();
  1252                 procEnvImpl.close();
  1253             }
  1253             }
  1254         } catch (CompletionFailure ex) {
  1254         } catch (CompletionFailure ex) {
  1255             log.error("cant.access", ex.sym, ex.getDetailValue());
  1255             log.error(Errors.CantAccess(ex.sym, ex.getDetailValue()));
  1256             if (deferredDiagnosticHandler != null) {
  1256             if (deferredDiagnosticHandler != null) {
  1257                 deferredDiagnosticHandler.reportDeferredDiagnostics();
  1257                 deferredDiagnosticHandler.reportDeferredDiagnostics();
  1258                 log.popDiagnosticHandler(deferredDiagnosticHandler);
  1258                 log.popDiagnosticHandler(deferredDiagnosticHandler);
  1259             }
  1259             }
  1260         }
  1260         }
  1621                     file = genCode(env, cdef);
  1621                     file = genCode(env, cdef);
  1622                 }
  1622                 }
  1623                 if (results != null && file != null)
  1623                 if (results != null && file != null)
  1624                     results.add(file);
  1624                     results.add(file);
  1625             } catch (IOException ex) {
  1625             } catch (IOException ex) {
  1626                 log.error(cdef.pos(), "class.cant.write",
  1626                 log.error(cdef.pos(),
  1627                           cdef.sym, ex.getMessage());
  1627                           Errors.ClassCantWrite(cdef.sym, ex.getMessage()));
  1628                 return;
  1628                 return;
  1629             } finally {
  1629             } finally {
  1630                 log.useSource(prev);
  1630                 log.useSource(prev);
  1631             }
  1631             }
  1632 
  1632 
  1709         if (errorCount() == 0
  1709         if (errorCount() == 0
  1710                 && annotationProcessingOccurred
  1710                 && annotationProcessingOccurred
  1711                 && implicitSourceFilesRead
  1711                 && implicitSourceFilesRead
  1712                 && implicitSourcePolicy == ImplicitSourcePolicy.UNSET) {
  1712                 && implicitSourcePolicy == ImplicitSourcePolicy.UNSET) {
  1713             if (explicitAnnotationProcessingRequested())
  1713             if (explicitAnnotationProcessingRequested())
  1714                 log.warning("proc.use.implicit");
  1714                 log.warning(Warnings.ProcUseImplicit);
  1715             else
  1715             else
  1716                 log.warning("proc.use.proc.or.implicit");
  1716                 log.warning(Warnings.ProcUseProcOrImplicit);
  1717         }
  1717         }
  1718         chk.reportDeferredDiagnostics();
  1718         chk.reportDeferredDiagnostics();
  1719         if (log.compressedOutput) {
  1719         if (log.compressedOutput) {
  1720             log.mandatoryNote(null, "compressed.diags");
  1720             log.mandatoryNote(null, Notes.CompressedDiags);
  1721         }
  1721         }
  1722     }
  1722     }
  1723 
  1723 
  1724     public void enterDone() {
  1724     public void enterDone() {
  1725         enterDone = true;
  1725         enterDone = true;
  1796                 } catch (IOException e) {
  1796                 } catch (IOException e) {
  1797                     // When javac uses JDK 7 as a baseline, this code would be
  1797                     // When javac uses JDK 7 as a baseline, this code would be
  1798                     // better written to set any/all exceptions from all the
  1798                     // better written to set any/all exceptions from all the
  1799                     // Closeables as suppressed exceptions on the FatalError
  1799                     // Closeables as suppressed exceptions on the FatalError
  1800                     // that is thrown.
  1800                     // that is thrown.
  1801                     JCDiagnostic msg = diagFactory.fragment("fatal.err.cant.close");
  1801                     JCDiagnostic msg = diagFactory.fragment(Fragments.FatalErrCantClose);
  1802                     throw new FatalError(msg, e);
  1802                     throw new FatalError(msg, e);
  1803                 }
  1803                 }
  1804             }
  1804             }
  1805             closeables = List.nil();
  1805             closeables = List.nil();
  1806         }
  1806         }