jdk/src/java.base/share/classes/jdk/internal/module/ModuleBootstrap.java
author alanb
Thu, 17 Mar 2016 19:04:16 +0000
changeset 36511 9d0388c6b336
child 37779 7c84df693837
permissions -rw-r--r--
8142968: Module System implementation Summary: Initial integration of JEP 200, JEP 260, JEP 261, and JEP 282 Reviewed-by: alanb, mchung, naoto, rriggs, psandoz, plevart, mullan, ascarpino, vinnie, prr, sherman, dfuchs, mhaupt Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, chris.hegarty@oracle.com, alexandr.scherbatiy@oracle.com, amy.lu@oracle.com, calvin.cheung@oracle.com, daniel.fuchs@oracle.com, erik.joelsson@oracle.com, harold.seigel@oracle.com, jaroslav.bachorik@oracle.com, jean-francois.denise@oracle.com, jan.lahoda@oracle.com, james.laskey@oracle.com, lois.foltan@oracle.com, miroslav.kos@oracle.com, huaming.li@oracle.com, sean.mullan@oracle.com, naoto.sato@oracle.com, masayoshi.okutsu@oracle.com, peter.levart@gmail.com, philip.race@oracle.com, claes.redestad@oracle.com, sergey.bylokhov@oracle.com, alexandre.iline@oracle.com, volker.simonis@gmail.com, staffan.larsen@oracle.com, stuart.marks@oracle.com, semyon.sadetsky@oracle.com, serguei.spitsyn@oracle.com, sundararajan.athijegannathan@oracle.com, valerie.peng@oracle.com, vincent.x.ryan@oracle.com, weijun.wang@oracle.com, yuri.nesterenko@oracle.com, yekaterina.kantserova@oracle.com, alexander.kulyakhtin@oracle.com, felix.yang@oracle.com, andrei.eremeev@oracle.com, frank.yuan@oracle.com, sergei.pikalev@oracle.com, sibabrata.sahoo@oracle.com, tiantian.du@oracle.com, sha.jiang@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
package jdk.internal.module;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.io.File;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.lang.module.Configuration;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.lang.module.ModuleReference;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.lang.module.ModuleFinder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.lang.module.ResolvedModule;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.lang.reflect.Layer;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.lang.reflect.Module;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.nio.file.Paths;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.util.HashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.util.HashSet;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.util.Optional;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.util.Set;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.util.function.Function;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.util.stream.Collectors;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import jdk.internal.loader.BootLoader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import jdk.internal.loader.BuiltinClassLoader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import jdk.internal.perf.PerfCounter;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
 * Initializes/boots the module system.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
 * The {@link #boot() boot} method is called early in the startup to initialize
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
 * the module system. In summary, the boot method creates a Configuration by
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
 * resolving a set of module names specified via the launcher (or equivalent)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
 * -m and -addmods options. The modules are located on a module path that is
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
 * constructed from the upgrade, system and application module paths. The
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
 * Configuration is reified by creating the boot Layer with each module in the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
 * the configuration defined to one of the built-in class loaders. The mapping
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
 * of modules to class loaders is statically mapped in a helper class.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
public final class ModuleBootstrap {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
    private ModuleBootstrap() { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
    private static final String JAVA_BASE = "java.base";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
    // the token for "all unnamed modules"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
    private static final String ALL_UNNAMED = "ALL-UNNAMED";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
    // the token for "all system modules"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
    private static final String ALL_SYSTEM = "ALL-SYSTEM";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
    // the token for "all modules on the module path"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
    private static final String ALL_MODULE_PATH = "ALL-MODULE-PATH";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
    // ModuleFinder for the initial configuration
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
    private static ModuleFinder initialFinder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
     * Returns the ModuleFinder for the initial configuration
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
    public static ModuleFinder finder() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
        assert initialFinder != null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
        return initialFinder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
     * Initialize the module system, returning the boot Layer.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
     * @see java.lang.System#initPhase2()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
    public static Layer boot() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
        long t0 = System.nanoTime();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
        // system module path
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
        ModuleFinder systemModulePath = ModuleFinder.ofSystem();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
        // Once we have the system module path then we define the base module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
        // We do this here so that java.base is defined to the VM as early as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
        // possible and also that resources in the base module can be located
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
        // for error messages that may happen from here on.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
        Optional<ModuleReference> obase = systemModulePath.find(JAVA_BASE);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
        if (!obase.isPresent())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
            throw new InternalError(JAVA_BASE + " not found");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
        ModuleReference base = obase.get();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
        BootLoader.loadModule(base);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
        Modules.defineModule(null, base.descriptor(), base.location().orElse(null));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
        // -upgrademodulepath option specified to launcher
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
        ModuleFinder upgradeModulePath
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
            = createModulePathFinder("jdk.upgrade.module.path");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
        // -modulepath option specified to the launcher
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
        ModuleFinder appModulePath = createModulePathFinder("jdk.module.path");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
        // The module finder: [-upgrademodulepath] system-module-path [-modulepath]
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
        ModuleFinder finder = systemModulePath;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
        if (upgradeModulePath != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
            finder = ModuleFinder.compose(upgradeModulePath, finder);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
        if (appModulePath != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
            finder = ModuleFinder.compose(finder, appModulePath);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
        // launcher -m option to specify the initial module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
        String mainModule = System.getProperty("jdk.module.main");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
        // additional module(s) specified by -addmods
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
        boolean addAllSystemModules = false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
        boolean addAllApplicationModules = false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
        Set<String> addModules = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
        String propValue = System.getProperty("jdk.launcher.addmods");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
        if (propValue != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
            addModules = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
            for (String mod: propValue.split(",")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
                switch (mod) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
                    case ALL_SYSTEM:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
                        addAllSystemModules = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
                        break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
                    case ALL_MODULE_PATH:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
                        addAllApplicationModules = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
                        break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
                    default :
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
                        addModules.add(mod);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
        // The root modules to resolve
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
        Set<String> roots = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
        // main/initial module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
        if (mainModule != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
            roots.add(mainModule);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
            if (addAllApplicationModules)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
                fail(ALL_MODULE_PATH + " not allowed with initial module");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
        // If -addmods is specified then those modules need to be resolved
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
        if (addModules != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
            roots.addAll(addModules);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
        // -limitmods
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
        boolean limitmods = false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
        propValue = System.getProperty("jdk.launcher.limitmods");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
        if (propValue != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
            Set<String> mods = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
            for (String mod: propValue.split(",")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
                mods.add(mod);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
            finder = limitFinder(finder, mods, roots);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
            limitmods = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
        // If there is no initial module specified then assume that the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
        // initial module is the unnamed module of the application class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
        // loader. By convention, and for compatibility, this is
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
        // implemented by putting the names of all modules on the system
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
        // module path into the set of modules to resolve.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
        //
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
        // If `-addmods ALL-SYSTEM` is used then all modules on the system
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
        // module path will be resolved, irrespective of whether an initial
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
        // module is specified.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
        //
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
        // If `-addmods ALL-MODULE-PATH` is used, and no initial module is
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
        // specified, then all modules on the application module path will
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
        // be resolved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
        //
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
        if (mainModule == null || addAllSystemModules) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
            Set<ModuleReference> mrefs;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   194
            if (addAllApplicationModules) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
                assert mainModule == null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
                mrefs = finder.findAll();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   197
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
                mrefs = systemModulePath.findAll();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
                if (limitmods) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
                    ModuleFinder f = finder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
                    mrefs = mrefs.stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
                        .filter(m -> f.find(m.descriptor().name()).isPresent())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
                        .collect(Collectors.toSet());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
            // map to module names
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
            for (ModuleReference mref : mrefs) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
                roots.add(mref.descriptor().name());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
        long t1 = System.nanoTime();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   213
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   214
        // run the resolver to create the configuration
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   215
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   216
        Configuration cf = Configuration.empty()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
                .resolveRequiresAndUses(finder,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
                                        ModuleFinder.empty(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
                                        roots);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
        // time to create configuration
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
        PerfCounters.resolveTime.addElapsedTimeFrom(t1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
        // mapping of modules to class loaders
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
        Function<String, ClassLoader> clf = ModuleLoaderMap.mappingFunction(cf);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
        // check that all modules to be mapped to the boot loader will be
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
        // loaded from the system module path
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
        if (finder != systemModulePath) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
            for (ResolvedModule resolvedModule : cf.modules()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
                ModuleReference mref = resolvedModule.reference();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
                String name = mref.descriptor().name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
                ClassLoader cl = clf.apply(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
                if (cl == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
                    if (upgradeModulePath != null
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
                            && upgradeModulePath.find(name).isPresent())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
                        fail(name + ": cannot be loaded from upgrade module path");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
                    if (!systemModulePath.find(name).isPresent())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
                        fail(name + ": cannot be loaded from application module path");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
        long t2 = System.nanoTime();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
        // define modules to VM/runtime
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
        Layer bootLayer = Layer.empty().defineModules(cf, clf);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
        PerfCounters.layerCreateTime.addElapsedTimeFrom(t2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
        long t3 = System.nanoTime();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   254
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   255
        // define the module to its class loader, except java.base
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   256
        for (ResolvedModule resolvedModule : cf.modules()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   257
            ModuleReference mref = resolvedModule.reference();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   258
            String name = mref.descriptor().name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   259
            ClassLoader cl = clf.apply(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   260
            if (cl == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   261
                if (!name.equals(JAVA_BASE)) BootLoader.loadModule(mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   262
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
                ((BuiltinClassLoader)cl).loadModule(mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   264
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   265
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
        PerfCounters.loadModulesTime.addElapsedTimeFrom(t3);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
        // -XaddReads and -XaddExports
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   270
        addExtraReads(bootLayer);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   271
        addExtraExports(bootLayer);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   272
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   273
        // total time to initialize
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   274
        PerfCounters.bootstrapTime.addElapsedTimeFrom(t0);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   275
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   276
        // remember the ModuleFinder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   277
        initialFinder = finder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   278
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   279
        return bootLayer;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   280
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   281
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   282
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   283
     * Returns a ModuleFinder that limits observability to the given root
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   284
     * modules, their transitive dependences, plus a set of other modules.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   285
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   286
    private static ModuleFinder limitFinder(ModuleFinder finder,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   287
                                            Set<String> roots,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   288
                                            Set<String> otherMods)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   289
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   290
        // resolve all root modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   291
        Configuration cf = Configuration.empty()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   292
                .resolveRequires(finder,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   293
                                 ModuleFinder.empty(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   294
                                 roots);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   295
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   296
        // module name -> reference
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   297
        Map<String, ModuleReference> map = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   298
        cf.modules().stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   299
            .map(ResolvedModule::reference)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   300
            .forEach(mref -> map.put(mref.descriptor().name(), mref));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   301
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   302
        // set of modules that are observable
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   303
        Set<ModuleReference> mrefs = new HashSet<>(map.values());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   304
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   305
        // add the other modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   306
        for (String mod : otherMods) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   307
            Optional<ModuleReference> omref = finder.find(mod);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   308
            if (omref.isPresent()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   309
                ModuleReference mref = omref.get();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   310
                map.putIfAbsent(mod, mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   311
                mrefs.add(mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   312
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   313
                // no need to fail
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   314
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   315
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   316
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   317
        return new ModuleFinder() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   318
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   319
            public Optional<ModuleReference> find(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   320
                return Optional.ofNullable(map.get(name));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   321
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   322
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   323
            public Set<ModuleReference> findAll() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   324
                return mrefs;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   325
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   326
        };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   327
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   328
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   329
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   330
     * Creates a finder from the module path that is the value of the given
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   331
     * system property.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   332
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   333
    private static ModuleFinder createModulePathFinder(String prop) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   334
        String s = System.getProperty(prop);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   335
        if (s == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   336
            return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   337
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   338
            String[] dirs = s.split(File.pathSeparator);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   339
            Path[] paths = new Path[dirs.length];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   340
            int i = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   341
            for (String dir: dirs) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   342
                paths[i++] = Paths.get(dir);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   343
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   344
            return ModuleFinder.of(paths);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   345
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   346
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   347
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   348
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   349
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   350
     * Process the -XaddReads options to add any additional read edges that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   351
     * are specified on the command-line.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   352
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   353
    private static void addExtraReads(Layer bootLayer) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   354
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   355
        // decode the command line options
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   356
        Map<String, Set<String>> map = decode("jdk.launcher.addreads.");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   357
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   358
        for (Map.Entry<String, Set<String>> e : map.entrySet()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   359
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   360
            // the key is $MODULE
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   361
            String mn = e.getKey();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   362
            Optional<Module> om = bootLayer.findModule(mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   363
            if (!om.isPresent())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   364
                fail("Unknown module: " + mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   365
            Module m = om.get();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   366
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   367
            // the value is the set of other modules (by name)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   368
            for (String name : e.getValue()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   369
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   370
                Module other;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   371
                if (ALL_UNNAMED.equals(name)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   372
                    other = null;  // loose
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   373
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   374
                    om = bootLayer.findModule(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   375
                    if (!om.isPresent())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   376
                        fail("Unknown module: " + name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   377
                    other = om.get();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   378
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   379
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   380
                Modules.addReads(m, other);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   381
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   382
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   383
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   384
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   385
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   386
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   387
     * Process the -XaddExports options to add any additional read edges that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   388
     * are specified on the command-line.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   389
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   390
    private static void addExtraExports(Layer bootLayer) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   391
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   392
        // decode the command line options
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   393
        Map<String, Set<String>> map = decode("jdk.launcher.addexports.");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   394
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   395
        for (Map.Entry<String, Set<String>> e : map.entrySet()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   396
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   397
            // the key is $MODULE/$PACKAGE
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   398
            String key = e.getKey();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   399
            String[] s = key.split("/");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   400
            if (s.length != 2)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   401
                fail("Unable to parse: " + key);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   402
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   403
            String mn = s[0];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   404
            String pn = s[1];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   405
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   406
            // The exporting module is in the boot layer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   407
            Module m;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   408
            Optional<Module> om = bootLayer.findModule(mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   409
            if (!om.isPresent())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   410
                fail("Unknown module: " + mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   411
            m = om.get();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   412
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   413
            // the value is the set of modules to export to (by name)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   414
            for (String name : e.getValue()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   415
                boolean allUnnamed = false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   416
                Module other = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   417
                if (ALL_UNNAMED.equals(name)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   418
                    allUnnamed = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   419
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   420
                    om = bootLayer.findModule(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   421
                    if (om.isPresent()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   422
                        other = om.get();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   423
                    } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   424
                        fail("Unknown module: " + name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   425
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   426
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   427
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   428
                if (allUnnamed) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   429
                    Modules.addExportsToAllUnnamed(m, pn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   430
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   431
                    Modules.addExports(m, pn, other);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   432
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   433
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   434
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   435
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   436
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   437
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   438
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   439
     * Decodes the values of -XaddReads or -XaddExports options
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   440
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   441
     * The format of the options is: $KEY=$MODULE(,$MODULE)*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   442
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   443
     * For transition purposes, this method allows the first usage to be
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   444
     *     $KEY=$MODULE(,$KEY=$MODULE)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   445
     * This format will eventually be removed.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   446
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   447
    private static Map<String, Set<String>> decode(String prefix) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   448
        int index = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   449
        String value = System.getProperty(prefix + index);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   450
        if (value == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   451
            return Collections.emptyMap();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   452
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   453
        Map<String, Set<String>> map = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   454
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   455
        while (value != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   456
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   457
            int pos = value.indexOf('=');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   458
            if (pos == -1)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   459
                fail("Unable to parse: " + value);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   460
            if (pos == 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   461
                fail("Missing module name in: " + value);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   462
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   463
            // key is <module> or <module>/<package>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   464
            String key = value.substring(0, pos);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   465
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   466
            String rhs = value.substring(pos+1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   467
            if (rhs.isEmpty())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   468
                fail("Unable to parse: " + value);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   469
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   470
            // new format $MODULE(,$MODULE)* or old format $(MODULE)=...
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   471
            pos = rhs.indexOf('=');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   472
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   473
            // old format only allowed in first -X option
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   474
            if (pos >= 0 && index > 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   475
                fail("Unable to parse: " + value);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   476
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   477
            if (pos == -1) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   478
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   479
                // new format: $KEY=$MODULE(,$MODULE)*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   480
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   481
                Set<String> values = map.get(key);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   482
                if (values != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   483
                    fail(key + " specified more than once");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   484
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   485
                values = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   486
                map.put(key, values);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   487
                for (String s : rhs.split(",")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   488
                    if (s.length() > 0) values.add(s);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   489
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   490
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   491
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   492
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   493
                // old format: $KEY=$MODULE(,$KEY=$MODULE)*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   494
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   495
                assert index == 0;  // old format only allowed in first usage
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   496
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   497
                for (String expr : value.split(",")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   498
                    if (expr.length() > 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   499
                        String[] s = expr.split("=");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   500
                        if (s.length != 2)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   501
                            fail("Unable to parse: " + expr);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   502
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   503
                        map.computeIfAbsent(s[0], k -> new HashSet<>()).add(s[1]);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   504
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   505
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   506
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   507
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   508
            index++;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   509
            value = System.getProperty(prefix + index);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   510
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   512
        return map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   513
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   514
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   515
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   516
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   517
     * Throws a RuntimeException with the given message
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   518
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   519
    static void fail(String m) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   520
        throw new RuntimeException(m);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   521
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   522
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   523
    static class PerfCounters {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   524
        static PerfCounter resolveTime
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   525
            = PerfCounter.newPerfCounter("jdk.module.bootstrap.resolveTime");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   526
        static PerfCounter layerCreateTime
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   527
            = PerfCounter.newPerfCounter("jdk.module.bootstrap.layerCreateTime");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   528
        static PerfCounter loadModulesTime
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   529
            = PerfCounter.newPerfCounter("jdk.module.bootstrap.loadModulesTime");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   530
        static PerfCounter bootstrapTime
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   531
            = PerfCounter.newPerfCounter("jdk.module.bootstrap.totalTime");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   532
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   533
}