Download jdk_11/solution/solution.patch from lolbench26/LoLBench: direct link, hf CLI and curl.
- Browser
- Download file 36.6 kB
-
https://huggingface.co/datasets/lolbench26/LoLBench/resolve/main/jdk_11/solution/solution.patch
- Command line
-
hf download hf://datasets/lolbench26/LoLBench/jdk_11/solution/solution.patch
-
curl -L -o solution.patch https://huggingface.co/datasets/lolbench26/LoLBench/resolve/main/jdk_11/solution/solution.patch
36.6 kB
| diff --git a/src/java.base/share/classes/jdk/internal/javac/PreviewFeature.java b/src/java.base/share/classes/jdk/internal/javac/PreviewFeature.java | |
| --- a/src/java.base/share/classes/jdk/internal/javac/PreviewFeature.java | |
| +++ b/src/java.base/share/classes/jdk/internal/javac/PreviewFeature.java | |
| public enum Feature { | |
| CLASSFILE_API, | |
| @JEP(number=461, title="Stream Gatherers", status="Preview") | |
| STREAM_GATHERERS, | |
| + @JEP(number=476, title="Module Import Declarations", status="Preview") | |
| + MODULE_IMPORTS, | |
| LANGUAGE_MODEL, | |
| /** | |
| * A key for testing. | |
| diff --git a/src/jdk.compiler/share/classes/com/sun/source/tree/ImportTree.java b/src/jdk.compiler/share/classes/com/sun/source/tree/ImportTree.java | |
| --- a/src/jdk.compiler/share/classes/com/sun/source/tree/ImportTree.java | |
| +++ b/src/jdk.compiler/share/classes/com/sun/source/tree/ImportTree.java | |
| /* | |
| - * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved. | |
| + * Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved. | |
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
| * | |
| * This code is free software; you can redistribute it and/or modify it | |
| package com.sun.source.tree; | |
| +import jdk.internal.javac.PreviewFeature; | |
| + | |
| /** | |
| * A tree node for an import declaration. | |
| * | |
| public interface ImportTree extends Tree { | |
| * @return true if this is a static import | |
| */ | |
| boolean isStatic(); | |
| + /** | |
| + * {@return true if this is an module import declaration.} | |
| + * @since 23 | |
| + */ | |
| + @PreviewFeature(feature=PreviewFeature.Feature.MODULE_IMPORTS, reflective=true) | |
| + boolean isModule(); | |
| /** | |
| * Returns the qualified identifier for the declaration(s) | |
| diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Preview.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Preview.java | |
| --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Preview.java | |
| +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Preview.java | |
| /* | |
| - * Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved. | |
| + * Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved. | |
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
| * | |
| * This code is free software; you can redistribute it and/or modify it | |
| public boolean isPreview(Feature feature) { | |
| case IMPLICIT_CLASSES -> true; | |
| case SUPER_INIT -> true; | |
| case PRIMITIVE_PATTERNS -> true; | |
| + case MODULE_IMPORTS -> true; | |
| //Note: this is a backdoor which allows to optionally treat all features as 'preview' (for testing). | |
| //When real preview features will be added, this method can be implemented to return 'true' | |
| //for those selected features, and 'false' for all the others. | |
| diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java | |
| --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java | |
| +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java | |
| public enum Feature { | |
| UNNAMED_VARIABLES(JDK22, Fragments.FeatureUnnamedVariables, DiagKind.PLURAL), | |
| PRIMITIVE_PATTERNS(JDK23, Fragments.FeaturePrimitivePatterns, DiagKind.PLURAL), | |
| SUPER_INIT(JDK22, Fragments.FeatureSuperInit, DiagKind.NORMAL), | |
| + MODULE_IMPORTS(JDK23, Fragments.FeatureModuleImports, DiagKind.PLURAL), | |
| ; | |
| enum DiagKind { | |
| diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java | |
| --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java | |
| +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java | |
| public void checkFunctionalInterface(JCClassDecl tree, ClassSymbol cs) { | |
| } | |
| public void checkImportsResolvable(final JCCompilationUnit toplevel) { | |
| - for (final JCImport imp : toplevel.getImports()) { | |
| + for (final JCImportBase impBase : toplevel.getImports()) { | |
| + if (!(impBase instanceof JCImport imp)) | |
| + continue; | |
| if (!imp.staticImport || !imp.qualid.hasTag(SELECT)) | |
| continue; | |
| final JCFieldAccess select = imp.qualid; | |
| public void checkImportsResolvable(final JCCompilationUnit toplevel) { | |
| // Check that packages imported are in scope (JLS 7.4.3, 6.3, 6.5.3.1, 6.5.3.2) | |
| public void checkImportedPackagesObservable(final JCCompilationUnit toplevel) { | |
| - OUTER: for (JCImport imp : toplevel.getImports()) { | |
| - if (!imp.staticImport && TreeInfo.name(imp.qualid) == names.asterisk) { | |
| + OUTER: for (JCImportBase impBase : toplevel.getImports()) { | |
| + if (impBase instanceof JCImport imp && !imp.staticImport && | |
| + TreeInfo.name(imp.qualid) == names.asterisk) { | |
| TypeSymbol tsym = imp.qualid.selected.type.tsym; | |
| if (tsym.kind == PCK && tsym.members().isEmpty() && | |
| !(Feature.IMPORT_ON_DEMAND_OBSERVABLE_PACKAGES.allowedInSource(source) && tsym.exists())) { | |
| diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TreeDiffer.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TreeDiffer.java | |
| --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TreeDiffer.java | |
| +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TreeDiffer.java | |
| /* | |
| * Copyright (c) 2018, Google LLC. All rights reserved. | |
| - * Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved. | |
| + * Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved. | |
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
| * | |
| * This code is free software; you can redistribute it and/or modify it | |
| import com.sun.tools.javac.tree.JCTree.JCMethodInvocation; | |
| import com.sun.tools.javac.tree.JCTree.JCModifiers; | |
| import com.sun.tools.javac.tree.JCTree.JCModuleDecl; | |
| +import com.sun.tools.javac.tree.JCTree.JCModuleImport; | |
| import com.sun.tools.javac.tree.JCTree.JCNewArray; | |
| import com.sun.tools.javac.tree.JCTree.JCNewClass; | |
| import com.sun.tools.javac.tree.JCTree.JCOpens; | |
| public void visitImport(JCImport tree) { | |
| result = tree.staticImport == that.staticImport && scan(tree.qualid, that.qualid); | |
| } | |
| + @Override | |
| + public void visitModuleImport(JCModuleImport tree) { | |
| + JCModuleImport that = (JCModuleImport) parameter; | |
| + result = scan(tree.module, that.module); | |
| + } | |
| + | |
| @Override | |
| public void visitIndexed(JCArrayAccess tree) { | |
| JCArrayAccess that = (JCArrayAccess) parameter; | |
| diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java | |
| --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java | |
| +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java | |
| import javax.tools.JavaFileObject; | |
| import com.sun.tools.javac.code.*; | |
| +import com.sun.tools.javac.code.Directive.ExportsDirective; | |
| +import com.sun.tools.javac.code.Directive.RequiresDirective; | |
| import com.sun.tools.javac.code.Lint.LintCategory; | |
| import com.sun.tools.javac.code.Scope.ImportFilter; | |
| -import com.sun.tools.javac.code.Scope.ImportScope; | |
| import com.sun.tools.javac.code.Scope.NamedImportScope; | |
| import com.sun.tools.javac.code.Scope.StarImportScope; | |
| import com.sun.tools.javac.code.Scope.WriteableScope; | |
| import com.sun.tools.javac.code.Source.Feature; | |
| import com.sun.tools.javac.comp.Annotate.AnnotationTypeMetadata; | |
| -import com.sun.tools.javac.parser.Parser; | |
| -import com.sun.tools.javac.parser.ParserFactory; | |
| import com.sun.tools.javac.tree.*; | |
| import com.sun.tools.javac.util.*; | |
| import com.sun.tools.javac.util.DefinedBy.Api; | |
| import static com.sun.tools.javac.code.Kinds.Kind.*; | |
| import static com.sun.tools.javac.code.TypeTag.CLASS; | |
| import static com.sun.tools.javac.code.TypeTag.ERROR; | |
| -import com.sun.tools.javac.resources.CompilerProperties.Fragments; | |
| import static com.sun.tools.javac.code.TypeTag.*; | |
| import static com.sun.tools.javac.tree.JCTree.Tag.*; | |
| public class TypeEnter implements Completer { | |
| private final Lint lint; | |
| private final TypeEnvs typeEnvs; | |
| private final Dependencies dependencies; | |
| - private final ParserFactory parserFactory; | |
| - private final Preview preview; | |
| public static TypeEnter instance(Context context) { | |
| TypeEnter instance = context.get(typeEnterKey); | |
| protected TypeEnter(Context context) { | |
| lint = Lint.instance(context); | |
| typeEnvs = TypeEnvs.instance(context); | |
| dependencies = Dependencies.instance(context); | |
| - parserFactory = ParserFactory.instance(context); | |
| - preview = Preview.instance(context); | |
| Source source = Source.instance(context); | |
| allowDeprecationOnImport = Feature.DEPRECATION_ON_IMPORT.allowedInSource(source); | |
| } | |
| private void resolveImports(JCCompilationUnit tree, Env<AttrContext> env) { | |
| if (tree.getPackage() != null && decl == null) | |
| checkClassPackageClash(tree.getPackage()); | |
| - for (JCImport imp : tree.getImports()) { | |
| - doImport(imp); | |
| - } | |
| + handleImports(tree.getImports()); | |
| if (decl != null) { | |
| DiagnosticPosition prevCheckDeprecatedLintPos = deferredLintHandler.setPos(decl.pos()); | |
| private void resolveImports(JCCompilationUnit tree, Env<AttrContext> env) { | |
| } | |
| } | |
| + private void handleImports(List<JCImportBase> imports) { | |
| + for (JCImportBase imp : imports) { | |
| + if (imp instanceof JCModuleImport mimp) { | |
| + doModuleImport(mimp); | |
| + } else { | |
| + doImport((JCImport) imp); | |
| + } | |
| + } | |
| + } | |
| + | |
| private void checkClassPackageClash(JCPackageDecl tree) { | |
| // check that no class exists with same fully qualified name as | |
| // toplevel package | |
| private void doImport(JCImport tree) { | |
| } | |
| } | |
| + private void doModuleImport(JCModuleImport tree) { | |
| + Name moduleName = TreeInfo.fullName(tree.module); | |
| + ModuleSymbol module = syms.getModule(moduleName); | |
| + | |
| + if (module != null) { | |
| + if (!env.toplevel.modle.readModules.contains(module)) { | |
| + if (env.toplevel.modle.isUnnamed()) { | |
| + log.error(tree.pos, Errors.ImportModuleDoesNotReadUnnamed(module)); | |
| + } else { | |
| + log.error(tree.pos, Errors.ImportModuleDoesNotRead(env.toplevel.modle, | |
| + module)); | |
| + } | |
| + //error recovery, make sure the module is completed: | |
| + module.getDirectives(); | |
| + } | |
| + | |
| + List<ModuleSymbol> todo = List.of(module); | |
| + Set<ModuleSymbol> seenModules = new HashSet<>(); | |
| + | |
| + while (!todo.isEmpty()) { | |
| + ModuleSymbol currentModule = todo.head; | |
| + | |
| + todo = todo.tail; | |
| + | |
| + if (!seenModules.add(currentModule)) { | |
| + continue; | |
| + } | |
| + | |
| + for (ExportsDirective export : currentModule.exports) { | |
| + if (export.modules != null && !export.modules.contains(env.toplevel.packge.modle)) { | |
| + continue; | |
| + } | |
| + | |
| + PackageSymbol pkg = export.getPackage(); | |
| + JCImport nestedImport = make.at(tree.pos) | |
| + .Import(make.Select(make.QualIdent(pkg), names.asterisk), false); | |
| + | |
| + doImport(nestedImport); | |
| + } | |
| + | |
| + for (RequiresDirective requires : currentModule.requires) { | |
| + if (requires.isTransitive()) { | |
| + todo = todo.prepend(requires.module); | |
| + } | |
| + } | |
| + } | |
| + } else { | |
| + log.error(tree.pos, Errors.ImportModuleNotFound(moduleName)); | |
| + } | |
| + } | |
| + | |
| Type attribImportType(JCTree tree, Env<AttrContext> env) { | |
| Assert.check(completionEnabled); | |
| Lint prevLint = chk.setLint(allowDeprecationOnImport ? | |
| diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java | |
| --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java | |
| +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java | |
| protected JCTree importDeclaration() { | |
| if (token.kind == STATIC) { | |
| importStatic = true; | |
| nextToken(); | |
| + } else if (token.kind == IDENTIFIER && token.name() == names.module && | |
| + peekToken(TokenKind.IDENTIFIER)) { | |
| + checkSourceLevel(Feature.MODULE_IMPORTS); | |
| + nextToken(); | |
| + JCExpression moduleName = qualident(false); | |
| + accept(SEMI); | |
| + return toP(F.at(pos).ModuleImport(moduleName)); | |
| } | |
| JCExpression pid = toP(F.at(token.pos).Ident(ident())); | |
| do { | |
| diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties | |
| --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties | |
| +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties | |
| compiler.misc.feature.implicit.classes=\ | |
| compiler.misc.feature.super.init=\ | |
| statements before super() | |
| +compiler.misc.feature.module.imports=\ | |
| + module imports | |
| + | |
| compiler.warn.underscore.as.identifier=\ | |
| as of release 9, ''_'' is a keyword, and may not be used as an identifier | |
| compiler.err.module.not.found=\ | |
| compiler.warn.module.not.found=\ | |
| module not found: {0} | |
| +# 0: name | |
| +compiler.err.import.module.not.found=\ | |
| + imported module not found: {0} | |
| + | |
| +# 0: symbol | |
| +compiler.err.import.module.does.not.read.unnamed=\ | |
| + unnamed module does not read: {0} | |
| + | |
| +# 0: symbol, 1: symbol | |
| +compiler.err.import.module.does.not.read=\ | |
| + module {0} does not read: {1} | |
| + | |
| compiler.err.too.many.modules=\ | |
| too many module declarations found | |
| diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java | |
| --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java | |
| +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java | |
| public enum Tag { | |
| */ | |
| IMPORT, | |
| + /** Module import clauses. | |
| + */ | |
| + MODULEIMPORT, | |
| + | |
| /** Class definitions, of type ClassDef. | |
| */ | |
| CLASSDEF, | |
| public ExpressionTree getPackageName() { | |
| } | |
| @DefinedBy(Api.COMPILER_TREE) | |
| - public List<JCImport> getImports() { | |
| - ListBuffer<JCImport> imports = new ListBuffer<>(); | |
| + public List<JCImportBase> getImports() { | |
| + ListBuffer<JCImportBase> imports = new ListBuffer<>(); | |
| for (JCTree tree : defs) { | |
| - if (tree.hasTag(IMPORT)) | |
| - imports.append((JCImport)tree); | |
| + if (tree instanceof JCImportBase imp) | |
| + imports.append(imp); | |
| else if (!tree.hasTag(PACKAGEDEF) && !tree.hasTag(SKIP)) | |
| break; | |
| } | |
| public List<JCTree> getTypeDecls() { | |
| List<JCTree> typeDefs; | |
| for (typeDefs = defs; !typeDefs.isEmpty(); typeDefs = typeDefs.tail) { | |
| if (!typeDefs.head.hasTag(MODULEDEF) | |
| - && !typeDefs.head.hasTag(PACKAGEDEF) && !typeDefs.head.hasTag(IMPORT)) { | |
| + && !typeDefs.head.hasTag(PACKAGEDEF) | |
| + && !typeDefs.head.hasTag(IMPORT) | |
| + && !typeDefs.head.hasTag(MODULEIMPORT)) { | |
| break; | |
| } | |
| } | |
| public Tag getTag() { | |
| } | |
| } | |
| + public static abstract class JCImportBase extends JCTree implements ImportTree { | |
| + | |
| + @DefinedBy(Api.COMPILER_TREE) | |
| + public Kind getKind() { return Kind.IMPORT; } | |
| + @Override @DefinedBy(Api.COMPILER_TREE) | |
| + public <R,D> R accept(TreeVisitor<R,D> v, D d) { | |
| + return v.visitImport(this, d); | |
| + } | |
| + | |
| + public abstract JCTree getQualifiedIdentifier(); | |
| + } | |
| + | |
| /** | |
| * An import clause. | |
| */ | |
| - public static class JCImport extends JCTree implements ImportTree { | |
| + public static class JCImport extends JCImportBase { | |
| public boolean staticImport; | |
| /** The imported class(es). */ | |
| public JCFieldAccess qualid; | |
| protected JCImport(JCFieldAccess qualid, boolean importStatic) { | |
| @DefinedBy(Api.COMPILER_TREE) | |
| public boolean isStatic() { return staticImport; } | |
| @DefinedBy(Api.COMPILER_TREE) | |
| + public boolean isModule() { return false; } | |
| + @DefinedBy(Api.COMPILER_TREE) | |
| public JCFieldAccess getQualifiedIdentifier() { return qualid; } | |
| + @Override | |
| + public Tag getTag() { | |
| + return IMPORT; | |
| + } | |
| + } | |
| + | |
| + /** | |
| + * A module import clause. | |
| + */ | |
| + public static class JCModuleImport extends JCImportBase { | |
| + /** The module name. */ | |
| + public JCExpression module; | |
| + protected JCModuleImport(JCExpression module) { | |
| + this.module = module; | |
| + } | |
| + @Override | |
| + public void accept(Visitor v) { v.visitModuleImport(this); } | |
| + | |
| + @DefinedBy(Api.COMPILER_TREE) | |
| + public boolean isStatic() { return false; } | |
| + @DefinedBy(Api.COMPILER_TREE) | |
| + public boolean isModule() { return true; } | |
| + @DefinedBy(Api.COMPILER_TREE) | |
| + public JCExpression getQualifiedIdentifier() { return module; } | |
| + | |
| @DefinedBy(Api.COMPILER_TREE) | |
| public Kind getKind() { return Kind.IMPORT; } | |
| @Override @DefinedBy(Api.COMPILER_TREE) | |
| public <R,D> R accept(TreeVisitor<R,D> v, D d) { | |
| @Override | |
| public Tag getTag() { | |
| - return IMPORT; | |
| + return MODULEIMPORT; | |
| } | |
| } | |
| public abstract static class Visitor { | |
| public void visitTopLevel(JCCompilationUnit that) { visitTree(that); } | |
| public void visitPackageDef(JCPackageDecl that) { visitTree(that); } | |
| public void visitImport(JCImport that) { visitTree(that); } | |
| + public void visitModuleImport(JCModuleImport that) { visitTree(that); } | |
| public void visitClassDef(JCClassDecl that) { visitTree(that); } | |
| public void visitMethodDef(JCMethodDecl that) { visitTree(that); } | |
| public void visitVarDef(JCVariableDecl that) { visitTree(that); } | |
| diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/Pretty.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/Pretty.java | |
| --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/Pretty.java | |
| +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/Pretty.java | |
| public void printUnit(JCCompilationUnit tree, JCClassDecl cdef) throws IOExcepti | |
| (cdef == null || | |
| l.head.hasTag(IMPORT) || l.head.hasTag(PACKAGEDEF)); | |
| l = l.tail) { | |
| - if (l.head.hasTag(IMPORT)) { | |
| - JCImport imp = (JCImport)l.head; | |
| - Name name = TreeInfo.name(imp.qualid); | |
| + if (l.head instanceof JCImportBase imp) { | |
| + Name name = TreeInfo.name(imp.getQualifiedIdentifier()); | |
| if (name == name.table.names.asterisk || | |
| cdef == null || | |
| - isUsed(TreeInfo.symbol(imp.qualid), cdef)) { | |
| + imp instanceof JCModuleImport || | |
| + isUsed(TreeInfo.symbol(imp.getQualifiedIdentifier()), cdef)) { | |
| if (firstImport) { | |
| firstImport = false; | |
| println(); | |
| public void visitImport(JCImport tree) { | |
| } | |
| } | |
| + public void visitModuleImport(JCModuleImport tree) { | |
| + try { | |
| + print("import module "); | |
| + printExpr(tree.module); | |
| + print(';'); | |
| + println(); | |
| + } catch (IOException e) { | |
| + throw new UncheckedIOException(e); | |
| + } | |
| + } | |
| + | |
| public void visitClassDef(JCClassDecl tree) { | |
| try { | |
| println(); align(); | |
| diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeCopier.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeCopier.java | |
| --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeCopier.java | |
| +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeCopier.java | |
| public JCTree visitIf(IfTree node, P p) { | |
| @DefinedBy(Api.COMPILER_TREE) | |
| public JCTree visitImport(ImportTree node, P p) { | |
| - JCImport t = (JCImport) node; | |
| - JCFieldAccess qualid = copy(t.qualid, p); | |
| - return M.at(t.pos).Import(qualid, t.staticImport); | |
| + if (node instanceof JCModuleImport mimp) { | |
| + JCExpression module = copy(mimp.module, p); | |
| + return M.at(mimp.pos).ModuleImport(module); | |
| + } else { | |
| + JCImport t = (JCImport) node; | |
| + JCFieldAccess qualid = copy(t.qualid, p); | |
| + return M.at(t.pos).Import(qualid, t.staticImport); | |
| + } | |
| } | |
| @DefinedBy(Api.COMPILER_TREE) | |
| diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeMaker.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeMaker.java | |
| --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeMaker.java | |
| +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeMaker.java | |
| public JCCompilationUnit TopLevel(List<JCTree> defs) { | |
| Assert.check(node instanceof JCClassDecl | |
| || node instanceof JCPackageDecl | |
| || node instanceof JCImport | |
| + || node instanceof JCModuleImport | |
| || node instanceof JCModuleDecl | |
| || node instanceof JCSkip | |
| || node instanceof JCErroneous | |
| public JCPackageDecl PackageDecl(List<JCAnnotation> annotations, | |
| return tree; | |
| } | |
| - public JCImport Import(JCFieldAccess qualid, boolean importStatic) { | |
| - JCImport tree = new JCImport(qualid, importStatic); | |
| + public JCImport Import(JCFieldAccess qualid, boolean staticImport) { | |
| + JCImport tree = new JCImport(qualid, staticImport); | |
| + tree.pos = pos; | |
| + return tree; | |
| + } | |
| + | |
| + public JCModuleImport ModuleImport(JCExpression moduleName) { | |
| + JCModuleImport tree = new JCModuleImport(moduleName); | |
| tree.pos = pos; | |
| return tree; | |
| } | |
| diff --git a/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java b/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java | |
| --- a/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java | |
| +++ b/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java | |
| /* | |
| - * Copyright (c) 2014, 2022, Oracle and/or its affiliates. All rights reserved. | |
| + * Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved. | |
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
| * | |
| * This code is free software; you can redistribute it and/or modify it | |
| String[] shownOptions() { | |
| return selectOptions(e -> e.getKey().showOption); | |
| } | |
| + boolean hasOption(OptionKind kind) { | |
| + return optMap.containsKey(kind); | |
| + } | |
| + | |
| void addAll(OptionKind kind, Collection<String> vals) { | |
| optMap.computeIfAbsent(kind, k -> new ArrayList<>()) | |
| .addAll(vals); | |
| Options parse(OptionSet options) { | |
| .map(mp -> mp.contains("=") ? mp : mp + "=ALL-UNNAMED") | |
| .toList() | |
| ); | |
| - if (options.has(argEnablePreview)) { | |
| + if (previewEnabled(options)) { | |
| opts.addAll(OptionKind.ENABLE_PREVIEW, List.of( | |
| OptionKind.ENABLE_PREVIEW.optionFlag)); | |
| opts.addAll(OptionKind.SOURCE_RELEASE, List.of( | |
| Options parse(OptionSet options) { | |
| } | |
| } | |
| + boolean previewEnabled(OptionSet options) { | |
| + return options.has(argEnablePreview); | |
| + } | |
| + | |
| void addOptions(OptionKind kind, Collection<String> vals) { | |
| if (!vals.isEmpty()) { | |
| if (kind.onlyOne && vals.size() > 1) { | |
| Options parse(OptionSet options) { | |
| initialStartup = Startup.noStartup(); | |
| } else { | |
| String packedStartup = prefs.get(STARTUP_KEY); | |
| - initialStartup = Startup.unpack(packedStartup, new InitMessageHandler()); | |
| + boolean preview = previewEnabled(options); | |
| + initialStartup = Startup.unpack(packedStartup, preview, new InitMessageHandler()); | |
| } | |
| if (options.has(argExecution)) { | |
| executionControlSpec = options.valueOf(argExecution); | |
| boolean setStart(ArgTokenizer at) { | |
| return false; | |
| } | |
| } else if (defaultOption) { | |
| - startup = Startup.defaultStartup(this); | |
| + boolean preview = options.hasOption(OptionKind.ENABLE_PREVIEW); | |
| + startup = Startup.defaultStartup(preview, this); | |
| } else if (noneOption) { | |
| startup = Startup.noStartup(); | |
| } | |
| void showSetStart() { | |
| StringBuilder sb = new StringBuilder(); | |
| String retained = prefs.get(STARTUP_KEY); | |
| if (retained != null) { | |
| - Startup retainedStart = Startup.unpack(retained, this); | |
| + boolean preview = options.hasOption(OptionKind.ENABLE_PREVIEW); | |
| + Startup retainedStart = Startup.unpack(retained, preview, this); | |
| boolean currentDifferent = !startup.equals(retainedStart); | |
| sb.append(retainedStart.show(true)); | |
| if (currentDifferent) { | |
| diff --git a/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/Startup.java b/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/Startup.java | |
| --- a/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/Startup.java | |
| +++ b/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/Startup.java | |
| /* | |
| - * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. | |
| + * Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved. | |
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
| * | |
| * This code is free software; you can redistribute it and/or modify it | |
| public boolean equals(Object o) { | |
| } | |
| private static final String DEFAULT_STARTUP_NAME = "DEFAULT"; | |
| + private static final String PREVIEW_DEFAULT_STARTUP_NAME = "PREVIEW_DEFAULT"; | |
| // cached DEFAULT start-up | |
| - private static Startup defaultStartup = null; | |
| + private static Startup[] defaultStartup = new Startup[] { | |
| + null, //standard startup | |
| + null //preview startup | |
| + }; | |
| // the list of entries | |
| private List<StartupEntry> entries; | |
| boolean isEmpty() { | |
| boolean isDefault() { | |
| if (entries.size() == 1) { | |
| StartupEntry sue = entries.get(0); | |
| - if (sue.isBuiltIn && sue.name.equals(DEFAULT_STARTUP_NAME)) { | |
| + if (sue.isBuiltIn && (sue.name.equals(DEFAULT_STARTUP_NAME) || | |
| + sue.name.equals(PREVIEW_DEFAULT_STARTUP_NAME))) { | |
| return true; | |
| } | |
| } | |
| String showDetail() { | |
| * @param mh handler for error messages | |
| * @return Startup, or default startup when error (message has been printed) | |
| */ | |
| - static Startup unpack(String storedForm, MessageHandler mh) { | |
| + static Startup unpack(String storedForm, boolean preview, MessageHandler mh) { | |
| if (storedForm != null) { | |
| if (storedForm.isEmpty()) { | |
| return noStartup(); | |
| static Startup unpack(String storedForm, MessageHandler mh) { | |
| mh.errormsg("jshell.err.corrupted.stored.startup", ex.getMessage()); | |
| } | |
| } | |
| - return defaultStartup(mh); | |
| + return defaultStartup(preview, mh); | |
| } | |
| /** | |
| static Startup noStartup() { | |
| * @param mh handler for error messages | |
| * @return The default Startup, or empty startup when error (message has been printed) | |
| */ | |
| - static Startup defaultStartup(MessageHandler mh) { | |
| - if (defaultStartup != null) { | |
| - return defaultStartup; | |
| + static Startup defaultStartup(boolean preview, MessageHandler mh) { | |
| + int idx = preview ? 1 : 0; | |
| + | |
| + if (defaultStartup[idx] != null) { | |
| + return defaultStartup[idx]; | |
| } | |
| + String resourceName = preview ? PREVIEW_DEFAULT_STARTUP_NAME | |
| + : DEFAULT_STARTUP_NAME; | |
| try { | |
| - String content = readResource(DEFAULT_STARTUP_NAME); | |
| - return defaultStartup = new Startup( | |
| - new StartupEntry(true, DEFAULT_STARTUP_NAME, content)); | |
| + String content = readResource(resourceName); | |
| + return defaultStartup[idx] = new Startup( | |
| + new StartupEntry(true, resourceName, content)); | |
| } catch (AccessDeniedException e) { | |
| - mh.errormsg("jshell.err.file.not.accessible", "jshell", DEFAULT_STARTUP_NAME, e.getMessage()); | |
| + mh.errormsg("jshell.err.file.not.accessible", "jshell", resourceName, e.getMessage()); | |
| } catch (NoSuchFileException e) { | |
| - mh.errormsg("jshell.err.file.not.found", "jshell", DEFAULT_STARTUP_NAME); | |
| + mh.errormsg("jshell.err.file.not.found", "jshell", resourceName); | |
| } catch (Exception e) { | |
| - mh.errormsg("jshell.err.file.exception", "jshell", DEFAULT_STARTUP_NAME, e); | |
| + mh.errormsg("jshell.err.file.exception", "jshell", resourceName, e); | |
| } | |
| - return defaultStartup = noStartup(); | |
| + return defaultStartup[idx] = noStartup(); | |
| } | |
| } | |
| diff --git a/src/jdk.jshell/share/classes/jdk/jshell/Eval.java b/src/jdk.jshell/share/classes/jdk/jshell/Eval.java | |
| --- a/src/jdk.jshell/share/classes/jdk/jshell/Eval.java | |
| +++ b/src/jdk.jshell/share/classes/jdk/jshell/Eval.java | |
| /* | |
| - * Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved. | |
| + * Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved. | |
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
| * | |
| * This code is free software; you can redistribute it and/or modify it | |
| import static jdk.jshell.Util.DOIT_METHOD_NAME; | |
| import static jdk.jshell.Util.PREFIX_PATTERN; | |
| import static jdk.jshell.Util.expunge; | |
| +import static jdk.jshell.Snippet.SubKind.MODULE_IMPORT_SUBKIND; | |
| import static jdk.jshell.Snippet.SubKind.SINGLE_TYPE_IMPORT_SUBKIND; | |
| import static jdk.jshell.Snippet.SubKind.SINGLE_STATIC_IMPORT_SUBKIND; | |
| import static jdk.jshell.Snippet.SubKind.TYPE_IMPORT_ON_DEMAND_SUBKIND; | |
| */ | |
| class Eval { | |
| - private static final Pattern IMPORT_PATTERN = Pattern.compile("import\\p{javaWhitespace}+(?<static>static\\p{javaWhitespace}+)?(?<fullname>[\\p{L}\\p{N}_\\$\\.]+\\.(?<name>[\\p{L}\\p{N}_\\$]+|\\*))"); | |
| + private static final Pattern IMPORT_PATTERN = Pattern.compile("import\\p{javaWhitespace}+(?<module>module\\p{javaWhitespace}+)?(?<static>static\\p{javaWhitespace}+)?(?<fullname>[\\p{L}\\p{N}_\\$\\.]+\\.(?<name>[\\p{L}\\p{N}_\\$]+|\\*))"); | |
| private static final Pattern DEFAULT_PREFIX = Pattern.compile("\\p{javaWhitespace}*(default)\\p{javaWhitespace}+"); | |
| // for uses that should not change state -- non-evaluations | |
| private List<Snippet> processImport(String userSource, String compileSource) { | |
| Matcher mat = IMPORT_PATTERN.matcher(compileSource); | |
| String fullname; | |
| String name; | |
| + boolean isModule; | |
| boolean isStatic; | |
| if (mat.find()) { | |
| + isModule = mat.group("module") != null; | |
| isStatic = mat.group("static") != null; | |
| - name = mat.group("name"); | |
| fullname = mat.group("fullname"); | |
| + if (isModule) { | |
| + name = fullname; | |
| + } else { | |
| + name = mat.group("name"); | |
| + } | |
| } else { | |
| // bad import -- fake it | |
| + isModule = compileSource.contains(" module "); | |
| isStatic = compileSource.contains("static"); | |
| name = fullname = compileSource; | |
| } | |
| private List<Snippet> processImport(String userSource, String compileSource) { | |
| String keyName = isStar | |
| ? fullname | |
| : name; | |
| - SubKind snippetKind = isStar | |
| - ? (isStatic ? STATIC_IMPORT_ON_DEMAND_SUBKIND : TYPE_IMPORT_ON_DEMAND_SUBKIND) | |
| - : (isStatic ? SINGLE_STATIC_IMPORT_SUBKIND : SINGLE_TYPE_IMPORT_SUBKIND); | |
| + SubKind snippetKind; | |
| + if (isModule) { | |
| + snippetKind = MODULE_IMPORT_SUBKIND; | |
| + } else if (isStar) { | |
| + snippetKind = isStatic ? STATIC_IMPORT_ON_DEMAND_SUBKIND : TYPE_IMPORT_ON_DEMAND_SUBKIND; | |
| + } else { | |
| + snippetKind = isStatic ? SINGLE_STATIC_IMPORT_SUBKIND : SINGLE_TYPE_IMPORT_SUBKIND; | |
| + } | |
| Snippet snip = new ImportSnippet(state.keyMap.keyForImport(keyName, snippetKind), | |
| userSource, guts, fullname, name, snippetKind, fullkey, isStatic, isStar); | |
| return singletonList(snip); | |
| diff --git a/src/jdk.jshell/share/classes/jdk/jshell/Snippet.java b/src/jdk.jshell/share/classes/jdk/jshell/Snippet.java | |
| --- a/src/jdk.jshell/share/classes/jdk/jshell/Snippet.java | |
| +++ b/src/jdk.jshell/share/classes/jdk/jshell/Snippet.java | |
| /* | |
| - * Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved. | |
| + * Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved. | |
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
| * | |
| * This code is free software; you can redistribute it and/or modify it | |
| import java.util.Collection; | |
| import java.util.Collections; | |
| import java.util.List; | |
| +import jdk.internal.javac.PreviewFeature; | |
| /** | |
| * A Snippet represents a snippet of Java source code as passed to | |
| public enum SubKind { | |
| */ | |
| STATIC_IMPORT_ON_DEMAND_SUBKIND(Kind.IMPORT), | |
| + /** | |
| + * Import Module Declaration. | |
| + * An import declaration of a module. | |
| + * @jls 7.5.5 Import Module Declarations | |
| + * @since 23 | |
| + */ | |
| + @PreviewFeature(feature=PreviewFeature.Feature.MODULE_IMPORTS, reflective=true) | |
| + MODULE_IMPORT_SUBKIND(Kind.IMPORT), | |
| + | |
| /** | |
| * A class declaration. | |
| * A {@code SubKind} of {@link Kind#TYPE_DECL}. | |
| diff --git a/src/jdk.jshell/share/classes/jdk/jshell/tool/resources/PREVIEW_DEFAULT.jsh b/src/jdk.jshell/share/classes/jdk/jshell/tool/resources/PREVIEW_DEFAULT.jsh | |
| new file mode 100644 | |
| --- /dev/null | |
| +++ b/src/jdk.jshell/share/classes/jdk/jshell/tool/resources/PREVIEW_DEFAULT.jsh | |
| @@ -0,0 +1 @@ | |
| +import module java.base; | |