1 /*
2 * Copyright (c) 1999, 2025, 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
806
807 /** Compile a source file that has been accessed by the class finder.
808 * @param c The class the source file of which needs to be compiled.
809 */
810 private void readSourceFile(ClassSymbol c) throws CompletionFailure {
811 readSourceFile(null, c);
812 }
813
814 /** Compile a ClassSymbol from source, optionally using the given compilation unit as
815 * the source tree.
816 * @param tree the compilation unit in which the given ClassSymbol resides,
817 * or null if should be parsed from source
818 * @param c the ClassSymbol to complete
819 */
820 public void readSourceFile(JCCompilationUnit tree, ClassSymbol c) throws CompletionFailure {
821 if (completionFailureName == c.fullname) {
822 throw new CompletionFailure(
823 c, () -> diagFactory.fragment(Fragments.UserSelectedCompletionFailure), dcfh);
824 }
825 JavaFileObject filename = c.classfile;
826 JavaFileObject prev = log.useSource(filename);
827
828 if (tree == null) {
829 try {
830 tree = parse(filename, filename.getCharContent(false));
831 } catch (IOException e) {
832 log.error(Errors.ErrorReadingFile(filename, JavacFileManager.getMessage(e)));
833 tree = make.TopLevel(List.<JCTree>nil());
834 } finally {
835 log.useSource(prev);
836 }
837 }
838
839 if (!taskListener.isEmpty()) {
840 TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, tree);
841 taskListener.started(e);
842 }
843
844 // Process module declarations.
845 // If module resolution fails, ignore trees, and if trying to
846 // complete a specific symbol, throw CompletionFailure.
847 // Note that if module resolution failed, we may not even
848 // have enough modules available to access java.lang, and
1606 }
1607 @Override
1608 public void visitRecordPattern(JCRecordPattern that) {
1609 hasPatterns = true;
1610 super.visitRecordPattern(that);
1611 }
1612 @Override
1613 public void visitSwitch(JCSwitch tree) {
1614 hasPatterns |= tree.patternSwitch;
1615 super.visitSwitch(tree);
1616 }
1617 @Override
1618 public void visitSwitchExpression(JCSwitchExpression tree) {
1619 hasPatterns |= tree.patternSwitch;
1620 super.visitSwitchExpression(tree);
1621 }
1622 }
1623 ScanNested scanner = new ScanNested();
1624 scanner.scan(env.tree);
1625 for (Env<AttrContext> dep: scanner.dependencies) {
1626 if (!compileStates.isDone(dep, CompileState.WARN))
1627 desugaredEnvs.put(dep, desugar(warn(flow(attribute(dep)))));
1628 }
1629
1630 //We need to check for error another time as more classes might
1631 //have been attributed and analyzed at this stage
1632 if (shouldStop(CompileState.TRANSTYPES))
1633 return;
1634
1635 if (verboseCompilePolicy)
1636 printNote("[desugar " + env.enclClass.sym + "]");
1637
1638 JavaFileObject prev = log.useSource(env.enclClass.sym.sourcefile != null ?
1639 env.enclClass.sym.sourcefile :
1640 env.toplevel.sourcefile);
1641 try {
1642 //save tree prior to rewriting
1643 JCTree untranslated = env.tree;
1644
1645 make.at(Position.FIRSTPOS);
1646 TreeMaker localMake = make.forToplevel(env.toplevel);
1647
1687 return;
1688 }
1689
1690 //translate out inner classes
1691 List<JCTree> cdefs = lower.translateTopLevelClass(env, env.tree, localMake);
1692 compileStates.put(env, CompileState.LOWER);
1693
1694 if (shouldStop(CompileState.LOWER))
1695 return;
1696
1697 if (scanner.hasLambdas) {
1698 if (shouldStop(CompileState.UNLAMBDA))
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 }
|
1 /*
2 * Copyright (c) 1999, 2026, 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
806
807 /** Compile a source file that has been accessed by the class finder.
808 * @param c The class the source file of which needs to be compiled.
809 */
810 private void readSourceFile(ClassSymbol c) throws CompletionFailure {
811 readSourceFile(null, c);
812 }
813
814 /** Compile a ClassSymbol from source, optionally using the given compilation unit as
815 * the source tree.
816 * @param tree the compilation unit in which the given ClassSymbol resides,
817 * or null if should be parsed from source
818 * @param c the ClassSymbol to complete
819 */
820 public void readSourceFile(JCCompilationUnit tree, ClassSymbol c) throws CompletionFailure {
821 if (completionFailureName == c.fullname) {
822 throw new CompletionFailure(
823 c, () -> diagFactory.fragment(Fragments.UserSelectedCompletionFailure), dcfh);
824 }
825 JavaFileObject filename = c.classfile;
826
827 if (tree == null) {
828 JavaFileObject prev = log.useSource(filename);
829 try {
830 tree = parse(filename, filename.getCharContent(false));
831 } catch (IOException e) {
832 log.error(Errors.ErrorReadingFile(filename, JavacFileManager.getMessage(e)));
833 tree = make.TopLevel(List.<JCTree>nil());
834 } finally {
835 log.useSource(prev);
836 }
837 }
838
839 if (!taskListener.isEmpty()) {
840 TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, tree);
841 taskListener.started(e);
842 }
843
844 // Process module declarations.
845 // If module resolution fails, ignore trees, and if trying to
846 // complete a specific symbol, throw CompletionFailure.
847 // Note that if module resolution failed, we may not even
848 // have enough modules available to access java.lang, and
1606 }
1607 @Override
1608 public void visitRecordPattern(JCRecordPattern that) {
1609 hasPatterns = true;
1610 super.visitRecordPattern(that);
1611 }
1612 @Override
1613 public void visitSwitch(JCSwitch tree) {
1614 hasPatterns |= tree.patternSwitch;
1615 super.visitSwitch(tree);
1616 }
1617 @Override
1618 public void visitSwitchExpression(JCSwitchExpression tree) {
1619 hasPatterns |= tree.patternSwitch;
1620 super.visitSwitchExpression(tree);
1621 }
1622 }
1623 ScanNested scanner = new ScanNested();
1624 scanner.scan(env.tree);
1625 for (Env<AttrContext> dep: scanner.dependencies) {
1626 if (!compileStates.isDone(dep, CompileState.WARN))
1627 desugaredEnvs.put(dep, desugar(warn(flow(attribute(dep)))));
1628 }
1629
1630 //We need to check for error another time as more classes might
1631 //have been attributed and analyzed at this stage
1632 if (shouldStop(CompileState.TRANSTYPES))
1633 return;
1634
1635 if (verboseCompilePolicy)
1636 printNote("[desugar " + env.enclClass.sym + "]");
1637
1638 JavaFileObject prev = log.useSource(env.enclClass.sym.sourcefile != null ?
1639 env.enclClass.sym.sourcefile :
1640 env.toplevel.sourcefile);
1641 try {
1642 //save tree prior to rewriting
1643 JCTree untranslated = env.tree;
1644
1645 make.at(Position.FIRSTPOS);
1646 TreeMaker localMake = make.forToplevel(env.toplevel);
1647
1687 return;
1688 }
1689
1690 //translate out inner classes
1691 List<JCTree> cdefs = lower.translateTopLevelClass(env, env.tree, localMake);
1692 compileStates.put(env, CompileState.LOWER);
1693
1694 if (shouldStop(CompileState.LOWER))
1695 return;
1696
1697 if (scanner.hasLambdas) {
1698 if (shouldStop(CompileState.UNLAMBDA))
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 if (shouldStop(CompileState.STRICT_FIELDS_PROXIES))
1708 return;
1709 for (JCTree def : cdefs) {
1710 LocalProxyVarsGen.instance(context).translateTopLevelClass(def, localMake);
1711 }
1712 compileStates.put(env, CompileState.STRICT_FIELDS_PROXIES);
1713
1714 //generate code for each class
1715 for (List<JCTree> l = cdefs; l.nonEmpty(); l = l.tail) {
1716 JCClassDecl cdef = (JCClassDecl)l.head;
1717 results.add(new Pair<>(env, cdef));
1718 }
1719 }
1720 finally {
1721 log.useSource(prev);
1722 }
1723
1724 }
1725
1726 /** Generates the source or class file for a list of classes.
1727 * The decision to generate a source file or a class file is
1728 * based upon the compiler's options.
1729 * Generation stops if an error occurs while writing files.
1730 */
1731 public void generate(Queue<Pair<Env<AttrContext>, JCClassDecl>> queue) {
1732 generate(queue, null);
1733 }
|