< prev index next >

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

Print this page

   1 /*
   2  * Copyright (c) 1999, 2023, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   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 static com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag.*;
 101 
 102 import static javax.tools.StandardLocation.CLASS_OUTPUT;
 103 import static javax.tools.StandardLocation.ANNOTATION_PROCESSOR_PATH;
 104 
 105 import com.sun.tools.javac.tree.JCTree.JCModuleDecl;
 106 import com.sun.tools.javac.tree.JCTree.JCRecordPattern;
 107 import com.sun.tools.javac.tree.JCTree.JCSwitch;
 108 import com.sun.tools.javac.tree.JCTree.JCSwitchExpression;
 109 

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


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

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









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

1589 
1590             make.at(Position.FIRSTPOS);
1591             TreeMaker localMake = make.forToplevel(env.toplevel);
1592 
1593             if (env.tree.hasTag(JCTree.Tag.PACKAGEDEF) || env.tree.hasTag(JCTree.Tag.MODULEDEF)) {
1594                 if (!(sourceOutput)) {
1595                     if (shouldStop(CompileState.LOWER))
1596                         return;
1597                     List<JCTree> def = lower.translateTopLevelClass(env, env.tree, localMake);
1598                     if (def.head != null) {
1599                         Assert.check(def.tail.isEmpty());
1600                         results.add(new Pair<>(env, (JCClassDecl)def.head));
1601                     }
1602                 }
1603                 return;
1604             }
1605 
1606             if (shouldStop(CompileState.TRANSTYPES))
1607                 return;
1608 







1609             env.tree = transTypes.translateTopLevelClass(env.tree, localMake);
1610             compileStates.put(env, CompileState.TRANSTYPES);
1611 
1612             if (shouldStop(CompileState.TRANSPATTERNS))
1613                 return;
1614 
1615             if (scanner.hasPatterns) {
1616                 env.tree = TransPatterns.instance(context).translateTopLevelClass(env, env.tree, localMake);

1617             }
1618 
1619             compileStates.put(env, CompileState.TRANSPATTERNS);
1620 
1621             if (shouldStop(CompileState.LOWER))
1622                 return;
1623 
1624             if (sourceOutput) {
1625                 //emit standard Java source file, only for compilation
1626                 //units enumerated explicitly on the command line
1627                 JCClassDecl cdef = (JCClassDecl)env.tree;
1628                 if (untranslated instanceof JCClassDecl classDecl &&
1629                     rootClasses.contains(classDecl)) {
1630                     results.add(new Pair<>(env, cdef));
1631                 }
1632                 return;
1633             }
1634 
1635             //translate out inner classes
1636             List<JCTree> cdefs = lower.translateTopLevelClass(env, env.tree, localMake);

1644                     return;
1645 
1646                 for (JCTree def : cdefs) {
1647                     LambdaToMethod.instance(context).translateTopLevelClass(env, def, localMake);
1648                 }
1649                 compileStates.put(env, CompileState.UNLAMBDA);
1650             }
1651 
1652             //generate code for each class
1653             for (List<JCTree> l = cdefs; l.nonEmpty(); l = l.tail) {
1654                 JCClassDecl cdef = (JCClassDecl)l.head;
1655                 results.add(new Pair<>(env, cdef));
1656             }
1657         }
1658         finally {
1659             log.useSource(prev);
1660         }
1661 
1662     }
1663 

































1664     /** Generates the source or class file for a list of classes.
1665      * The decision to generate a source file or a class file is
1666      * based upon the compiler's options.
1667      * Generation stops if an error occurs while writing files.
1668      */
1669     public void generate(Queue<Pair<Env<AttrContext>, JCClassDecl>> queue) {
1670         generate(queue, null);
1671     }
1672 
1673     public void generate(Queue<Pair<Env<AttrContext>, JCClassDecl>> queue, Queue<JavaFileObject> results) {
1674         if (shouldStop(CompileState.GENERATE))
1675             return;
1676 
1677         for (Pair<Env<AttrContext>, JCClassDecl> x: queue) {
1678             Env<AttrContext> env = x.fst;
1679             JCClassDecl cdef = x.snd;
1680 
1681             if (verboseCompilePolicy) {
1682                 printNote("[generate " + (sourceOutput ? " source" : "code") + " " + cdef.sym + "]");
1683             }

1799                 log.warning(Warnings.ProcUseImplicit);
1800             else
1801                 log.warning(Warnings.ProcUseProcOrImplicit);
1802         }
1803         chk.reportDeferredDiagnostics();
1804         preview.reportDeferredDiagnostics();
1805         if (log.compressedOutput) {
1806             log.mandatoryNote(null, Notes.CompressedDiags);
1807         }
1808     }
1809 
1810     public void enterDone() {
1811         enterDone = true;
1812         annotate.enterDone();
1813     }
1814 
1815     public boolean isEnterDone() {
1816         return enterDone;
1817     }
1818 




