jdk/src/java.base/share/classes/jdk/internal/module/ModuleBootstrap.java
author alanb
Sat, 21 May 2016 08:01:03 +0100
changeset 38457 3d019217e322
parent 37779 7c84df693837
child 40261 86a49ba76f52
permissions -rw-r--r--
8152650: ModuleFinder.compose should accept varargs Reviewed-by: mchung
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;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    29
import java.io.PrintStream;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.lang.module.Configuration;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    31
import java.lang.module.ModuleDescriptor;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    32
import java.lang.module.ModuleFinder;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.lang.module.ModuleReference;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.lang.module.ResolvedModule;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.lang.reflect.Layer;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.lang.reflect.Module;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    37
import java.net.URI;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.nio.file.Paths;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.util.HashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.util.HashSet;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.util.Optional;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import java.util.Set;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import java.util.function.Function;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import jdk.internal.loader.BootLoader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import jdk.internal.loader.BuiltinClassLoader;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    50
import jdk.internal.misc.SharedSecrets;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
import jdk.internal.perf.PerfCounter;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
 * Initializes/boots the module system.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
 * The {@link #boot() boot} method is called early in the startup to initialize
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
 * the module system. In summary, the boot method creates a Configuration by
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
 * resolving a set of module names specified via the launcher (or equivalent)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
 * -m and -addmods options. The modules are located on a module path that is
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    60
 * constructed from the upgrade module path, system modules, and application
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    61
 * module path. The Configuration is instantiated as the boot Layer with each
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    62
 * module in the the configuration defined to one of the built-in class loaders.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
public final class ModuleBootstrap {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
    private ModuleBootstrap() { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
    private static final String JAVA_BASE = "java.base";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    70
    private static final String JAVA_SE = "java.se";
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    71
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    72
    // the token for "all default modules"
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    73
    private static final String ALL_DEFAULT = "ALL-DEFAULT";
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    74
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
    // the token for "all unnamed modules"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
    private static final String ALL_UNNAMED = "ALL-UNNAMED";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
    // the token for "all system modules"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
    private static final String ALL_SYSTEM = "ALL-SYSTEM";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
    // the token for "all modules on the module path"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
    private static final String ALL_MODULE_PATH = "ALL-MODULE-PATH";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
    // ModuleFinder for the initial configuration
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
    private static ModuleFinder 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
     * Returns the ModuleFinder for the initial configuration
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
    public static ModuleFinder finder() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
        assert initialFinder != null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
        return initialFinder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
     * Initialize the module system, returning the boot Layer.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
     * @see java.lang.System#initPhase2()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
    public static Layer boot() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
        long t0 = System.nanoTime();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   104
        // system modules
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   105
        ModuleFinder systemModules = ModuleFinder.ofSystem();
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   106
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   107
        PerfCounters.systemModulesTime.addElapsedTimeFrom(t0);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   109
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   110
        long t1 = System.nanoTime();
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   111
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   112
        // Once we have the system modules then we define the base module to
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   113
        // the VM. We do this here so that java.base is defined as early as
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
        // possible and also that resources in the base module can be located
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
        // for error messages that may happen from here on.
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   116
        ModuleReference base = systemModules.find(JAVA_BASE).orElse(null);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   117
        if (base == null)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
            throw new InternalError(JAVA_BASE + " not found");
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   119
        URI baseUri = base.location().orElse(null);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   120
        if (baseUri == null)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   121
            throw new InternalError(JAVA_BASE + " does not have a location");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
        BootLoader.loadModule(base);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   123
        Modules.defineModule(null, base.descriptor(), baseUri);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   125
        PerfCounters.defineBaseTime.addElapsedTimeFrom(t1);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   126
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   127
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   128
        long t2 = System.nanoTime();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
        // -upgrademodulepath option specified to launcher
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
        ModuleFinder upgradeModulePath
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
            = createModulePathFinder("jdk.upgrade.module.path");
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   133
        if (upgradeModulePath != null)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   134
            systemModules = ModuleFinder.compose(upgradeModulePath, systemModules);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
        // -modulepath option specified to the launcher
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
        ModuleFinder appModulePath = createModulePathFinder("jdk.module.path");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   139
        // The module finder: [-upgrademodulepath] system [-modulepath]
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   140
        ModuleFinder finder = systemModules;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
        if (appModulePath != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
            finder = ModuleFinder.compose(finder, appModulePath);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   144
        // The root modules to resolve
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   145
        Set<String> roots = new HashSet<>();
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   146
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   147
        // launcher -m option to specify the main/initial module
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
        String mainModule = System.getProperty("jdk.module.main");
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   149
        if (mainModule != null)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   150
            roots.add(mainModule);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
        // additional module(s) specified by -addmods
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   153
        boolean addAllDefaultModules = false;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
        boolean addAllSystemModules = false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
        boolean addAllApplicationModules = false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
        String propValue = System.getProperty("jdk.launcher.addmods");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
        if (propValue != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
            for (String mod: propValue.split(",")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
                switch (mod) {
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   160
                    case ALL_DEFAULT:
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   161
                        addAllDefaultModules = true;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   162
                        break;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
                    case ALL_SYSTEM:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
                        addAllSystemModules = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
                        break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
                    case ALL_MODULE_PATH:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
                        addAllApplicationModules = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
                        break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
                    default :
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   170
                        roots.add(mod);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
        // -limitmods
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
        propValue = System.getProperty("jdk.launcher.limitmods");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
        if (propValue != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
            Set<String> mods = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
            for (String mod: propValue.split(",")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
                mods.add(mod);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
            finder = limitFinder(finder, mods, roots);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   185
        // If there is no initial module specified then assume that the initial
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   186
        // module is the unnamed module of the application class loader. This
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   187
        // is implemented by resolving "java.se" and all (non-java.*) modules
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   188
        // that export an API. If "java.se" is not observable then all java.*
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   189
        // modules are resolved.
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   190
        if (mainModule == null || addAllDefaultModules) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   191
            boolean hasJava = false;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   192
            if (systemModules.find(JAVA_SE).isPresent()) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   193
                // java.se is a system module
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   194
                if (finder == systemModules || finder.find(JAVA_SE).isPresent()) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   195
                    // java.se is observable
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   196
                    hasJava = true;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   197
                    roots.add(JAVA_SE);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
            }
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   200
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   201
            for (ModuleReference mref : systemModules.findAll()) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   202
                String mn = mref.descriptor().name();
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   203
                if (hasJava && mn.startsWith("java."))
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   204
                    continue;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   205
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   206
                // add as root if observable and exports at least one package
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   207
                if ((finder == systemModules || finder.find(mn).isPresent())) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   208
                    ModuleDescriptor descriptor = mref.descriptor();
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   209
                    for (ModuleDescriptor.Exports e : descriptor.exports()) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   210
                        if (!e.isQualified()) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   211
                            roots.add(mn);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   212
                            break;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   213
                        }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   214
                    }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   215
                }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   216
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   219
        // If `-addmods ALL-SYSTEM` is specified then all observable system
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   220
        // modules will be resolved.
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   221
        if (addAllSystemModules) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   222
            ModuleFinder f = finder;  // observable modules
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   223
            systemModules.findAll()
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   224
                .stream()
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   225
                .map(ModuleReference::descriptor)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   226
                .map(ModuleDescriptor::name)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   227
                .filter(mn -> f.find(mn).isPresent())  // observable
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   228
                .forEach(mn -> roots.add(mn));
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   229
        }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   230
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   231
        // If `-addmods ALL-MODULE-PATH` is specified then all observable
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   232
        // modules on the application module path will be resolved.
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   233
        if  (appModulePath != null && addAllApplicationModules) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   234
            ModuleFinder f = finder;  // observable modules
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   235
            appModulePath.findAll()
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   236
                .stream()
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   237
                .map(ModuleReference::descriptor)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   238
                .map(ModuleDescriptor::name)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   239
                .filter(mn -> f.find(mn).isPresent())  // observable
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   240
                .forEach(mn -> roots.add(mn));
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   241
        }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   242
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   243
        PerfCounters.optionsAndRootsTime.addElapsedTimeFrom(t2);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   244
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   245
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   246
        long t3 = System.nanoTime();
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   247
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   248
        // determine if post resolution checks are needed
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   249
        boolean needPostResolutionChecks = true;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   250
        if (baseUri.getScheme().equals("jrt")   // toLowerCase not needed here
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   251
                && (upgradeModulePath == null)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   252
                && (appModulePath == null)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   253
                && (System.getProperty("jdk.launcher.patch.0") == null)) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   254
            needPostResolutionChecks = false;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   255
        }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   256
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   257
        PrintStream traceOutput = null;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   258
        if (Boolean.getBoolean("jdk.launcher.traceResolver"))
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   259
            traceOutput = System.out;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   260
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   261
        // run the resolver to create the configuration
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   262
        Configuration cf = SharedSecrets.getJavaLangModuleAccess()
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
                .resolveRequiresAndUses(finder,
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   264
                                        roots,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   265
                                        needPostResolutionChecks,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   266
                                        traceOutput);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
        // time to create configuration
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   269
        PerfCounters.resolveTime.addElapsedTimeFrom(t3);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   270
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   271
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   272
        // mapping of modules to class loaders
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   273
        Function<String, ClassLoader> clf = ModuleLoaderMap.mappingFunction(cf);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   274
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   275
        // check that all modules to be mapped to the boot loader will be
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   276
        // loaded from the runtime image
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   277
        if (needPostResolutionChecks) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   278
            for (ResolvedModule resolvedModule : cf.modules()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   279
                ModuleReference mref = resolvedModule.reference();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   280
                String name = mref.descriptor().name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   281
                ClassLoader cl = clf.apply(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   282
                if (cl == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   283
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   284
                    if (upgradeModulePath != null
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   285
                            && upgradeModulePath.find(name).isPresent())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   286
                        fail(name + ": cannot be loaded from upgrade module path");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   287
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   288
                    if (!systemModules.find(name).isPresent())
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   289
                        fail(name + ": cannot be loaded from application module path");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   290
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   291
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   292
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   293
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   294
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   295
        long t4 = System.nanoTime();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   296
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   297
        // define modules to VM/runtime
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   298
        Layer bootLayer = Layer.empty().defineModules(cf, clf);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   299
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   300
        PerfCounters.layerCreateTime.addElapsedTimeFrom(t4);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   301
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   302
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   303
        long t5 = System.nanoTime();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   304
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   305
        // define the module to its class loader, except java.base
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   306
        for (ResolvedModule resolvedModule : cf.modules()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   307
            ModuleReference mref = resolvedModule.reference();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   308
            String name = mref.descriptor().name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   309
            ClassLoader cl = clf.apply(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   310
            if (cl == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   311
                if (!name.equals(JAVA_BASE)) BootLoader.loadModule(mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   312
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   313
                ((BuiltinClassLoader)cl).loadModule(mref);
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
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   317
        PerfCounters.loadModulesTime.addElapsedTimeFrom(t5);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   318
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   319
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   320
        // -XaddReads and -XaddExports
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   321
        addExtraReads(bootLayer);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   322
        addExtraExports(bootLayer);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   323
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   324
        // total time to initialize
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   325
        PerfCounters.bootstrapTime.addElapsedTimeFrom(t0);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   326
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   327
        // remember the ModuleFinder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   328
        initialFinder = finder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   329
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   330
        return bootLayer;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   331
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   332
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   333
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   334
     * Returns a ModuleFinder that limits observability to the given root
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   335
     * modules, their transitive dependences, plus a set of other modules.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   336
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   337
    private static ModuleFinder limitFinder(ModuleFinder finder,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   338
                                            Set<String> roots,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   339
                                            Set<String> otherMods)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   340
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   341
        // resolve all root modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   342
        Configuration cf = Configuration.empty()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   343
                .resolveRequires(finder,
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37779
diff changeset
   344
                                 ModuleFinder.of(),
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   345
                                 roots);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   346
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   347
        // module name -> reference
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   348
        Map<String, ModuleReference> map = new HashMap<>();
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   349
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   350
        // root modules and their transitive dependences
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   351
        cf.modules().stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   352
            .map(ResolvedModule::reference)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   353
            .forEach(mref -> map.put(mref.descriptor().name(), mref));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   354
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   355
        // additional modules
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   356
        otherMods.stream()
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   357
            .map(finder::find)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   358
            .flatMap(Optional::stream)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   359
            .forEach(mref -> map.putIfAbsent(mref.descriptor().name(), mref));
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   360
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   361
        // set of modules that are observable
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   362
        Set<ModuleReference> mrefs = new HashSet<>(map.values());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   363
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   364
        return new ModuleFinder() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   365
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   366
            public Optional<ModuleReference> find(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   367
                return Optional.ofNullable(map.get(name));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   368
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   369
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   370
            public Set<ModuleReference> findAll() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   371
                return mrefs;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   372
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   373
        };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   374
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   375
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   376
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   377
     * Creates a finder from the module path that is the value of the given
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   378
     * system property.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   379
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   380
    private static ModuleFinder createModulePathFinder(String prop) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   381
        String s = System.getProperty(prop);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   382
        if (s == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   383
            return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   384
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   385
            String[] dirs = s.split(File.pathSeparator);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   386
            Path[] paths = new Path[dirs.length];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   387
            int i = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   388
            for (String dir: dirs) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   389
                paths[i++] = Paths.get(dir);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   390
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   391
            return ModuleFinder.of(paths);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   392
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   393
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   394
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   395
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   396
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   397
     * Process the -XaddReads options to add any additional read edges that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   398
     * are specified on the command-line.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   399
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   400
    private static void addExtraReads(Layer bootLayer) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   401
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   402
        // decode the command line options
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   403
        Map<String, Set<String>> map = decode("jdk.launcher.addreads.");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   404
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   405
        for (Map.Entry<String, Set<String>> e : map.entrySet()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   406
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   407
            // the key is $MODULE
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   408
            String mn = e.getKey();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   409
            Optional<Module> om = bootLayer.findModule(mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   410
            if (!om.isPresent())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   411
                fail("Unknown module: " + mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   412
            Module m = om.get();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   413
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   414
            // the value is the set of other modules (by name)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   415
            for (String name : e.getValue()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   416
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   417
                Module other;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   418
                if (ALL_UNNAMED.equals(name)) {
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   419
                    Modules.addReadsAllUnnamed(m);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   420
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   421
                    om = bootLayer.findModule(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   422
                    if (!om.isPresent())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   423
                        fail("Unknown module: " + name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   424
                    other = om.get();
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   425
                    Modules.addReads(m, other);
36511
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
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   429
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   430
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   431
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
     * Process the -XaddExports options to add any additional read edges that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   435
     * are specified on the command-line.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   436
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   437
    private static void addExtraExports(Layer bootLayer) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   438
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   439
        // decode the command line options
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   440
        Map<String, Set<String>> map = decode("jdk.launcher.addexports.");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   441
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   442
        for (Map.Entry<String, Set<String>> e : map.entrySet()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   443
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   444
            // the key is $MODULE/$PACKAGE
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   445
            String key = e.getKey();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   446
            String[] s = key.split("/");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   447
            if (s.length != 2)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   448
                fail("Unable to parse: " + key);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   449
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   450
            String mn = s[0];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   451
            String pn = s[1];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   452
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   453
            // The exporting module is in the boot layer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   454
            Module m;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   455
            Optional<Module> om = bootLayer.findModule(mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   456
            if (!om.isPresent())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   457
                fail("Unknown module: " + mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   458
            m = om.get();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   459
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   460
            // the value is the set of modules to export to (by name)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   461
            for (String name : e.getValue()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   462
                boolean allUnnamed = false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   463
                Module other = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   464
                if (ALL_UNNAMED.equals(name)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   465
                    allUnnamed = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   466
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   467
                    om = bootLayer.findModule(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   468
                    if (om.isPresent()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   469
                        other = om.get();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   470
                    } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   471
                        fail("Unknown module: " + name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   472
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   473
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   474
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   475
                if (allUnnamed) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   476
                    Modules.addExportsToAllUnnamed(m, pn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   477
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   478
                    Modules.addExports(m, pn, other);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   479
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   480
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   481
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   482
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   483
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   484
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   485
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   486
     * Decodes the values of -XaddReads or -XaddExports options
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   487
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   488
     * The format of the options is: $KEY=$MODULE(,$MODULE)*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   489
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   490
    private static Map<String, Set<String>> decode(String prefix) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   491
        int index = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   492
        String value = System.getProperty(prefix + index);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   493
        if (value == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   494
            return Collections.emptyMap();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   495
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   496
        Map<String, Set<String>> map = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   497
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   498
        while (value != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   499
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   500
            int pos = value.indexOf('=');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   501
            if (pos == -1)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   502
                fail("Unable to parse: " + value);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   503
            if (pos == 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   504
                fail("Missing module name in: " + value);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   505
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   506
            // key is <module> or <module>/<package>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   507
            String key = value.substring(0, pos);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   508
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   509
            String rhs = value.substring(pos+1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   510
            if (rhs.isEmpty())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   511
                fail("Unable to parse: " + value);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   512
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   513
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   514
            // value is <module>(,<module>)*
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   515
            if (map.containsKey(key))
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   516
                fail(key + " specified more than once");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   517
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   518
            Set<String> values = new HashSet<>();
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   519
            map.put(key, values);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   520
            for (String s : rhs.split(",")) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   521
                if (s.length() > 0) values.add(s);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   522
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   523
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   524
            index++;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   525
            value = System.getProperty(prefix + index);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   526
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   527
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   528
        return map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   529
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   530
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   531
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   532
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   533
     * Throws a RuntimeException with the given message
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   534
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   535
    static void fail(String m) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   536
        throw new RuntimeException(m);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   537
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   538
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   539
    static class PerfCounters {
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   540
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   541
        static PerfCounter systemModulesTime
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   542
            = PerfCounter.newPerfCounter("jdk.module.bootstrap.systemModulesTime");
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   543
        static PerfCounter defineBaseTime
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   544
            = PerfCounter.newPerfCounter("jdk.module.bootstrap.defineBaseTime");
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   545
        static PerfCounter optionsAndRootsTime
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   546
            = PerfCounter.newPerfCounter("jdk.module.bootstrap.optionsAndRootsTime");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   547
        static PerfCounter resolveTime
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   548
            = PerfCounter.newPerfCounter("jdk.module.bootstrap.resolveTime");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   549
        static PerfCounter layerCreateTime
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   550
            = PerfCounter.newPerfCounter("jdk.module.bootstrap.layerCreateTime");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   551
        static PerfCounter loadModulesTime
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   552
            = PerfCounter.newPerfCounter("jdk.module.bootstrap.loadModulesTime");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   553
        static PerfCounter bootstrapTime
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   554
            = PerfCounter.newPerfCounter("jdk.module.bootstrap.totalTime");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   555
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   556
}