< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java

Print this page

   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.javac.main;
  27 
  28 import java.io.*;

  29 import java.nio.file.FileSystemNotFoundException;
  30 import java.nio.file.InvalidPathException;
  31 import java.nio.file.ReadOnlyFileSystemException;
  32 import java.util.Collection;
  33 import java.util.Comparator;
  34 import java.util.HashMap;
  35 import java.util.HashSet;
  36 import java.util.LinkedHashMap;
  37 import java.util.LinkedHashSet;
  38 import java.util.Map;
  39 import java.util.MissingResourceException;

  40 import java.util.Queue;
  41 import java.util.ResourceBundle;

  42 import java.util.Set;
  43 import java.util.function.Function;
  44 import java.util.function.ToIntFunction;
  45 
  46 import javax.annotation.processing.Processor;
  47 import javax.lang.model.SourceVersion;
  48 import javax.lang.model.element.ElementVisitor;
  49 import javax.tools.DiagnosticListener;
  50 import javax.tools.JavaFileManager;
  51 import javax.tools.JavaFileObject;
  52 import javax.tools.JavaFileObject.Kind;
  53 import javax.tools.StandardLocation;
  54 
  55 import com.sun.source.util.TaskEvent;
  56 import com.sun.tools.javac.api.MultiTaskListener;
  57 import com.sun.tools.javac.code.*;
  58 import com.sun.tools.javac.code.Lint.LintCategory;
  59 import com.sun.tools.javac.code.Source.Feature;
  60 import com.sun.tools.javac.code.Symbol.ClassSymbol;
  61 import com.sun.tools.javac.code.Symbol.CompletionFailure;

  68 import com.sun.tools.javac.platform.PlatformDescription;
  69 import com.sun.tools.javac.processing.*;
  70 import com.sun.tools.javac.tree.*;
  71 import com.sun.tools.javac.tree.JCTree.JCClassDecl;
  72 import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
  73 import com.sun.tools.javac.tree.JCTree.JCExpression;
  74 import com.sun.tools.javac.tree.JCTree.JCLambda;
  75 import com.sun.tools.javac.tree.JCTree.JCMemberReference;
  76 import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
  77 import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
  78 import com.sun.tools.javac.util.*;
  79 import com.sun.tools.javac.util.Context.Key;
  80 import com.sun.tools.javac.util.DefinedBy.Api;
  81 import com.sun.tools.javac.util.JCDiagnostic.Factory;
  82 import com.sun.tools.javac.util.Log.DiagnosticHandler;
  83 import com.sun.tools.javac.util.Log.DiscardDiagnosticHandler;
  84 import com.sun.tools.javac.util.Log.WriterKind;
  85 
  86 import static com.sun.tools.javac.code.Kinds.Kind.*;
  87 
  88 import com.sun.tools.javac.code.Lint;
  89 import com.sun.tools.javac.code.Lint.LintCategory;
  90 import com.sun.tools.javac.code.Symbol.ModuleSymbol;
  91 
  92 import com.sun.tools.javac.resources.CompilerProperties.Errors;
  93 import com.sun.tools.javac.resources.CompilerProperties.Fragments;
  94 import com.sun.tools.javac.resources.CompilerProperties.Notes;
  95 import com.sun.tools.javac.resources.CompilerProperties.Warnings;
  96 
  97 import static com.sun.tools.javac.code.TypeTag.CLASS;
  98 import static com.sun.tools.javac.main.Option.*;
  99 import com.sun.tools.javac.tree.JCTree.JCBindingPattern;
 100 import com.sun.tools.javac.tree.JCTree.JCInstanceOf;
 101 import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag.*;
 102 
 103 import static javax.tools.StandardLocation.CLASS_OUTPUT;
 104 import static javax.tools.StandardLocation.ANNOTATION_PROCESSOR_PATH;
 105 
 106 import com.sun.tools.javac.tree.JCTree.JCModuleDecl;
 107 import com.sun.tools.javac.tree.JCTree.JCRecordPattern;
 108 import com.sun.tools.javac.tree.JCTree.JCSwitch;
 109 import com.sun.tools.javac.tree.JCTree.JCSwitchExpression;

 362      * Command line options.
 363      */
 364     protected Options options;
 365 
 366     protected Context context;
 367 
 368     /**
 369      * Flag set if any annotation processing occurred.
 370      **/
 371     protected boolean annotationProcessingOccurred;
 372 
 373     /**
 374      * Flag set if any implicit source files read.
 375      **/
 376     protected boolean implicitSourceFilesRead;
 377 
 378     private boolean enterDone;
 379 
 380     protected CompileStates compileStates;
 381 


 382     /** Construct a new compiler using a shared context.
 383      */
 384     @SuppressWarnings("this-escape")
 385     public JavaCompiler(Context context) {
 386         this.context = context;
 387         context.put(compilerKey, this);
 388 
 389         // if fileManager not already set, register the JavacFileManager to be used
 390         if (context.get(JavaFileManager.class) == null)
 391             JavacFileManager.preRegister(context);
 392 
 393         names = Names.instance(context);
 394         log = Log.instance(context);
 395         diagFactory = JCDiagnostic.Factory.instance(context);
 396         finder = ClassFinder.instance(context);
 397         reader = ClassReader.instance(context);
 398         make = TreeMaker.instance(context);
 399         writer = ClassWriter.instance(context);
 400         jniWriter = JNIWriter.instance(context);
 401         enter = Enter.instance(context);

1028         Set<JavaFileObject> filesSoFar = new HashSet<>();
1029         for (JavaFileObject fileObject : fileObjects) {
1030             if (!filesSoFar.contains(fileObject)) {
1031                 filesSoFar.add(fileObject);
1032                 trees.append(parse(fileObject));
1033             }
1034         }
1035         return trees.toList();
1036     }
1037 
1038    /**
1039     * Returns true iff the compilation will continue after annotation processing
1040     * is done.
1041     */
1042     public boolean continueAfterProcessAnnotations() {
1043         return !shouldStop(CompileState.ATTR);
1044     }
1045 
1046     public List<JCCompilationUnit> initModules(List<JCCompilationUnit> roots) {
1047         modules.initModules(roots);









1048         if (roots.isEmpty()) {
1049             enterDone();
1050         }
1051         return roots;
1052     }
1053 
1054     /**
1055      * Enter the symbols found in a list of parse trees.
1056      * As a side-effect, this puts elements on the "todo" list.
1057      * Also stores a list of all top level classes in rootClasses.
1058      */
1059     public List<JCCompilationUnit> enterTrees(List<JCCompilationUnit> roots) {
1060         //enter symbols for all files
1061         if (!taskListener.isEmpty()) {
1062             for (JCCompilationUnit unit: roots) {
1063                 TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, unit);
1064                 taskListener.started(e);
1065             }
1066         }
1067 

