< 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.EnumSet;
  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.Queue;
  42 import java.util.ResourceBundle;

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

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


  90 import com.sun.tools.javac.resources.CompilerProperties.Errors;
  91 import com.sun.tools.javac.resources.CompilerProperties.Fragments;
  92 import com.sun.tools.javac.resources.CompilerProperties.Notes;
  93 import com.sun.tools.javac.resources.CompilerProperties.Warnings;
  94 
  95 import static com.sun.tools.javac.code.TypeTag.CLASS;
  96 import static com.sun.tools.javac.main.Option.*;
  97 import com.sun.tools.javac.tree.JCTree.JCBindingPattern;
  98 import com.sun.tools.javac.tree.JCTree.JCInstanceOf;
  99 import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag.*;
 100 
 101 import static javax.tools.StandardLocation.CLASS_OUTPUT;
 102 import static javax.tools.StandardLocation.ANNOTATION_PROCESSOR_PATH;
 103 
 104 import com.sun.tools.javac.tree.JCTree.JCModuleDecl;
 105 import com.sun.tools.javac.tree.JCTree.JCRecordPattern;
 106 import com.sun.tools.javac.tree.JCTree.JCSwitch;
 107 import com.sun.tools.javac.tree.JCTree.JCSwitchExpression;
 108 
 109 /** This class could be the main entry point for GJC when GJC is used as a

 750                 tree = (tree == null) ? make.Ident(names.fromString(s))
 751                                       : make.Select(tree, names.fromString(s));
 752             }
 753             JCCompilationUnit toplevel =
 754                 make.TopLevel(List.nil());
 755             toplevel.modle = msym;
 756             toplevel.packge = msym.unnamedPackage;
 757             return attr.attribIdent(tree, toplevel);
 758         } finally {
 759             log.useSource(prev);
 760         }
 761     }
 762 
 763     /** Generate code and emit a class file for a given class
 764      *  @param env    The attribution environment of the outermost class
 765      *                containing this class.
 766      *  @param cdef   The class definition from which code is generated.
 767      */
 768     JavaFileObject genCode(Env<AttrContext> env, JCClassDecl cdef) throws IOException {
 769         try {







 770             if (gen.genClass(env, cdef) && (errorCount() == 0))
 771                 return writer.writeClass(cdef.sym);
 772         } catch (ClassWriter.PoolOverflow ex) {
 773             log.error(cdef.pos(), Errors.LimitPool);
 774         } catch (ClassWriter.StringOverflow ex) {
 775             log.error(cdef.pos(),
 776                       Errors.LimitStringOverflow(ex.value.substring(0, 20)));
 777         } catch (CompletionFailure ex) {
 778             chk.completionError(cdef.pos(), ex);
 779         }
 780         return null;
 781     }
 782 
 783     /** Emit plain Java source for a class.
 784      *  @param env    The attribution environment of the outermost class
 785      *                containing this class.
 786      *  @param cdef   The class definition to be printed.
 787      */
 788     JavaFileObject printSource(Env<AttrContext> env, JCClassDecl cdef) throws IOException {
 789         JavaFileObject outFile

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

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

1454      * Check for various things to warn about in an attributed parse tree.
1455      */
1456     protected void warn(Env<AttrContext> env, Queue<Env<AttrContext>> results) {
1457         if (compileStates.isDone(env, CompileState.WARN)) {
1458             results.add(env);
1459             return;
1460         }
1461 
1462         if (shouldStop(CompileState.WARN))
1463             return;
1464 
1465         if (verboseCompilePolicy)
1466             printNote("[warn " + env.enclClass.sym + "]");
1467         JavaFileObject prev = log.useSource(
1468                                             env.enclClass.sym.sourcefile != null ?
1469                                             env.enclClass.sym.sourcefile :
1470                                             env.toplevel.sourcefile);
1471         try {
1472             warningAnalyzer.analyzeTree(env);
1473             compileStates.put(env, CompileState.WARN);












1474             results.add(env);
1475         }
1476         finally {
1477             log.useSource(prev);
1478         }
1479     }
1480 
1481     private TaskEvent newAnalyzeTaskEvent(Env<AttrContext> env) {
1482         JCCompilationUnit toplevel = env.toplevel;
1483         ClassSymbol sym;
1484         if (env.enclClass.sym == syms.predefClass) {
1485             if (TreeInfo.isModuleInfo(toplevel)) {
1486                 sym = toplevel.modle.module_info;
1487             } else if (TreeInfo.isPackageInfo(toplevel)) {
1488                 sym = toplevel.packge.package_info;
1489             } else {
1490                 throw new IllegalStateException("unknown env.toplevel");
1491             }
1492         } else {
1493             sym = env.enclClass.sym;

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














































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

   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.lang.reflect.Method;
  31 import java.nio.file.FileSystemNotFoundException;
  32 import java.nio.file.InvalidPathException;
  33 import java.nio.file.ReadOnlyFileSystemException;
  34 import java.util.Collection;
  35 import java.util.Comparator;
  36 import java.util.EnumSet;
  37 import java.util.HashMap;
  38 import java.util.HashSet;
  39 import java.util.LinkedHashMap;
  40 import java.util.LinkedHashSet;
  41 import java.util.Map;
  42 import java.util.MissingResourceException;
  43 import java.util.Optional;
  44 import java.util.Queue;
  45 import java.util.ResourceBundle;
  46 import java.util.ServiceLoader;
  47 import java.util.Set;
  48 import java.util.function.Function;
  49 import java.util.function.ToIntFunction;
  50 
  51 import javax.annotation.processing.Processor;
  52 import javax.lang.model.SourceVersion;
  53 import javax.lang.model.element.ElementVisitor;
  54 import javax.tools.DiagnosticListener;
  55 import javax.tools.JavaFileManager;
  56 import javax.tools.JavaFileObject;
  57 import javax.tools.JavaFileObject.Kind;
  58 import javax.tools.StandardLocation;
  59 
  60 import com.sun.source.util.TaskEvent;
  61 import com.sun.tools.javac.api.MultiTaskListener;
  62 import com.sun.tools.javac.code.*;
  63 import com.sun.tools.javac.code.Lint.LintCategory;
  64 import com.sun.tools.javac.code.Source.Feature;
  65 import com.sun.tools.javac.code.Symbol.ClassSymbol;
  66 import com.sun.tools.javac.code.Symbol.CompletionFailure;

  74 import com.sun.tools.javac.platform.PlatformDescription;
  75 import com.sun.tools.javac.processing.*;
  76 import com.sun.tools.javac.tree.*;
  77 import com.sun.tools.javac.tree.JCTree.JCClassDecl;
  78 import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
  79 import com.sun.tools.javac.tree.JCTree.JCExpression;
  80 import com.sun.tools.javac.tree.JCTree.JCLambda;
  81 import com.sun.tools.javac.tree.JCTree.JCMemberReference;
  82 import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
  83 import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
  84 import com.sun.tools.javac.util.*;
  85 import com.sun.tools.javac.util.Context.Key;
  86 import com.sun.tools.javac.util.DefinedBy.Api;
  87 import com.sun.tools.javac.util.JCDiagnostic.Factory;
  88 import com.sun.tools.javac.util.Log.DiagnosticHandler;
  89 import com.sun.tools.javac.util.Log.DiscardDiagnosticHandler;
  90 import com.sun.tools.javac.util.Log.WriterKind;
  91 
  92 import static com.sun.tools.javac.code.Kinds.Kind.*;
  93 
  94 import com.sun.tools.javac.code.Symbol.ModuleSymbol;
  95 
  96 import com.sun.tools.javac.resources.CompilerProperties.Errors;
  97 import com.sun.tools.javac.resources.CompilerProperties.Fragments;
  98 import com.sun.tools.javac.resources.CompilerProperties.Notes;
  99 import com.sun.tools.javac.resources.CompilerProperties.Warnings;
 100 
 101 import static com.sun.tools.javac.code.TypeTag.CLASS;
 102 import static com.sun.tools.javac.main.Option.*;
 103 import com.sun.tools.javac.tree.JCTree.JCBindingPattern;
 104 import com.sun.tools.javac.tree.JCTree.JCInstanceOf;
 105 import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag.*;
 106 
 107 import static javax.tools.StandardLocation.CLASS_OUTPUT;
 108 import static javax.tools.StandardLocation.ANNOTATION_PROCESSOR_PATH;
 109 
 110 import com.sun.tools.javac.tree.JCTree.JCModuleDecl;
 111 import com.sun.tools.javac.tree.JCTree.JCRecordPattern;
 112 import com.sun.tools.javac.tree.JCTree.JCSwitch;
 113 import com.sun.tools.javac.tree.JCTree.JCSwitchExpression;
 114 
 115 /** This class could be the main entry point for GJC when GJC is used as a

 756                 tree = (tree == null) ? make.Ident(names.fromString(s))
 757                                       : make.Select(tree, names.fromString(s));
 758             }
 759             JCCompilationUnit toplevel =
 760                 make.TopLevel(List.nil());
 761             toplevel.modle = msym;
 762             toplevel.packge = msym.unnamedPackage;
 763             return attr.attribIdent(tree, toplevel);
 764         } finally {
 765             log.useSource(prev);
 766         }
 767     }
 768 
 769     /** Generate code and emit a class file for a given class
 770      *  @param env    The attribution environment of the outermost class
 771      *                containing this class.
 772      *  @param cdef   The class definition from which code is generated.
 773      */
 774     JavaFileObject genCode(Env<AttrContext> env, JCClassDecl cdef) throws IOException {
 775         try {
 776             if (Feature.REFLECT_METHODS.allowedInSource(source)) {
 777                 Optional<CodeReflectionTransformer> reflectMethods = reflectMethods();
 778                 if (reflectMethods.isPresent()) {
 779                     reflectMethods.get().genCode(context, cdef);
 780                 }
 781             }
 782 
 783             if (gen.genClass(env, cdef) && (errorCount() == 0))
 784                 return writer.writeClass(cdef.sym);
 785         } catch (ClassWriter.PoolOverflow ex) {
 786             log.error(cdef.pos(), Errors.LimitPool);
 787         } catch (ClassWriter.StringOverflow ex) {
 788             log.error(cdef.pos(),
 789                       Errors.LimitStringOverflow(ex.value.substring(0, 20)));
 790         } catch (CompletionFailure ex) {
 791             chk.completionError(cdef.pos(), ex);
 792         }
 793         return null;
 794     }
 795 
 796     /** Emit plain Java source for a class.
 797      *  @param env    The attribution environment of the outermost class
 798      *                containing this class.
 799      *  @param cdef   The class definition to be printed.
 800      */
 801     JavaFileObject printSource(Env<AttrContext> env, JCClassDecl cdef) throws IOException {
 802         JavaFileObject outFile

1045         Set<JavaFileObject> filesSoFar = new HashSet<>();
1046         for (JavaFileObject fileObject : fileObjects) {
1047             if (!filesSoFar.contains(fileObject)) {
1048                 filesSoFar.add(fileObject);
1049                 trees.append(parse(fileObject));
1050             }
1051         }
1052         return trees.toList();
1053     }
1054 
1055    /**
1056     * Returns true iff the compilation will continue after annotation processing
1057     * is done.
1058     */
1059     public boolean continueAfterProcessAnnotations() {
1060         return !shouldStop(CompileState.ATTR);
1061     }
1062 
1063     public List<JCCompilationUnit> initModules(List<JCCompilationUnit> roots) {
1064         modules.initModules(roots);
1065 
1066         if (roots.isEmpty()) {
1067             enterDone();
1068         }
1069         return roots;
1070     }
1071 
1072     /**
1073      * Enter the symbols found in a list of parse trees.
1074      * As a side-effect, this puts elements on the "todo" list.
1075      * Also stores a list of all top level classes in rootClasses.
1076      */
1077     public List<JCCompilationUnit> enterTrees(List<JCCompilationUnit> roots) {
1078         //enter symbols for all files
1079         if (!taskListener.isEmpty()) {
1080             for (JCCompilationUnit unit: roots) {
1081                 TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, unit);
1082                 taskListener.started(e);
1083             }
1084         }
1085 

1468      * Check for various things to warn about in an attributed parse tree.
1469      */
1470     protected void warn(Env<AttrContext> env, Queue<Env<AttrContext>> results) {
1471         if (compileStates.isDone(env, CompileState.WARN)) {
1472             results.add(env);
1473             return;
1474         }
1475 
1476         if (shouldStop(CompileState.WARN))
1477             return;
1478 
1479         if (verboseCompilePolicy)
1480             printNote("[warn " + env.enclClass.sym + "]");
1481         JavaFileObject prev = log.useSource(
1482                                             env.enclClass.sym.sourcefile != null ?
1483                                             env.enclClass.sym.sourcefile :
1484                                             env.toplevel.sourcefile);
1485         try {
1486             warningAnalyzer.analyzeTree(env);
1487             compileStates.put(env, CompileState.WARN);
1488             // Run ReflectMethods here to ensure that it always runs before TransTypes,
1489             // even when TransTypes recursively translates classes from other compilation units
1490             if (!shouldStop(CompileState.TRANSTYPES)
1491                     && env.tree.hasTag(JCTree.Tag.CLASSDEF)
1492                     && (implicitSourcePolicy != ImplicitSourcePolicy.NONE || inputFiles.contains(env.toplevel.sourcefile))
1493                     && (modules.multiModuleMode || env.toplevel.modle == modules.getDefaultModule())
1494                     && Feature.REFLECT_METHODS.allowedInSource(source)) {
1495                 Optional<CodeReflectionTransformer> reflectMethods = reflectMethods();
1496                 if (reflectMethods.isPresent()) {
1497                     env.tree = reflectMethods.get().translateTopLevelClass(context, env.tree, make.forToplevel(env.toplevel));
1498                 }
1499             }
1500             results.add(env);
1501         }
1502         finally {
1503             log.useSource(prev);
1504         }
1505     }
1506 
1507     private TaskEvent newAnalyzeTaskEvent(Env<AttrContext> env) {
1508         JCCompilationUnit toplevel = env.toplevel;
1509         ClassSymbol sym;
1510         if (env.enclClass.sym == syms.predefClass) {
1511             if (TreeInfo.isModuleInfo(toplevel)) {
1512                 sym = toplevel.modle.module_info;
1513             } else if (TreeInfo.isPackageInfo(toplevel)) {
1514                 sym = toplevel.packge.package_info;
1515             } else {
1516                 throw new IllegalStateException("unknown env.toplevel");
1517             }
1518         } else {
1519             sym = env.enclClass.sym;

1725                     return;
1726 
1727                 for (JCTree def : cdefs) {
1728                     LambdaToMethod.instance(context).translateTopLevelClass(env, def, localMake);
1729                 }
1730                 compileStates.put(env, CompileState.UNLAMBDA);
1731             }
1732 
1733             //generate code for each class
1734             for (List<JCTree> l = cdefs; l.nonEmpty(); l = l.tail) {
1735                 JCClassDecl cdef = (JCClassDecl)l.head;
1736                 results.add(new Pair<>(env, cdef));
1737             }
1738         }
1739         finally {
1740             log.useSource(prev);
1741         }
1742 
1743     }
1744 
1745     Optional<CodeReflectionTransformer> reflectMethods() {
1746         return CodeReflectionSupport.CODE_LAYER != null ?
1747                 ServiceLoader.load(CodeReflectionSupport.CODE_LAYER, CodeReflectionTransformer.class).findFirst() :
1748                 Optional.empty();
1749     }
1750 
1751     public static class CodeReflectionSupport {
1752         public static final ModuleLayer CODE_LAYER;
1753 
1754         static {
1755             if (ModuleLayer.boot().findModule("jdk.incubator.code").isPresent()) {
1756                 // we are in an exploded build, so just use the boot layer
1757                 CODE_LAYER = ModuleLayer.boot();
1758             } else if (java.lang.module.ModuleFinder.ofSystem().find("jdk.incubator.code").isPresent()) {
1759                 // The incubating jdk.incubator.code module is installed, but not in the boot layer
1760                 // The module needs to be resolved so the jdk.compiler module can find and use the service provider
1761                 // for CodeReflectionTransformer
1762                 ModuleLayer parent = ModuleLayer.boot();
1763                 // Create a new configuration from the boot layer configuration with the resolved jdk.incubator.code module
1764                 Configuration cf = parent.configuration()
1765                         .resolve(java.lang.module.ModuleFinder.of(), java.lang.module.ModuleFinder.ofSystem(), Set.of("jdk.incubator.code"));
1766                 ClassLoader scl = ClassLoader.getSystemClassLoader();
1767                 // Create the code module layer using the new configuration and the system class loader
1768                 CODE_LAYER = parent.defineModulesWithOneLoader(cf, scl);
1769                 Module codeReflectionModule = CODE_LAYER.findModule("jdk.incubator.code").get();
1770                 Module jdkCompilerModule = JavaCompiler.class.getModule();
1771                 // Add exports to all jdk.compiler packages so that the jdk.incubator.code module can use them
1772                 for (String packageName : jdkCompilerModule.getPackages()) {
1773                     jdkCompilerModule.addExports(packageName, codeReflectionModule);
1774                 }
1775                 // Add exports to all java.base packages so that the jdk.incubator.code module can use them
1776                 // We need to do so by calling a method in java.base reflectively
1777                 try {
1778                     Class<?> codeModuleLayerInit = Class.forName("jdk.internal.access.code.CodeModuleLayerInit");
1779                     Method initLayerMethod = codeModuleLayerInit.getDeclaredMethod("initCodeModuleLayer", ModuleLayer.class);
1780                     initLayerMethod.invoke(null, CODE_LAYER);
1781                 } catch (ReflectiveOperationException ex) {
1782                     throw new AssertionError(ex);
1783                 }
1784             } else {
1785                 // If we run in bootstrap mode, there might be no jdk.incubator.code
1786                 CODE_LAYER = null;
1787             }
1788         }
1789     }
1790 
1791     /** Generates the source or class file for a list of classes.
1792      * The decision to generate a source file or a class file is
1793      * based upon the compiler's options.
1794      * Generation stops if an error occurs while writing files.
1795      */
1796     public void generate(Queue<Pair<Env<AttrContext>, JCClassDecl>> queue) {
1797         generate(queue, null);
1798     }
1799 
1800     public void generate(Queue<Pair<Env<AttrContext>, JCClassDecl>> queue, Queue<JavaFileObject> results) {
1801         if (shouldStop(CompileState.GENERATE))
1802             return;
1803 
1804         for (Pair<Env<AttrContext>, JCClassDecl> x: queue) {
1805             Env<AttrContext> env = x.fst;
1806             JCClassDecl cdef = x.snd;
1807 
1808             if (verboseCompilePolicy) {
1809                 printNote("[generate " + (sourceOutput ? " source" : "code") + " " + cdef.sym + "]");
1810             }
< prev index next >