1819     private Name readModuleName(JavaFileObject fo) {
1820         return parseAndGetName(fo, t -> {
1821             JCModuleDecl md = t.getModuleDecl();
1822 
1823             return md != null ? TreeInfo.fullName(md.getName()) : null;
1824         });
1825     }
1826 
1827     private Name findPackageInFile(JavaFileObject fo) {
1828         return parseAndGetName(fo, t -> t.getPackage() != null ?
1829                                         TreeInfo.fullName(t.getPackage().getPackageName()) : null);
1830     }
1831 
1832     private Name parseAndGetName(JavaFileObject fo,
1833                                  Function<JCTree.JCCompilationUnit, Name> tree2Name) {
1834         DiagnosticHandler dh = new DiscardDiagnosticHandler(log);
1835         JavaFileObject prevSource = log.useSource(fo);
1836         try {
1837             JCTree.JCCompilationUnit t = parse(fo, fo.getCharContent(false), true);
1838             return tree2Name.apply(t);

   1 /*
   2  * Copyright (c) 1999, 2024, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   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 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;
 110 

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

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

1601 
1602             make.at(Position.FIRSTPOS);
1603             TreeMaker localMake = make.forToplevel(env.toplevel);
1604 
1605             if (env.tree.hasTag(JCTree.Tag.PACKAGEDEF) || env.tree.hasTag(JCTree.Tag.MODULEDEF)) {
1606                 if (!(sourceOutput)) {
1607                     if (shouldStop(CompileState.LOWER))
1608                         return;
1609                     List<JCTree> def = lower.translateTopLevelClass(env, env.tree, localMake);
1610                     if (def.head != null) {
1611                         Assert.check(def.tail.isEmpty());
1612                         results.add(new Pair<>(env, (JCClassDecl)def.head));
1613                     }
1614                 }
1615                 return;
1616             }
1617 
1618             if (shouldStop(CompileState.TRANSTYPES))
1619                 return;
1620 
1621             if (Feature.REFLECT_METHODS.allowedInSource(source)) {
1622                 Optional<CodeReflectionTransformer> reflectMethods = reflectMethods();
1623                 if (reflectMethods.isPresent()) {
1624                     env.tree = reflectMethods.get().translateTopLevelClass(context, env.tree, localMake);
1625                 }
1626             }
1627 
1628             env.tree = transTypes.translateTopLevelClass(env.tree, localMake);
1629             compileStates.put(env, CompileState.TRANSTYPES);
1630 
1631             if (shouldStop(CompileState.TRANSPATTERNS))
1632                 return;
1633 
1634             if (scanner.hasPatterns) {
1635                 env.tree = TransPatterns.instance(context)
1636                         .translateTopLevelClass(env, env.tree, localMake);
1637             }
1638 
1639             compileStates.put(env, CompileState.TRANSPATTERNS);
1640 
1641             if (shouldStop(CompileState.LOWER))
1642                 return;
1643 
1644             if (sourceOutput) {
1645                 //emit standard Java source file, only for compilation
1646                 //units enumerated explicitly on the command line
1647                 JCClassDecl cdef = (JCClassDecl)env.tree;
1648                 if (untranslated instanceof JCClassDecl classDecl &&
1649                     rootClasses.contains(classDecl)) {
1650                     results.add(new Pair<>(env, cdef));
1651                 }
1652                 return;
1653             }
1654 
1655             //translate out inner classes
1656             List<JCTree> cdefs = lower.translateTopLevelClass(env, env.tree, localMake);

1664                     return;
1665 
1666                 for (JCTree def : cdefs) {
1667                     LambdaToMethod.instance(context).translateTopLevelClass(env, def, localMake);
1668                 }
1669                 compileStates.put(env, CompileState.UNLAMBDA);
1670             }
1671 
1672             //generate code for each class
1673             for (List<JCTree> l = cdefs; l.nonEmpty(); l = l.tail) {
1674                 JCClassDecl cdef = (JCClassDecl)l.head;
1675                 results.add(new Pair<>(env, cdef));
1676             }
1677         }
1678         finally {
1679             log.useSource(prev);
1680         }
1681 
1682     }
1683 
1684     Optional<CodeReflectionTransformer> reflectMethods() {
1685         if (CodeReflectionSupport.CODE_LAYER != null) {
1686             return ServiceLoader.load(CodeReflectionSupport.CODE_LAYER, CodeReflectionTransformer.class)
1687                             .findFirst();
1688         } else {
1689             return Optional.empty();
1690         }
1691     }
1692 
1693     static class CodeReflectionSupport {
1694         static final ModuleLayer CODE_LAYER;
1695 
1696         static {
1697             if (java.lang.module.ModuleFinder.ofSystem().find("jdk.incubator.code").isPresent() &&
1698                     !ModuleLayer.boot().findModule("jdk.incubator.code").isPresent()) {
1699                 ModuleLayer parent = ModuleLayer.boot();
1700                 Configuration cf = parent.configuration()
1701                         .resolve(java.lang.module.ModuleFinder.of(), java.lang.module.ModuleFinder.ofSystem(), Set.of("jdk.incubator.code"));
1702                 ClassLoader scl = ClassLoader.getSystemClassLoader();
1703                 CODE_LAYER = parent.defineModulesWithOneLoader(cf, scl);
1704                 Module codeReflectionModule = CODE_LAYER.findModule("jdk.incubator.code").get();
1705                 Module jdkCompilerModule = JavaCompiler.class.getModule();
1706                 // We need to add exports all jdk.compiler packages so that the plugin can use them
1707                 for (String packageName : jdkCompilerModule.getPackages()) {
1708                     jdkCompilerModule.addExports(packageName, codeReflectionModule);
1709                 }
1710             } else {
1711                 // if we run javac in bootstrap mode, there might be no jdk.incubator.code
1712                 CODE_LAYER = null;
1713             }
1714         }
1715     }
1716 
1717     /** Generates the source or class file for a list of classes.
1718      * The decision to generate a source file or a class file is
1719      * based upon the compiler's options.
1720      * Generation stops if an error occurs while writing files.
1721      */
1722     public void generate(Queue<Pair<Env<AttrContext>, JCClassDecl>> queue) {
1723         generate(queue, null);
1724     }
1725 
1726     public void generate(Queue<Pair<Env<AttrContext>, JCClassDecl>> queue, Queue<JavaFileObject> results) {
1727         if (shouldStop(CompileState.GENERATE))
1728             return;
1729 
1730         for (Pair<Env<AttrContext>, JCClassDecl> x: queue) {
1731             Env<AttrContext> env = x.fst;
1732             JCClassDecl cdef = x.snd;
1733 
1734             if (verboseCompilePolicy) {
1735                 printNote("[generate " + (sourceOutput ? " source" : "code") + " " + cdef.sym + "]");
1736             }

1852                 log.warning(Warnings.ProcUseImplicit);
1853             else
1854                 log.warning(Warnings.ProcUseProcOrImplicit);
1855         }
1856         chk.reportDeferredDiagnostics();
1857         preview.reportDeferredDiagnostics();
1858         if (log.compressedOutput) {
1859             log.mandatoryNote(null, Notes.CompressedDiags);
1860         }
1861     }
1862 
1863     public void enterDone() {
1864         enterDone = true;
1865         annotate.enterDone();
1866     }
1867 
1868     public boolean isEnterDone() {
1869         return enterDone;
1870     }
1871 
1872     public boolean hasCodeReflectionModule() {
1873         return hasCodeReflectionModule;
1874     }
1875 
1876     private Name readModuleName(JavaFileObject fo) {
1877         return parseAndGetName(fo, t -> {
1878             JCModuleDecl md = t.getModuleDecl();
1879 
1880             return md != null ? TreeInfo.fullName(md.getName()) : null;
1881         });
1882     }
1883 
1884     private Name findPackageInFile(JavaFileObject fo) {
1885         return parseAndGetName(fo, t -> t.getPackage() != null ?
1886                                         TreeInfo.fullName(t.getPackage().getPackageName()) : null);
1887     }
1888 
1889     private Name parseAndGetName(JavaFileObject fo,
1890                                  Function<JCTree.JCCompilationUnit, Name> tree2Name) {
1891         DiagnosticHandler dh = new DiscardDiagnosticHandler(log);
1892         JavaFileObject prevSource = log.useSource(fo);
1893         try {
1894             JCTree.JCCompilationUnit t = parse(fo, fo.getCharContent(false), true);
1895             return tree2Name.apply(t);
< prev index next >