1641 
1642             make.at(Position.FIRSTPOS);
1643             TreeMaker localMake = make.forToplevel(env.toplevel);
1644 
1645             if (env.tree.hasTag(JCTree.Tag.PACKAGEDEF) || env.tree.hasTag(JCTree.Tag.MODULEDEF)) {
1646                 if (!(sourceOutput)) {
1647                     if (shouldStop(CompileState.LOWER))
1648                         return;
1649                     List<JCTree> def = lower.translateTopLevelClass(env, env.tree, localMake);
1650                     if (def.head != null) {
1651                         Assert.check(def.tail.isEmpty());
1652                         results.add(new Pair<>(env, (JCClassDecl)def.head));
1653                     }
1654                 }
1655                 return;
1656             }
1657 
1658             if (shouldStop(CompileState.TRANSTYPES))
1659                 return;
1660 







1661             env.tree = transTypes.translateTopLevelClass(env.tree, localMake);
1662             compileStates.put(env, CompileState.TRANSTYPES);
1663 
1664             if (shouldStop(CompileState.TRANSPATTERNS))
1665                 return;
1666 
1667             if (scanner.hasPatterns) {
1668                 env.tree = TransPatterns.instance(context).translateTopLevelClass(env, env.tree, localMake);

1669             }
1670 
1671             compileStates.put(env, CompileState.TRANSPATTERNS);
1672 
1673             if (shouldStop(CompileState.LOWER))
1674                 return;
1675 
1676             if (sourceOutput) {
1677                 //emit standard Java source file, only for compilation
1678                 //units enumerated explicitly on the command line
1679                 JCClassDecl cdef = (JCClassDecl)env.tree;
1680                 if (untranslated instanceof JCClassDecl classDecl &&
1681                     rootClasses.contains(classDecl)) {
1682                     results.add(new Pair<>(env, cdef));
1683                 }
1684                 return;
1685             }
1686 
1687             //translate out inner classes
1688             List<JCTree> cdefs = lower.translateTopLevelClass(env, env.tree, localMake);

