294 /** The attributor.
295 */
296 protected Check chk;
297
298 /** The flow analyzer.
299 */
300 protected Flow flow;
301
302 /** The modules visitor
303 */
304 protected Modules modules;
305
306 /** The module finder
307 */
308 protected ModuleFinder moduleFinder;
309
310 /** The diagnostics factory
311 */
312 protected JCDiagnostic.Factory diags;
313
314 /** The type eraser.
315 */
316 protected TransTypes transTypes;
317
318 /** The syntactic sugar desweetener.
319 */
320 protected Lower lower;
321
322 /** The annotation annotator.
323 */
324 protected Annotate annotate;
325
326 /** Force a completion failure on this name
327 */
328 protected final Name completionFailureName;
329
330 /** Type utilities.
331 */
332 protected Types types;
333
399 // catch completion problems with predefineds
400 syms = Symtab.instance(context);
401 } catch (CompletionFailure ex) {
402 // inlined Check.completionError as it is not initialized yet
403 log.error(Errors.CantAccess(ex.sym, ex.getDetailValue()));
404 }
405 source = Source.instance(context);
406 attr = Attr.instance(context);
407 analyzer = Analyzer.instance(context);
408 chk = Check.instance(context);
409 gen = Gen.instance(context);
410 flow = Flow.instance(context);
411 transTypes = TransTypes.instance(context);
412 lower = Lower.instance(context);
413 annotate = Annotate.instance(context);
414 types = Types.instance(context);
415 taskListener = MultiTaskListener.instance(context);
416 modules = Modules.instance(context);
417 moduleFinder = ModuleFinder.instance(context);
418 diags = Factory.instance(context);
419
420 finder.sourceCompleter = sourceCompleter;
421 modules.findPackageInFile = this::findPackageInFile;
422 moduleFinder.moduleNameFromSourceReader = this::readModuleName;
423
424 options = Options.instance(context);
425
426 verbose = options.isSet(VERBOSE);
427 sourceOutput = options.isSet(PRINTSOURCE); // used to be -s
428 lineDebugInfo = options.isUnset(G_CUSTOM) ||
429 options.isSet(G_CUSTOM, "lines");
430 genEndPos = options.isSet(XJCOV) ||
431 context.get(DiagnosticListener.class) != null;
432 devVerbose = options.isSet("dev");
433 processPcks = options.isSet("process.packages");
434 werror = options.isSet(WERROR);
435
436 verboseCompilePolicy = options.isSet("verboseCompilePolicy");
437
438 if (options.isSet("should-stop.at") &&
782 }
783 }
784
785 /** Compile a source file that has been accessed by the class finder.
786 * @param c The class the source file of which needs to be compiled.
787 */
788 private void readSourceFile(ClassSymbol c) throws CompletionFailure {
789 readSourceFile(null, c);
790 }
791
792 /** Compile a ClassSymbol from source, optionally using the given compilation unit as
793 * the source tree.
794 * @param tree the compilation unit in which the given ClassSymbol resides,
795 * or null if should be parsed from source
796 * @param c the ClassSymbol to complete
797 */
798 public void readSourceFile(JCCompilationUnit tree, ClassSymbol c) throws CompletionFailure {
799 if (completionFailureName == c.fullname) {
800 JCDiagnostic msg =
801 diagFactory.fragment(Fragments.UserSelectedCompletionFailure);
802 throw new CompletionFailure(c, msg);
803 }
804 JavaFileObject filename = c.classfile;
805 JavaFileObject prev = log.useSource(filename);
806
807 if (tree == null) {
808 try {
809 tree = parse(filename, filename.getCharContent(false));
810 } catch (IOException e) {
811 log.error(Errors.ErrorReadingFile(filename, JavacFileManager.getMessage(e)));
812 tree = make.TopLevel(List.<JCTree>nil());
813 } finally {
814 log.useSource(prev);
815 }
816 }
817
818 if (!taskListener.isEmpty()) {
819 TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, tree);
820 taskListener.started(e);
821 }
822
823 // Process module declarations.
824 // If module resolution fails, ignore trees, and if trying to
825 // complete a specific symbol, throw CompletionFailure.
826 // Note that if module resolution failed, we may not even
827 // have enough modules available to access java.lang, and
828 // so risk getting FatalError("no.java.lang") from MemberEnter.
829 if (!modules.enter(List.of(tree), c)) {
830 throw new CompletionFailure(c, diags.fragment(Fragments.CantResolveModules));
831 }
832
833 enter.complete(List.of(tree), c);
834
835 if (!taskListener.isEmpty()) {
836 TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, tree);
837 taskListener.finished(e);
838 }
839
840 if (enter.getEnv(c) == null) {
841 boolean isPkgInfo =
842 tree.sourcefile.isNameCompatible("package-info",
843 JavaFileObject.Kind.SOURCE);
844 boolean isModuleInfo =
845 tree.sourcefile.isNameCompatible("module-info",
846 JavaFileObject.Kind.SOURCE);
847 if (isModuleInfo) {
848 if (enter.getEnv(tree.modle) == null) {
849 JCDiagnostic diag =
850 diagFactory.fragment(Fragments.FileDoesNotContainModule);
851 throw new ClassFinder.BadClassFile(c, filename, diag, diagFactory);
852 }
853 } else if (isPkgInfo) {
854 if (enter.getEnv(tree.packge) == null) {
855 JCDiagnostic diag =
856 diagFactory.fragment(Fragments.FileDoesNotContainPackage(c.location()));
857 throw new ClassFinder.BadClassFile(c, filename, diag, diagFactory);
858 }
859 } else {
860 JCDiagnostic diag =
861 diagFactory.fragment(Fragments.FileDoesntContainClass(c.getQualifiedName()));
862 throw new ClassFinder.BadClassFile(c, filename, diag, diagFactory);
863 }
864 }
865
866 implicitSourceFilesRead = true;
867 }
868
869 /** Track when the JavaCompiler has been used to compile something. */
870 private boolean hasBeenUsed = false;
871 private long start_msec = 0;
872 public long elapsed_msec = 0;
873
874 public void compile(List<JavaFileObject> sourceFileObject)
875 throws Throwable {
876 compile(sourceFileObject, List.nil(), null, List.nil());
877 }
878
879 /**
880 * Main method: compile a list of files, return all compiled classes
881 *
882 * @param sourceFileObjects file objects to be compiled
|
294 /** The attributor.
295 */
296 protected Check chk;
297
298 /** The flow analyzer.
299 */
300 protected Flow flow;
301
302 /** The modules visitor
303 */
304 protected Modules modules;
305
306 /** The module finder
307 */
308 protected ModuleFinder moduleFinder;
309
310 /** The diagnostics factory
311 */
312 protected JCDiagnostic.Factory diags;
313
314 protected DeferredCompletionFailureHandler dcfh;
315
316 /** The type eraser.
317 */
318 protected TransTypes transTypes;
319
320 /** The syntactic sugar desweetener.
321 */
322 protected Lower lower;
323
324 /** The annotation annotator.
325 */
326 protected Annotate annotate;
327
328 /** Force a completion failure on this name
329 */
330 protected final Name completionFailureName;
331
332 /** Type utilities.
333 */
334 protected Types types;
335
401 // catch completion problems with predefineds
402 syms = Symtab.instance(context);
403 } catch (CompletionFailure ex) {
404 // inlined Check.completionError as it is not initialized yet
405 log.error(Errors.CantAccess(ex.sym, ex.getDetailValue()));
406 }
407 source = Source.instance(context);
408 attr = Attr.instance(context);
409 analyzer = Analyzer.instance(context);
410 chk = Check.instance(context);
411 gen = Gen.instance(context);
412 flow = Flow.instance(context);
413 transTypes = TransTypes.instance(context);
414 lower = Lower.instance(context);
415 annotate = Annotate.instance(context);
416 types = Types.instance(context);
417 taskListener = MultiTaskListener.instance(context);
418 modules = Modules.instance(context);
419 moduleFinder = ModuleFinder.instance(context);
420 diags = Factory.instance(context);
421 dcfh = DeferredCompletionFailureHandler.instance(context);
422
423 finder.sourceCompleter = sourceCompleter;
424 modules.findPackageInFile = this::findPackageInFile;
425 moduleFinder.moduleNameFromSourceReader = this::readModuleName;
426
427 options = Options.instance(context);
428
429 verbose = options.isSet(VERBOSE);
430 sourceOutput = options.isSet(PRINTSOURCE); // used to be -s
431 lineDebugInfo = options.isUnset(G_CUSTOM) ||
432 options.isSet(G_CUSTOM, "lines");
433 genEndPos = options.isSet(XJCOV) ||
434 context.get(DiagnosticListener.class) != null;
435 devVerbose = options.isSet("dev");
436 processPcks = options.isSet("process.packages");
437 werror = options.isSet(WERROR);
438
439 verboseCompilePolicy = options.isSet("verboseCompilePolicy");
440
441 if (options.isSet("should-stop.at") &&
785 }
786 }
787
788 /** Compile a source file that has been accessed by the class finder.
789 * @param c The class the source file of which needs to be compiled.
790 */
791 private void readSourceFile(ClassSymbol c) throws CompletionFailure {
792 readSourceFile(null, c);
793 }
794
795 /** Compile a ClassSymbol from source, optionally using the given compilation unit as
796 * the source tree.
797 * @param tree the compilation unit in which the given ClassSymbol resides,
798 * or null if should be parsed from source
799 * @param c the ClassSymbol to complete
800 */
801 public void readSourceFile(JCCompilationUnit tree, ClassSymbol c) throws CompletionFailure {
802 if (completionFailureName == c.fullname) {
803 JCDiagnostic msg =
804 diagFactory.fragment(Fragments.UserSelectedCompletionFailure);
805 throw new CompletionFailure(c, msg, dcfh);
806 }
807 JavaFileObject filename = c.classfile;
808 JavaFileObject prev = log.useSource(filename);
809
810 if (tree == null) {
811 try {
812 tree = parse(filename, filename.getCharContent(false));
813 } catch (IOException e) {
814 log.error(Errors.ErrorReadingFile(filename, JavacFileManager.getMessage(e)));
815 tree = make.TopLevel(List.<JCTree>nil());
816 } finally {
817 log.useSource(prev);
818 }
819 }
820
821 if (!taskListener.isEmpty()) {
822 TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, tree);
823 taskListener.started(e);
824 }
825
826 // Process module declarations.
827 // If module resolution fails, ignore trees, and if trying to
828 // complete a specific symbol, throw CompletionFailure.
829 // Note that if module resolution failed, we may not even
830 // have enough modules available to access java.lang, and
831 // so risk getting FatalError("no.java.lang") from MemberEnter.
832 if (!modules.enter(List.of(tree), c)) {
833 throw new CompletionFailure(c, diags.fragment(Fragments.CantResolveModules), dcfh);
834 }
835
836 enter.complete(List.of(tree), c);
837
838 if (!taskListener.isEmpty()) {
839 TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, tree);
840 taskListener.finished(e);
841 }
842
843 if (enter.getEnv(c) == null) {
844 boolean isPkgInfo =
845 tree.sourcefile.isNameCompatible("package-info",
846 JavaFileObject.Kind.SOURCE);
847 boolean isModuleInfo =
848 tree.sourcefile.isNameCompatible("module-info",
849 JavaFileObject.Kind.SOURCE);
850 if (isModuleInfo) {
851 if (enter.getEnv(tree.modle) == null) {
852 JCDiagnostic diag =
853 diagFactory.fragment(Fragments.FileDoesNotContainModule);
854 throw new ClassFinder.BadClassFile(c, filename, diag, diagFactory, dcfh);
855 }
856 } else if (isPkgInfo) {
857 if (enter.getEnv(tree.packge) == null) {
858 JCDiagnostic diag =
859 diagFactory.fragment(Fragments.FileDoesNotContainPackage(c.location()));
860 throw new ClassFinder.BadClassFile(c, filename, diag, diagFactory, dcfh);
861 }
862 } else {
863 JCDiagnostic diag =
864 diagFactory.fragment(Fragments.FileDoesntContainClass(c.getQualifiedName()));
865 throw new ClassFinder.BadClassFile(c, filename, diag, diagFactory, dcfh);
866 }
867 }
868
869 implicitSourceFilesRead = true;
870 }
871
872 /** Track when the JavaCompiler has been used to compile something. */
873 private boolean hasBeenUsed = false;
874 private long start_msec = 0;
875 public long elapsed_msec = 0;
876
877 public void compile(List<JavaFileObject> sourceFileObject)
878 throws Throwable {
879 compile(sourceFileObject, List.nil(), null, List.nil());
880 }
881
882 /**
883 * Main method: compile a list of files, return all compiled classes
884 *
885 * @param sourceFileObjects file objects to be compiled
|