1696                     return;
1697 
1698                 for (JCTree def : cdefs) {
1699                     LambdaToMethod.instance(context).translateTopLevelClass(env, def, localMake);
1700                 }
1701                 compileStates.put(env, CompileState.UNLAMBDA);
1702             }
1703 
1704             //generate code for each class
1705             for (List<JCTree> l = cdefs; l.nonEmpty(); l = l.tail) {
1706                 JCClassDecl cdef = (JCClassDecl)l.head;
1707                 results.add(new Pair<>(env, cdef));
1708             }
1709         }
1710         finally {
1711             log.useSource(prev);
1712         }
1713 
1714     }
1715 

































1716     /** Generates the source or class file for a list of classes.
1717      * The decision to generate a source file or a class file is
1718      * based upon the compiler's options.
1719      * Generation stops if an error occurs while writing files.
1720      */
1721     public void generate(Queue<Pair<Env<AttrContext>, JCClassDecl>> queue) {
1722         generate(queue, null);
1723     }
1724 
1725     public void generate(Queue<Pair<Env<AttrContext>, JCClassDecl>> queue, Queue<JavaFileObject> results) {
1726         if (shouldStop(CompileState.GENERATE))
1727             return;
1728 
1729         for (Pair<Env<AttrContext>, JCClassDecl> x: queue) {
1730             Env<AttrContext> env = x.fst;
1731             JCClassDecl cdef = x.snd;
1732 
1733             if (verboseCompilePolicy) {
1734                 printNote("[generate " + (sourceOutput ? " source" : "code") + " " + cdef.sym + "]");
1735             }

1851                 log.warning(Warnings.ProcUseImplicit);
1852             else
1853                 log.warning(Warnings.ProcUseProcOrImplicit);
1854         }
1855         chk.reportDeferredDiagnostics();
1856         preview.reportDeferredDiagnostics();
1857         if (log.compressedOutput) {
1858             log.mandatoryNote(null, Notes.CompressedDiags);
1859         }
1860     }
1861 
1862     public void enterDone() {
1863         enterDone = true;
1864         annotate.enterDone();
1865     }
1866 
1867     public boolean isEnterDone() {
1868         return enterDone;
1869     }
1870 




1871     private Name readModuleName(JavaFileObject fo) {
1872         return parseAndGetName(fo, t -> {
1873             JCModuleDecl md = t.getModuleDecl();
1874 
1875             return md != null ? TreeInfo.fullName(md.getName()) : null;
1876         });
1877     }
1878 
1879     private Name findPackageInFile(JavaFileObject fo) {
1880         return parseAndGetName(fo, t -> t.getPackage() != null ?
1881                                         TreeInfo.fullName(t.getPackage().getPackageName()) : null);
1882     }
1883 
1884     private Name parseAndGetName(JavaFileObject fo,
1885                                  Function<JCTree.JCCompilationUnit, Name> tree2Name) {
1886         DiagnosticHandler dh = log.new DiscardDiagnosticHandler();
1887         JavaFileObject prevSource = log.useSource(fo);
1888         try {
1889             JCTree.JCCompilationUnit t = parse(fo, fo.getCharContent(false), true);
1890             return tree2Name.apply(t);

   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.javac.main;
  27 
  28 import java.io.*;
  29 import java.lang.module.Configuration;
  30 import java.nio.file.FileSystemNotFoundException;
  31 import java.nio.file.InvalidPathException;
  32 import java.nio.file.ReadOnlyFileSystemException;
  33 import java.util.Collection;
  34 import java.util.Comparator;
  35 import java.util.HashMap;
  36 import java.util.HashSet;
  37 import java.util.LinkedHashMap;
  38 import java.util.LinkedHashSet;
  39 import java.util.Map;
  40 import java.util.MissingResourceException;
  41 import java.util.Optional;
  42 import java.util.Queue;
  43 import java.util.ResourceBundle;
  44 import java.util.ServiceLoader;
  45 import java.util.Set;
  46 import java.util.function.Function;
  47 import java.util.function.ToIntFunction;
  48 
  49 import javax.annotation.processing.Processor;
  50 import javax.lang.model.SourceVersion;
  51 import javax.lang.model.element.ElementVisitor;
  52 import javax.tools.DiagnosticListener;
  53 import javax.tools.JavaFileManager;
  54 import javax.tools.JavaFileObject;
  55 import javax.tools.JavaFileObject.Kind;
  56 import javax.tools.StandardLocation;
  57 
  58 import com.sun.source.util.TaskEvent;
  59 import com.sun.tools.javac.api.MultiTaskListener;
  60 import com.sun.tools.javac.code.*;
  61 import com.sun.tools.javac.code.Lint.LintCategory;
  62 import com.sun.tools.javac.code.Source.Feature;
  63 import com.sun.tools.javac.code.Symbol.ClassSymbol;
  64 import com.sun.tools.javac.code.Symbol.CompletionFailure;

  71 import com.sun.tools.javac.platform.PlatformDescription;
  72 import com.sun.tools.javac.processing.*;
  73 import com.sun.tools.javac.tree.*;
  74 import com.sun.tools.javac.tree.JCTree.JCClassDecl;
  75 import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
  76 import com.sun.tools.javac.tree.JCTree.JCExpression;
  77 import com.sun.tools.javac.tree.JCTree.JCLambda;
  78 import com.sun.tools.javac.tree.JCTree.JCMemberReference;
  79 import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
  80 import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
  81 import com.sun.tools.javac.util.*;
  82 import com.sun.tools.javac.util.Context.Key;
  83 import com.sun.tools.javac.util.DefinedBy.Api;
  84 import com.sun.tools.javac.util.JCDiagnostic.Factory;
  85 import com.sun.tools.javac.util.Log.DiagnosticHandler;
  86 import com.sun.tools.javac.util.Log.DiscardDiagnosticHandler;
  87 import com.sun.tools.javac.util.Log.WriterKind;
  88 
  89 import static com.sun.tools.javac.code.Kinds.Kind.*;
  90 


  91 import com.sun.tools.javac.code.Symbol.ModuleSymbol;
  92 
  93 import com.sun.tools.javac.resources.CompilerProperties.Errors;
  94 import com.sun.tools.javac.resources.CompilerProperties.Fragments;
  95 import com.sun.tools.javac.resources.CompilerProperties.Notes;
  96 import com.sun.tools.javac.resources.CompilerProperties.Warnings;
  97 
  98 import static com.sun.tools.javac.code.TypeTag.CLASS;
  99 import static com.sun.tools.javac.main.Option.*;
 100 import com.sun.tools.javac.tree.JCTree.JCBindingPattern;
 101 import com.sun.tools.javac.tree.JCTree.JCInstanceOf;
 102 import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag.*;
 103 
 104 import static javax.tools.StandardLocation.CLASS_OUTPUT;
 105 import static javax.tools.StandardLocation.ANNOTATION_PROCESSOR_PATH;
 106 
 107 import com.sun.tools.javac.tree.JCTree.JCModuleDecl;
 108 import com.sun.tools.javac.tree.JCTree.JCRecordPattern;
 109 import com.sun.tools.javac.tree.JCTree.JCSwitch;
 110 import com.sun.tools.javac.tree.JCTree.JCSwitchExpression;

 363      * Command line options.
 364      */
 365     protected Options options;
 366 
 367     protected Context context;
 368 
 369     /**
 370      * Flag set if any annotation processing occurred.
 371      **/
 372     protected boolean annotationProcessingOccurred;
 373 
 374     /**
 375      * Flag set if any implicit source files read.
 376      **/
 377     protected boolean implicitSourceFilesRead;
 378 
 379     private boolean enterDone;
 380 
 381     protected CompileStates compileStates;
 382 
 383     private boolean hasCodeReflectionModule;
 384 
 385     /** Construct a new compiler using a shared context.
 386      */
 387     @SuppressWarnings("this-escape")
 388     public JavaCompiler(Context context) {
 389         this.context = context;
 390         context.put(compilerKey, this);
 391 
 392         // if fileManager not already set, register the JavacFileManager to be used
 393         if (context.get(JavaFileManager.class) == null)
 394             JavacFileManager.preRegister(context);
 395 
 396         names = Names.instance(context);
 397         log = Log.instance(context);
 398         diagFactory = JCDiagnostic.Factory.instance(context);
 399         finder = ClassFinder.instance(context);
 400         reader = ClassReader.instance(context);
 401         make = TreeMaker.instance(context);
 402         writer = ClassWriter.instance(context);
 403         jniWriter = JNIWriter.instance(context);
 404         enter = Enter.instance(context);

1031         Set<JavaFileObject> filesSoFar = new HashSet<>();
1032         for (JavaFileObject fileObject : fileObjects) {
1033             if (!filesSoFar.contains(fileObject)) {
1034                 filesSoFar.add(fileObject);
1035                 trees.append(parse(fileObject));
1036             }
1037         }
1038         return trees.toList();
1039     }
1040 
1041    /**
1042     * Returns true iff the compilation will continue after annotation processing
1043     * is done.
1044     */
1045     public boolean continueAfterProcessAnnotations() {
1046         return !shouldStop(CompileState.ATTR);
1047     }
1048 
1049     public List<JCCompilationUnit> initModules(List<JCCompilationUnit> roots) {
1050         modules.initModules(roots);
1051 
1052         if (modules.modulesInitialized()) {
1053             // This has to happen precisely here. At this point, we have all we need to
1054             // determine whether jdk.incubator.module is part of the module graph
1055             // but we have yet to trigger an ENTER event. This gives the code reflection plugin
1056             // a window to check whether code reflection should be enabled for this compilation unit.
1057             hasCodeReflectionModule = modules.getObservableModule(names.jdk_incubator_code) != null;
1058         }
1059 
1060         if (roots.isEmpty()) {
1061             enterDone();
1062         }
1063         return roots;
1064     }
1065 
1066     /**
1067      * Enter the symbols found in a list of parse trees.
1068      * As a side-effect, this puts elements on the "todo" list.
1069      * Also stores a list of all top level classes in rootClasses.
1070      */
1071     public List<JCCompilationUnit> enterTrees(List<JCCompilationUnit> roots) {
1072         //enter symbols for all files
1073         if (!taskListener.isEmpty()) {
1074             for (JCCompilationUnit unit: roots) {
1075                 TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, unit);
1076                 taskListener.started(e);
1077             }
1078         }
1079 

1653 
1654             make.at(Position.FIRSTPOS);
1655             TreeMaker localMake = make.forToplevel(env.toplevel);
1656 
1657             if (env.tree.hasTag(JCTree.Tag.PACKAGEDEF) || env.tree.hasTag(JCTree.Tag.MODULEDEF)) {
1658                 if (!(sourceOutput)) {
1659                     if (shouldStop(CompileState.LOWER))
1660                         return;
1661                     List<JCTree> def = lower.translateTopLevelClass(env, env.tree, localMake);
1662                     if (def.head != null) {
1663                         Assert.check(def.tail.isEmpty());
1664                         results.add(new Pair<>(env, (JCClassDecl)def.head));
1665                     }
1666                 }
1667                 return;
1668             }
1669 
1670             if (shouldStop(CompileState.TRANSTYPES))
1671                 return;
1672 
1673             if (Feature.REFLECT_METHODS.allowedInSource(source)) {
1674                 Optional<CodeReflectionTransformer> reflectMethods = reflectMethods();
1675                 if (reflectMethods.isPresent()) {
1676                     env.tree = reflectMethods.get().translateTopLevelClass(context, env.tree, localMake);
1677                 }
1678             }
1679 
1680             env.tree = transTypes.translateTopLevelClass(env.tree, localMake);
1681             compileStates.put(env, CompileState.TRANSTYPES);
1682 
1683             if (shouldStop(CompileState.TRANSPATTERNS))
1684                 return;
1685 
1686             if (scanner.hasPatterns) {
1687                 env.tree = TransPatterns.instance(context)
1688                         .translateTopLevelClass(env, env.tree, localMake);
1689             }
1690 
1691             compileStates.put(env, CompileState.TRANSPATTERNS);
1692 
1693             if (shouldStop(CompileState.LOWER))
1694                 return;
1695 
1696             if (sourceOutput) {
1697                 //emit standard Java source file, only for compilation
1698                 //units enumerated explicitly on the command line
1699                 JCClassDecl cdef = (JCClassDecl)env.tree;
1700                 if (untranslated instanceof JCClassDecl classDecl &&
1701                     rootClasses.contains(classDecl)) {
1702                     results.add(new Pair<>(env, cdef));
1703                 }
1704                 return;
1705             }
1706 
1707             //translate out inner classes
1708             List<JCTree> cdefs = lower.translateTopLevelClass(env, env.tree, localMake);

1716                     return;
1717 
1718                 for (JCTree def : cdefs) {
1719                     LambdaToMethod.instance(context).translateTopLevelClass(env, def, localMake);
1720                 }
1721                 compileStates.put(env, CompileState.UNLAMBDA);
1722             }
1723 
1724             //generate code for each class
1725             for (List<JCTree> l = cdefs; l.nonEmpty(); l = l.tail) {
1726                 JCClassDecl cdef = (JCClassDecl)l.head;
1727                 results.add(new Pair<>(env, cdef));
1728             }
1729         }
1730         finally {
1731             log.useSource(prev);
1732         }
1733 
1734     }
1735 
1736     Optional<CodeReflectionTransformer> reflectMethods() {
1737         return CodeReflectionSupport.CODE_LAYER != null ?
1738                 ServiceLoader.load(CodeReflectionSupport.CODE_LAYER, CodeReflectionTransformer.class).findFirst() :
1739                 Optional.empty();
1740     }
1741 
1742     static class CodeReflectionSupport {
1743         static final ModuleLayer CODE_LAYER;
1744 
1745         static {
1746             if (ModuleLayer.boot().findModule("jdk.incubator.code").isPresent()) {
1747                 // we are in an exploded build, so just use the boot layer
1748                 CODE_LAYER = ModuleLayer.boot();
1749             } else if (java.lang.module.ModuleFinder.ofSystem().find("jdk.incubator.code").isPresent()) {
1750                 // the code module is installed, but not in the boot layer, create a new layer which contains it
1751                 ModuleLayer parent = ModuleLayer.boot();
1752                 Configuration cf = parent.configuration()
1753                         .resolve(java.lang.module.ModuleFinder.of(), java.lang.module.ModuleFinder.ofSystem(), Set.of("jdk.incubator.code"));
1754                 ClassLoader scl = ClassLoader.getSystemClassLoader();
1755                 CODE_LAYER = parent.defineModulesWithOneLoader(cf, scl);
1756                 Module codeReflectionModule = CODE_LAYER.findModule("jdk.incubator.code").get();
1757                 Module jdkCompilerModule = JavaCompiler.class.getModule();
1758                 // We need to add exports all jdk.compiler packages so that the plugin can use them
1759                 for (String packageName : jdkCompilerModule.getPackages()) {
1760                     jdkCompilerModule.addExports(packageName, codeReflectionModule);
1761                 }
1762             } else {
1763                 // if we run in bootstrap mode, there might be no jdk.incubator.code
1764                 CODE_LAYER = null;
1765             }
1766         }
1767     }
1768 
1769     /** Generates the source or class file for a list of classes.
1770      * The decision to generate a source file or a class file is
1771      * based upon the compiler's options.
1772      * Generation stops if an error occurs while writing files.
1773      */
1774     public void generate(Queue<Pair<Env<AttrContext>, JCClassDecl>> queue) {
1775         generate(queue, null);
1776     }
1777 
1778     public void generate(Queue<Pair<Env<AttrContext>, JCClassDecl>> queue, Queue<JavaFileObject> results) {
1779         if (shouldStop(CompileState.GENERATE))
1780             return;
1781 
1782         for (Pair<Env<AttrContext>, JCClassDecl> x: queue) {
1783             Env<AttrContext> env = x.fst;
1784             JCClassDecl cdef = x.snd;
1785 
1786             if (verboseCompilePolicy) {
1787                 printNote("[generate " + (sourceOutput ? " source" : "code") + " " + cdef.sym + "]");
1788             }

1904                 log.warning(Warnings.ProcUseImplicit);
1905             else
1906                 log.warning(Warnings.ProcUseProcOrImplicit);
1907         }
1908         chk.reportDeferredDiagnostics();
1909         preview.reportDeferredDiagnostics();
1910         if (log.compressedOutput) {
1911             log.mandatoryNote(null, Notes.CompressedDiags);
1912         }
1913     }
1914 
1915     public void enterDone() {
1916         enterDone = true;
1917         annotate.enterDone();
1918     }
1919 
1920     public boolean isEnterDone() {
1921         return enterDone;
1922     }
1923 
1924     public boolean hasCodeReflectionModule() {
1925         return hasCodeReflectionModule;
1926     }
1927 
1928     private Name readModuleName(JavaFileObject fo) {
1929         return parseAndGetName(fo, t -> {
1930             JCModuleDecl md = t.getModuleDecl();
1931 
1932             return md != null ? TreeInfo.fullName(md.getName()) : null;
1933         });
1934     }
1935 
1936     private Name findPackageInFile(JavaFileObject fo) {
1937         return parseAndGetName(fo, t -> t.getPackage() != null ?
1938                                         TreeInfo.fullName(t.getPackage().getPackageName()) : null);
1939     }
1940 
1941     private Name parseAndGetName(JavaFileObject fo,
1942                                  Function<JCTree.JCCompilationUnit, Name> tree2Name) {
1943         DiagnosticHandler dh = log.new DiscardDiagnosticHandler();
1944         JavaFileObject prevSource = log.useSource(fo);
1945         try {
1946             JCTree.JCCompilationUnit t = parse(fo, fo.getCharContent(false), true);
1947             return tree2Name.apply(t);
< prev index next >