src/java.base/share/classes/jdk/internal/module/ModuleBootstrap.java
author jiangli
Fri, 10 Aug 2018 00:35:57 -0400
changeset 51371 3ab8b84e93cd
parent 51327 a19fda433921
child 52427 3c6aa484536c
permissions -rw-r--r--
8207263: Store the Configuration for system modules into CDS archive. Summary: Archive boot layer Configuration. Reviewed-by: redestad, iklam, ccheung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
49285
4d2e3f5abb48 8194746: (fs) Add equivalents of Paths.get to Path interface
bpb
parents: 47471
diff changeset
     2
 * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
36511
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;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    35
import java.net.URI;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.nio.file.Path;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
    37
import java.util.ArrayList;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.util.HashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.util.HashSet;
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
    41
import java.util.Iterator;
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
    42
import java.util.LinkedHashMap;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
    43
import java.util.List;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.util.Map;
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
    45
import java.util.NoSuchElementException;
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
    46
import java.util.Objects;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import java.util.Optional;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import java.util.Set;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import java.util.function.Function;
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
    50
import java.util.stream.Collectors;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
import jdk.internal.loader.BootLoader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
import jdk.internal.loader.BuiltinClassLoader;
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
    54
import jdk.internal.misc.JavaLangAccess;
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
    55
import jdk.internal.misc.JavaLangModuleAccess;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    56
import jdk.internal.misc.SharedSecrets;
50951
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
    57
import jdk.internal.misc.VM;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
import jdk.internal.perf.PerfCounter;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
 * Initializes/boots the module system.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
 * The {@link #boot() boot} method is called early in the startup to initialize
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
 * the module system. In summary, the boot method creates a Configuration by
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
 * resolving a set of module names specified via the launcher (or equivalent)
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 38457
diff changeset
    66
 * -m and --add-modules options. The modules are located on a module path that
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 38457
diff changeset
    67
 * is constructed from the upgrade module path, system modules, and application
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44364
diff changeset
    68
 * module path. The Configuration is instantiated as the boot layer with each
47471
304ef03403b1 8190323: "the the" typos
rriggs
parents: 47216
diff changeset
    69
 * module in the configuration defined to a class loader.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
public final class ModuleBootstrap {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
    private ModuleBootstrap() { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
    private static final String JAVA_BASE = "java.base";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    77
    // the token for "all default modules"
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    78
    private static final String ALL_DEFAULT = "ALL-DEFAULT";
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    79
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
    // the token for "all unnamed modules"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
    private static final String ALL_UNNAMED = "ALL-UNNAMED";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
    // the token for "all system modules"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
    private static final String ALL_SYSTEM = "ALL-SYSTEM";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
    // the token for "all modules on the module path"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
    private static final String ALL_MODULE_PATH = "ALL-MODULE-PATH";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
    89
    // access to java.lang/module
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
    90
    private static final JavaLangModuleAccess JLMA
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
    91
        = SharedSecrets.getJavaLangModuleAccess();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
    92
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
    93
    // The ModulePatcher for the initial configuration
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
    94
    private static final ModulePatcher patcher = initModulePatcher();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
    95
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
    97
     * Returns the ModulePatcher for the initial configuration.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
    98
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
    99
    public static ModulePatcher patcher() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   100
        return patcher;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   101
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   102
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   103
    // ModuleFinders for the initial configuration
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   104
    private static volatile ModuleFinder unlimitedFinder;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   105
    private static volatile ModuleFinder limitedFinder;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   106
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   107
    /**
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   108
     * Returns the ModuleFinder for the initial configuration before
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   109
     * observability is limited by the --limit-modules command line option.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   110
     *
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   111
     * @apiNote Used to support locating modules {@code java.instrument} and
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   112
     * {@code jdk.management.agent} modules when they are loaded dynamically.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
     */
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   114
    public static ModuleFinder unlimitedFinder() {
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   115
        ModuleFinder finder = unlimitedFinder;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   116
        if (finder == null) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   117
            return ModuleFinder.ofSystem();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   118
        } else {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   119
            return finder;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   120
        }
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   121
    }
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   122
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   123
    /**
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   124
     * Returns the ModuleFinder for the initial configuration.
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   125
     *
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   126
     * @apiNote Used to support "{@code java --list-modules}".
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   127
     */
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   128
    public static ModuleFinder limitedFinder() {
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   129
        ModuleFinder finder = limitedFinder;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   130
        if (finder == null) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   131
            return unlimitedFinder();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   132
        } else {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   133
            return finder;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   134
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
    /**
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44364
diff changeset
   138
     * Initialize the module system, returning the boot layer.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
     *
50700
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents: 49285
diff changeset
   140
     * @see java.lang.System#initPhase2(boolean, boolean)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
     */
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   142
    public static ModuleLayer boot() throws Exception {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   143
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   144
        // Step 0: Command line options
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   145
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   146
        long t0 = System.nanoTime();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   147
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   148
        ModuleFinder upgradeModulePath = finderFor("jdk.module.upgrade.path");
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   149
        ModuleFinder appModulePath = finderFor("jdk.module.path");
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   150
        boolean isPatched = patcher.hasPatches();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   152
        String mainModule = System.getProperty("jdk.module.main");
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   153
        Set<String> addModules = addModules();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   154
        Set<String> limitModules = limitModules();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   155
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   156
        PrintStream traceOutput = null;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   157
        String trace = getAndRemoveProperty("jdk.module.showModuleResolution");
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   158
        if (trace != null && Boolean.parseBoolean(trace))
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   159
            traceOutput = System.out;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   160
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   161
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   162
        // Step 1: The observable system modules, either all system modules
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   163
        // or the system modules pre-generated for the initial module (the
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   164
        // initial module may be the unnamed module). If the system modules
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   165
        // are pre-generated for the initial module then resolution can be
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   166
        // skipped.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   168
        long t1 = System.nanoTime();
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   169
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   170
        SystemModules systemModules = null;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   171
        ModuleFinder systemModuleFinder;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   172
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   173
        boolean haveModulePath = (appModulePath != null || upgradeModulePath != null);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   174
        boolean needResolution = true;
51371
3ab8b84e93cd 8207263: Store the Configuration for system modules into CDS archive.
jiangli
parents: 51327
diff changeset
   175
        boolean canArchive = false;
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   176
50951
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   177
        // If the java heap was archived at CDS dump time and the environment
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   178
        // at dump time matches the current environment then use the archived
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   179
        // system modules and finder.
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   180
        ArchivedModuleGraph archivedModuleGraph = ArchivedModuleGraph.get(mainModule);
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   181
        if (archivedModuleGraph != null
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   182
                && !haveModulePath
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   183
                && addModules.isEmpty()
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   184
                && limitModules.isEmpty()
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   185
                && !isPatched) {
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   186
            systemModules = archivedModuleGraph.systemModules();
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   187
            systemModuleFinder = archivedModuleGraph.finder();
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   188
            needResolution = (traceOutput != null);
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   189
        } else {
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   190
            if (!haveModulePath && addModules.isEmpty() && limitModules.isEmpty()) {
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   191
                systemModules = SystemModuleFinders.systemModules(mainModule);
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   192
                if (systemModules != null && !isPatched) {
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   193
                    needResolution = (traceOutput != null);
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   194
                    canArchive = true;
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   195
                }
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   196
            }
50951
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   197
            if (systemModules == null) {
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   198
                // all system modules are observable
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   199
                systemModules = SystemModuleFinders.allSystemModules();
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   200
            }
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   201
            if (systemModules != null) {
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   202
                // images build
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   203
                systemModuleFinder = SystemModuleFinders.of(systemModules);
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   204
            } else {
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   205
                // exploded build or testing
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   206
                systemModules = new ExplodedSystemModules();
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   207
                systemModuleFinder = SystemModuleFinders.ofSystem();
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   208
            }
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   209
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   210
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   211
        Counters.add("jdk.module.boot.1.systemModulesTime", t1);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   213
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   214
        // Step 2: Define and load java.base. This patches all classes loaded
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   215
        // to date so that they are members of java.base. Once java.base is
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   216
        // loaded then resources in java.base are available for error messages
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   217
        // needed from here on.
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   218
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   219
        long t2 = System.nanoTime();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   220
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   221
        ModuleReference base = systemModuleFinder.find(JAVA_BASE).orElse(null);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   222
        if (base == null)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
            throw new InternalError(JAVA_BASE + " not found");
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   224
        URI baseUri = base.location().orElse(null);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   225
        if (baseUri == null)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   226
            throw new InternalError(JAVA_BASE + " does not have a location");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
        BootLoader.loadModule(base);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   228
        Modules.defineModule(null, base.descriptor(), baseUri);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   230
        Counters.add("jdk.module.boot.2.defineBaseTime", t2);
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   231
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   232
50700
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents: 49285
diff changeset
   233
        // Step 2a: Scan all modules when --validate-modules specified
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   234
47010
d8dd5c878ce5 8186145: tools/launcher/modules/validate/ValidateModulesTest.java fails when launched with -XX:+EnableJVMCI
mchung
parents: 46096
diff changeset
   235
        if (getAndRemoveProperty("jdk.module.validation") != null) {
50700
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents: 49285
diff changeset
   236
            int errors = ModulePathValidator.scanAllModules(System.out);
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents: 49285
diff changeset
   237
            if (errors > 0) {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents: 49285
diff changeset
   238
                fail("Validation of module path failed");
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents: 49285
diff changeset
   239
            }
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   240
        }
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   241
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   242
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   243
        // Step 3: If resolution is needed then create the module finder and
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   244
        // the set of root modules to resolve.
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   245
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   246
        long t3 = System.nanoTime();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   248
        ModuleFinder savedModuleFinder = null;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   249
        ModuleFinder finder;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   250
        Set<String> roots;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   251
        if (needResolution) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   253
            // upgraded modules override the modules in the run-time image
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   254
            if (upgradeModulePath != null)
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   255
                systemModuleFinder = ModuleFinder.compose(upgradeModulePath,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   256
                                                          systemModuleFinder);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   257
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   258
            // The module finder: [--upgrade-module-path] system [--module-path]
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   259
            if (appModulePath != null) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   260
                finder = ModuleFinder.compose(systemModuleFinder, appModulePath);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   261
            } else {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   262
                finder = systemModuleFinder;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   263
            }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   264
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   265
            // The root modules to resolve
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   266
            roots = new HashSet<>();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   268
            // launcher -m option to specify the main/initial module
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   269
            if (mainModule != null)
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   270
                roots.add(mainModule);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   271
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   272
            // additional module(s) specified by --add-modules
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   273
            boolean addAllDefaultModules = false;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   274
            boolean addAllSystemModules = false;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   275
            boolean addAllApplicationModules = false;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   276
            for (String mod : addModules) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   277
                switch (mod) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   278
                    case ALL_DEFAULT:
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   279
                        addAllDefaultModules = true;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   280
                        break;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   281
                    case ALL_SYSTEM:
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   282
                        addAllSystemModules = true;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   283
                        break;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   284
                    case ALL_MODULE_PATH:
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   285
                        addAllApplicationModules = true;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   286
                        break;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   287
                    default:
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   288
                        roots.add(mod);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   289
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   290
            }
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   291
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   292
            // --limit-modules
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   293
            savedModuleFinder = finder;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   294
            if (!limitModules.isEmpty()) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   295
                finder = limitFinder(finder, limitModules, roots);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   296
            }
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   297
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   298
            // If there is no initial module specified then assume that the initial
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   299
            // module is the unnamed module of the application class loader. This
50738
6cc2dc161c64 8197532: Re-examine policy for the default set of modules when compiling or running code on the class path
alanb
parents: 50700
diff changeset
   300
            // is implemented by resolving all observable modules that export an
6cc2dc161c64 8197532: Re-examine policy for the default set of modules when compiling or running code on the class path
alanb
parents: 50700
diff changeset
   301
            // API. Modules that have the DO_NOT_RESOLVE_BY_DEFAULT bit set in
6cc2dc161c64 8197532: Re-examine policy for the default set of modules when compiling or running code on the class path
alanb
parents: 50700
diff changeset
   302
            // their ModuleResolution attribute flags are excluded from the
6cc2dc161c64 8197532: Re-examine policy for the default set of modules when compiling or running code on the class path
alanb
parents: 50700
diff changeset
   303
            // default set of roots.
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   304
            if (mainModule == null || addAllDefaultModules) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   305
                roots.addAll(DefaultRoots.compute(systemModuleFinder, finder));
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   306
            }
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   307
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   308
            // If `--add-modules ALL-SYSTEM` is specified then all observable system
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   309
            // modules will be resolved.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   310
            if (addAllSystemModules) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   311
                ModuleFinder f = finder;  // observable modules
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   312
                systemModuleFinder.findAll()
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   313
                    .stream()
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   314
                    .map(ModuleReference::descriptor)
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   315
                    .map(ModuleDescriptor::name)
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   316
                    .filter(mn -> f.find(mn).isPresent())  // observable
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   317
                    .forEach(mn -> roots.add(mn));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   318
            }
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   319
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   320
            // If `--add-modules ALL-MODULE-PATH` is specified then all observable
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   321
            // modules on the application module path will be resolved.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   322
            if (appModulePath != null && addAllApplicationModules) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   323
                ModuleFinder f = finder;  // observable modules
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   324
                appModulePath.findAll()
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   325
                    .stream()
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   326
                    .map(ModuleReference::descriptor)
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   327
                    .map(ModuleDescriptor::name)
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   328
                    .filter(mn -> f.find(mn).isPresent())  // observable
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   329
                    .forEach(mn -> roots.add(mn));
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   330
            }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   331
        } else {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   332
            // no resolution case
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   333
            finder = systemModuleFinder;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   334
            roots = null;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   335
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   336
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   337
        Counters.add("jdk.module.boot.3.optionsAndRootsTime", t3);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   338
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   339
        // Step 4: Resolve the root modules, with service binding, to create
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   340
        // the configuration for the boot layer. If resolution is not needed
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   341
        // then create the configuration for the boot layer from the
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   342
        // readability graph created at link time.
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   343
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   344
        long t4 = System.nanoTime();
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   345
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   346
        Configuration cf;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   347
        if (needResolution) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   348
            cf = JLMA.resolveAndBind(finder, roots, traceOutput);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   349
        } else {
51371
3ab8b84e93cd 8207263: Store the Configuration for system modules into CDS archive.
jiangli
parents: 51327
diff changeset
   350
            if (archivedModuleGraph != null) {
3ab8b84e93cd 8207263: Store the Configuration for system modules into CDS archive.
jiangli
parents: 51327
diff changeset
   351
                cf = archivedModuleGraph.configuration();
3ab8b84e93cd 8207263: Store the Configuration for system modules into CDS archive.
jiangli
parents: 51327
diff changeset
   352
            } else {
3ab8b84e93cd 8207263: Store the Configuration for system modules into CDS archive.
jiangli
parents: 51327
diff changeset
   353
                Map<String, Set<String>> map = systemModules.moduleReads();
3ab8b84e93cd 8207263: Store the Configuration for system modules into CDS archive.
jiangli
parents: 51327
diff changeset
   354
                cf = JLMA.newConfiguration(systemModuleFinder, map);
3ab8b84e93cd 8207263: Store the Configuration for system modules into CDS archive.
jiangli
parents: 51327
diff changeset
   355
            }
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   356
        }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   357
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   358
        // check that modules specified to --patch-module are resolved
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   359
        if (isPatched) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   360
            patcher.patchedModules()
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   361
                    .stream()
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   362
                    .filter(mn -> !cf.findModule(mn).isPresent())
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   363
                    .forEach(mn -> warnUnknownModule(PATCH_MODULE, mn));
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   364
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   365
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   366
        Counters.add("jdk.module.boot.4.resolveTime", t4);
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   367
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   368
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   369
        // Step 5: Map the modules in the configuration to class loaders.
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   370
        // The static configuration provides the mapping of standard and JDK
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   371
        // modules to the boot and platform loaders. All other modules (JDK
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   372
        // tool modules, and both explicit and automatic modules on the
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   373
        // application module path) are defined to the application class
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   374
        // loader.
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   375
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   376
        long t5 = System.nanoTime();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   377
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   378
        // mapping of modules to class loaders
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   379
        Function<String, ClassLoader> clf = ModuleLoaderMap.mappingFunction(cf);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   380
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   381
        // 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
   382
        // loaded from the runtime image
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   383
        if (haveModulePath) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   384
            for (ResolvedModule resolvedModule : cf.modules()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   385
                ModuleReference mref = resolvedModule.reference();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   386
                String name = mref.descriptor().name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   387
                ClassLoader cl = clf.apply(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   388
                if (cl == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   389
                    if (upgradeModulePath != null
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   390
                            && upgradeModulePath.find(name).isPresent())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   391
                        fail(name + ": cannot be loaded from upgrade module path");
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   392
                    if (!systemModuleFinder.find(name).isPresent())
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   393
                        fail(name + ": cannot be loaded from application module path");
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
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   398
        // check for split packages in the modules mapped to the built-in loaders
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   399
        if (systemModules.hasSplitPackages() || isPatched || haveModulePath) {
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   400
            checkSplitPackages(cf, clf);
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42774
diff changeset
   401
        }
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   402
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   403
        // load/register the modules with the built-in class loaders
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   404
        loadModules(cf, clf);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   405
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   406
        Counters.add("jdk.module.boot.5.loadModulesTime", t5);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   407
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   408
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   409
        // Step 6: Define all modules to the VM
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   410
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   411
        long t6 = System.nanoTime();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   412
        ModuleLayer bootLayer = ModuleLayer.empty().defineModules(cf, clf);
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   413
        Counters.add("jdk.module.boot.6.layerCreateTime", t6);
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   414
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   415
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   416
        // Step 7: Miscellaneous
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   417
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   418
        // check incubating status
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   419
        if (systemModules.hasIncubatorModules() || haveModulePath) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   420
            checkIncubatingStatus(cf);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   421
        }
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   422
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   423
        // --add-reads, --add-exports/--add-opens, and --illegal-access
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   424
        long t7 = System.nanoTime();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   425
        addExtraReads(bootLayer);
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   426
        boolean extraExportsOrOpens = addExtraExportsAndOpens(bootLayer);
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   427
        addIllegalAccess(upgradeModulePath, systemModules, bootLayer, extraExportsOrOpens);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   428
        Counters.add("jdk.module.boot.7.adjustModulesTime", t7);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   429
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   430
        // save module finders for later use
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   431
        if (savedModuleFinder != null) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   432
            unlimitedFinder = new SafeModuleFinder(savedModuleFinder);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   433
            if (savedModuleFinder != finder)
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   434
                limitedFinder = new SafeModuleFinder(finder);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   435
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   436
51371
3ab8b84e93cd 8207263: Store the Configuration for system modules into CDS archive.
jiangli
parents: 51327
diff changeset
   437
        // Module graph can be archived at CDS dump time. Only allow the
3ab8b84e93cd 8207263: Store the Configuration for system modules into CDS archive.
jiangli
parents: 51327
diff changeset
   438
        // unnamed module case for now.
3ab8b84e93cd 8207263: Store the Configuration for system modules into CDS archive.
jiangli
parents: 51327
diff changeset
   439
        if (canArchive && (mainModule == null)) {
3ab8b84e93cd 8207263: Store the Configuration for system modules into CDS archive.
jiangli
parents: 51327
diff changeset
   440
            ArchivedModuleGraph.archive(mainModule, systemModules,
3ab8b84e93cd 8207263: Store the Configuration for system modules into CDS archive.
jiangli
parents: 51327
diff changeset
   441
                                        systemModuleFinder, cf);
3ab8b84e93cd 8207263: Store the Configuration for system modules into CDS archive.
jiangli
parents: 51327
diff changeset
   442
        }
3ab8b84e93cd 8207263: Store the Configuration for system modules into CDS archive.
jiangli
parents: 51327
diff changeset
   443
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   444
        // total time to initialize
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   445
        Counters.add("jdk.module.boot.totalTime", t0);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   446
        Counters.publish();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   447
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   448
        return bootLayer;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   449
    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   450
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   451
    /**
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   452
     * Load/register the modules to the built-in class loaders.
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   453
     */
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   454
    private static void loadModules(Configuration cf,
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   455
                                    Function<String, ClassLoader> clf) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   456
        for (ResolvedModule resolvedModule : cf.modules()) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   457
            ModuleReference mref = resolvedModule.reference();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   458
            String name = resolvedModule.name();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   459
            ClassLoader loader = clf.apply(name);
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   460
            if (loader == null) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   461
                // skip java.base as it is already loaded
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   462
                if (!name.equals(JAVA_BASE)) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   463
                    BootLoader.loadModule(mref);
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   464
                }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   465
            } else if (loader instanceof BuiltinClassLoader) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   466
                ((BuiltinClassLoader) loader).loadModule(mref);
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   467
            }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   468
        }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   469
    }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   470
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   471
    /**
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   472
     * Checks for split packages between modules defined to the built-in class
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   473
     * loaders.
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   474
     */
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   475
    private static void checkSplitPackages(Configuration cf,
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   476
                                           Function<String, ClassLoader> clf) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   477
        Map<String, String> packageToModule = new HashMap<>();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   478
        for (ResolvedModule resolvedModule : cf.modules()) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   479
            ModuleDescriptor descriptor = resolvedModule.reference().descriptor();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   480
            String name = descriptor.name();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   481
            ClassLoader loader = clf.apply(name);
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   482
            if (loader == null || loader instanceof BuiltinClassLoader) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   483
                for (String p : descriptor.packages()) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   484
                    String other = packageToModule.putIfAbsent(p, name);
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   485
                    if (other != null) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   486
                        String msg = "Package " + p + " in both module "
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   487
                                     + name + " and module " + other;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   488
                        throw new LayerInstantiationException(msg);
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   489
                    }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   490
                }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   491
            }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   492
        }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   493
    }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   494
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   495
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   496
     * Returns a ModuleFinder that limits observability to the given root
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   497
     * modules, their transitive dependences, plus a set of other modules.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   498
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   499
    private static ModuleFinder limitFinder(ModuleFinder finder,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   500
                                            Set<String> roots,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   501
                                            Set<String> otherMods)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   502
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   503
        // resolve all root modules
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42774
diff changeset
   504
        Configuration cf = Configuration.empty().resolve(finder,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42774
diff changeset
   505
                                                         ModuleFinder.of(),
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42774
diff changeset
   506
                                                         roots);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   507
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   508
        // module name -> reference
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   509
        Map<String, ModuleReference> map = new HashMap<>();
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   510
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   511
        // root modules and their transitive dependences
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   512
        cf.modules().stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   513
            .map(ResolvedModule::reference)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   514
            .forEach(mref -> map.put(mref.descriptor().name(), mref));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   515
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   516
        // additional modules
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   517
        otherMods.stream()
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   518
            .map(finder::find)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   519
            .flatMap(Optional::stream)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   520
            .forEach(mref -> map.putIfAbsent(mref.descriptor().name(), mref));
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   521
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   522
        // set of modules that are observable
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   523
        Set<ModuleReference> mrefs = new HashSet<>(map.values());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   524
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   525
        return new ModuleFinder() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   526
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   527
            public Optional<ModuleReference> find(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   528
                return Optional.ofNullable(map.get(name));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   529
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   530
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   531
            public Set<ModuleReference> findAll() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   532
                return mrefs;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   533
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   534
        };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   535
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   536
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   537
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   538
     * Creates a finder from the module path that is the value of the given
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42774
diff changeset
   539
     * system property and optionally patched by --patch-module
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   540
     */
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   541
    private static ModuleFinder finderFor(String prop) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   542
        String s = System.getProperty(prop);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   543
        if (s == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   544
            return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   545
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   546
            String[] dirs = s.split(File.pathSeparator);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   547
            Path[] paths = new Path[dirs.length];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   548
            int i = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   549
            for (String dir: dirs) {
49285
4d2e3f5abb48 8194746: (fs) Add equivalents of Paths.get to Path interface
bpb
parents: 47471
diff changeset
   550
                paths[i++] = Path.of(dir);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   551
            }
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42774
diff changeset
   552
            return ModulePath.of(patcher, paths);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   553
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   554
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   555
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   556
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   557
     * Initialize the module patcher for the initial configuration passed on the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   558
     * value of the --patch-module options.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   559
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   560
    private static ModulePatcher initModulePatcher() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   561
        Map<String, List<String>> map = decode("jdk.module.patch.",
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   562
                                               File.pathSeparator,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   563
                                               false);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   564
        return new ModulePatcher(map);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   565
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   566
41114
f83e9aebbab4 8165634: Support multiple --add-modules options on the command line
hseigel
parents: 40261
diff changeset
   567
    /**
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   568
     * Returns the set of module names specified by --add-module options.
41114
f83e9aebbab4 8165634: Support multiple --add-modules options on the command line
hseigel
parents: 40261
diff changeset
   569
     */
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   570
    private static Set<String> addModules() {
41114
f83e9aebbab4 8165634: Support multiple --add-modules options on the command line
hseigel
parents: 40261
diff changeset
   571
        String prefix = "jdk.module.addmods.";
f83e9aebbab4 8165634: Support multiple --add-modules options on the command line
hseigel
parents: 40261
diff changeset
   572
        int index = 0;
f83e9aebbab4 8165634: Support multiple --add-modules options on the command line
hseigel
parents: 40261
diff changeset
   573
        // the system property is removed after decoding
f83e9aebbab4 8165634: Support multiple --add-modules options on the command line
hseigel
parents: 40261
diff changeset
   574
        String value = getAndRemoveProperty(prefix + index);
f83e9aebbab4 8165634: Support multiple --add-modules options on the command line
hseigel
parents: 40261
diff changeset
   575
        if (value == null) {
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 50951
diff changeset
   576
            return Set.of();
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   577
        } else {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   578
            Set<String> modules = new HashSet<>();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   579
            while (value != null) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   580
                for (String s : value.split(",")) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   581
                    if (s.length() > 0) modules.add(s);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   582
                }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   583
                index++;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   584
                value = getAndRemoveProperty(prefix + index);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   585
            }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   586
            return modules;
41114
f83e9aebbab4 8165634: Support multiple --add-modules options on the command line
hseigel
parents: 40261
diff changeset
   587
        }
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   588
    }
41114
f83e9aebbab4 8165634: Support multiple --add-modules options on the command line
hseigel
parents: 40261
diff changeset
   589
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   590
    /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   591
     * Returns the set of module names specified by --limit-modules.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   592
     */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   593
    private static Set<String> limitModules() {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   594
        String value = getAndRemoveProperty("jdk.module.limitmods");
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   595
        if (value == null) {
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 50951
diff changeset
   596
            return Set.of();
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   597
        } else {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   598
            Set<String> names = new HashSet<>();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   599
            for (String name : value.split(",")) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   600
                if (name.length() > 0) names.add(name);
41114
f83e9aebbab4 8165634: Support multiple --add-modules options on the command line
hseigel
parents: 40261
diff changeset
   601
            }
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   602
            return names;
41114
f83e9aebbab4 8165634: Support multiple --add-modules options on the command line
hseigel
parents: 40261
diff changeset
   603
        }
f83e9aebbab4 8165634: Support multiple --add-modules options on the command line
hseigel
parents: 40261
diff changeset
   604
    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   605
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   606
    /**
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 38457
diff changeset
   607
     * Process the --add-reads options to add any additional read edges that
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   608
     * are specified on the command-line.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   609
     */
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44364
diff changeset
   610
    private static void addExtraReads(ModuleLayer bootLayer) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   611
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   612
        // decode the command line options
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   613
        Map<String, List<String>> map = decode("jdk.module.addreads.");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   614
        if (map.isEmpty())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   615
            return;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   616
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   617
        for (Map.Entry<String, List<String>> e : map.entrySet()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   618
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   619
            // the key is $MODULE
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   620
            String mn = e.getKey();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   621
            Optional<Module> om = bootLayer.findModule(mn);
41414
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   622
            if (!om.isPresent()) {
42774
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   623
                warnUnknownModule(ADD_READS, mn);
41414
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   624
                continue;
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   625
            }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   626
            Module m = om.get();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   627
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   628
            // the value is the set of other modules (by name)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   629
            for (String name : e.getValue()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   630
                if (ALL_UNNAMED.equals(name)) {
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   631
                    Modules.addReadsAllUnnamed(m);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   632
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   633
                    om = bootLayer.findModule(name);
41414
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   634
                    if (om.isPresent()) {
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   635
                        Modules.addReads(m, om.get());
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   636
                    } else {
42774
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   637
                        warnUnknownModule(ADD_READS, name);
41414
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   638
                    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   639
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   640
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   641
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   642
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   643
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   644
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   645
     * Process the --add-exports and --add-opens options to export/open
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   646
     * additional packages specified on the command-line.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   647
     */
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   648
    private static boolean addExtraExportsAndOpens(ModuleLayer bootLayer) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   649
        boolean extraExportsOrOpens = false;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   650
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   651
        // --add-exports
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   652
        String prefix = "jdk.module.addexports.";
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   653
        Map<String, List<String>> extraExports = decode(prefix);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   654
        if (!extraExports.isEmpty()) {
44364
9cc9dc782213 8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents: 44359
diff changeset
   655
            addExtraExportsOrOpens(bootLayer, extraExports, false);
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   656
            extraExportsOrOpens = true;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   657
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   658
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   659
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   660
        // --add-opens
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   661
        prefix = "jdk.module.addopens.";
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   662
        Map<String, List<String>> extraOpens = decode(prefix);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   663
        if (!extraOpens.isEmpty()) {
44364
9cc9dc782213 8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents: 44359
diff changeset
   664
            addExtraExportsOrOpens(bootLayer, extraOpens, true);
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   665
            extraExportsOrOpens = true;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   666
        }
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42774
diff changeset
   667
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   668
        return extraExportsOrOpens;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   669
    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   670
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44364
diff changeset
   671
    private static void addExtraExportsOrOpens(ModuleLayer bootLayer,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   672
                                               Map<String, List<String>> map,
44364
9cc9dc782213 8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents: 44359
diff changeset
   673
                                               boolean opens)
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   674
    {
42774
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   675
        String option = opens ? ADD_OPENS : ADD_EXPORTS;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   676
        for (Map.Entry<String, List<String>> e : map.entrySet()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   677
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   678
            // the key is $MODULE/$PACKAGE
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   679
            String key = e.getKey();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   680
            String[] s = key.split("/");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   681
            if (s.length != 2)
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43813
diff changeset
   682
                fail(unableToParse(option, "<module>/<package>", key));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   683
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   684
            String mn = s[0];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   685
            String pn = s[1];
41414
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   686
            if (mn.isEmpty() || pn.isEmpty())
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43813
diff changeset
   687
                fail(unableToParse(option, "<module>/<package>", key));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   688
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   689
            // The exporting module is in the boot layer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   690
            Module m;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   691
            Optional<Module> om = bootLayer.findModule(mn);
41414
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   692
            if (!om.isPresent()) {
42774
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   693
                warnUnknownModule(option, mn);
41414
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   694
                continue;
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   695
            }
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   696
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   697
            m = om.get();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   698
41414
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   699
            if (!m.getDescriptor().packages().contains(pn)) {
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   700
                warn("package " + pn + " not in " + mn);
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   701
                continue;
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   702
            }
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   703
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   704
            // the value is the set of modules to export to (by name)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   705
            for (String name : e.getValue()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   706
                boolean allUnnamed = false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   707
                Module other = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   708
                if (ALL_UNNAMED.equals(name)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   709
                    allUnnamed = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   710
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   711
                    om = bootLayer.findModule(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   712
                    if (om.isPresent()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   713
                        other = om.get();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   714
                    } else {
42774
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   715
                        warnUnknownModule(option, name);
41414
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   716
                        continue;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   717
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   718
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   719
                if (allUnnamed) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   720
                    if (opens) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   721
                        Modules.addOpensToAllUnnamed(m, pn);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   722
                    } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   723
                        Modules.addExportsToAllUnnamed(m, pn);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   724
                    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   725
                } else {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   726
                    if (opens) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   727
                        Modules.addOpens(m, pn, other);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   728
                    } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   729
                        Modules.addExports(m, pn, other);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   730
                    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   731
                }
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   732
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   733
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   734
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   735
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   736
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   737
    /**
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   738
     * Process the --illegal-access option (and its default) to open packages
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   739
     * of system modules in the boot layer to code in unnamed modules.
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   740
     */
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   741
    private static void addIllegalAccess(ModuleFinder upgradeModulePath,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   742
                                         SystemModules systemModules,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   743
                                         ModuleLayer bootLayer,
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   744
                                         boolean extraExportsOrOpens) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   745
        String value = getAndRemoveProperty("jdk.module.illegalAccess");
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   746
        IllegalAccessLogger.Mode mode = IllegalAccessLogger.Mode.ONESHOT;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   747
        if (value != null) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   748
            switch (value) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   749
                case "deny":
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   750
                    return;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   751
                case "permit":
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   752
                    break;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   753
                case "warn":
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   754
                    mode = IllegalAccessLogger.Mode.WARN;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   755
                    break;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   756
                case "debug":
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   757
                    mode = IllegalAccessLogger.Mode.DEBUG;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   758
                    break;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   759
                default:
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   760
                    fail("Value specified to --illegal-access not recognized:"
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   761
                            + " '" + value + "'");
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   762
                    return;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   763
            }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   764
        }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   765
        IllegalAccessLogger.Builder builder
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   766
            = new IllegalAccessLogger.Builder(mode, System.err);
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   767
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   768
        Map<String, Set<String>> map1 = systemModules.concealedPackagesToOpen();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   769
        Map<String, Set<String>> map2 = systemModules.exportedPackagesToOpen();
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   770
        if (map1.isEmpty() && map2.isEmpty()) {
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   771
            // need to generate (exploded build)
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   772
            IllegalAccessMaps maps = IllegalAccessMaps.generate(limitedFinder());
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   773
            map1 = maps.concealedPackagesToOpen();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   774
            map2 = maps.exportedPackagesToOpen();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   775
        }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   776
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   777
        // open specific packages in the system modules
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   778
        for (Module m : bootLayer.modules()) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   779
            ModuleDescriptor descriptor = m.getDescriptor();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   780
            String name = m.getName();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   781
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   782
            // skip open modules
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   783
            if (descriptor.isOpen()) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   784
                continue;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   785
            }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   786
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   787
            // skip modules loaded from the upgrade module path
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   788
            if (upgradeModulePath != null
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   789
                && upgradeModulePath.find(name).isPresent()) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   790
                continue;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   791
            }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   792
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   793
            Set<String> concealedPackages = map1.getOrDefault(name, Set.of());
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   794
            Set<String> exportedPackages = map2.getOrDefault(name, Set.of());
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   795
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   796
            // refresh the set of concealed and exported packages if needed
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   797
            if (extraExportsOrOpens) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   798
                concealedPackages = new HashSet<>(concealedPackages);
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   799
                exportedPackages = new HashSet<>(exportedPackages);
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   800
                Iterator<String> iterator = concealedPackages.iterator();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   801
                while (iterator.hasNext()) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   802
                    String pn = iterator.next();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   803
                    if (m.isExported(pn, BootLoader.getUnnamedModule())) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   804
                        // concealed package is exported to ALL-UNNAMED
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   805
                        iterator.remove();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   806
                        exportedPackages.add(pn);
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   807
                    }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   808
                }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   809
                iterator = exportedPackages.iterator();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   810
                while (iterator.hasNext()) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   811
                    String pn = iterator.next();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   812
                    if (m.isOpen(pn, BootLoader.getUnnamedModule())) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   813
                        // exported package is opened to ALL-UNNAMED
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   814
                        iterator.remove();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   815
                    }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   816
                }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   817
            }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   818
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   819
            // log reflective access to all types in concealed packages
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   820
            builder.logAccessToConcealedPackages(m, concealedPackages);
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   821
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   822
            // log reflective access to non-public members/types in exported packages
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   823
            builder.logAccessToExportedPackages(m, exportedPackages);
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   824
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   825
            // open the packages to unnamed modules
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   826
            JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   827
            jla.addOpensToAllUnnamed(m, concat(concealedPackages.iterator(),
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   828
                                               exportedPackages.iterator()));
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   829
        }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   830
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   831
        builder.complete();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   832
    }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   833
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   834
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   835
     * Decodes the values of --add-reads, -add-exports, --add-opens or
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   836
     * --patch-modules options that are encoded in system properties.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   837
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   838
     * @param prefix the system property prefix
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   839
     * @praam regex the regex for splitting the RHS of the option value
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   840
     */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   841
    private static Map<String, List<String>> decode(String prefix,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   842
                                                    String regex,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   843
                                                    boolean allowDuplicates) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   844
        int index = 0;
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 38457
diff changeset
   845
        // the system property is removed after decoding
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 38457
diff changeset
   846
        String value = getAndRemoveProperty(prefix + index);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   847
        if (value == null)
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 50951
diff changeset
   848
            return Map.of();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   849
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   850
        Map<String, List<String>> map = new HashMap<>();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   851
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   852
        while (value != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   853
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   854
            int pos = value.indexOf('=');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   855
            if (pos == -1)
42774
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   856
                fail(unableToParse(option(prefix), "<module>=<value>", value));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   857
            if (pos == 0)
42774
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   858
                fail(unableToParse(option(prefix), "<module>=<value>", value));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   859
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   860
            // key is <module> or <module>/<package>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   861
            String key = value.substring(0, pos);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   862
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   863
            String rhs = value.substring(pos+1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   864
            if (rhs.isEmpty())
42774
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   865
                fail(unableToParse(option(prefix), "<module>=<value>", value));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   866
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   867
            // value is <module>(,<module>)* or <file>(<pathsep><file>)*
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   868
            if (!allowDuplicates && map.containsKey(key))
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43813
diff changeset
   869
                fail(key + " specified more than once to " + option(prefix));
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   870
            List<String> values = map.computeIfAbsent(key, k -> new ArrayList<>());
42774
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   871
            int ntargets = 0;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   872
            for (String s : rhs.split(regex)) {
42774
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   873
                if (s.length() > 0) {
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   874
                    values.add(s);
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   875
                    ntargets++;
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   876
                }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   877
            }
42774
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   878
            if (ntargets == 0)
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   879
                fail("Target must be specified: " + option(prefix) + " " + value);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   880
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   881
            index++;
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 38457
diff changeset
   882
            value = getAndRemoveProperty(prefix + index);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   883
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   884
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   885
        return map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   886
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   887
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 38457
diff changeset
   888
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   889
     * Decodes the values of --add-reads, -add-exports or --add-opens
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   890
     * which use the "," to separate the RHS of the option value.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   891
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   892
    private static Map<String, List<String>> decode(String prefix) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   893
        return decode(prefix, ",", true);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   894
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   895
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   896
    /**
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 38457
diff changeset
   897
     * Gets and remove the named system property
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 38457
diff changeset
   898
     */
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 38457
diff changeset
   899
    private static String getAndRemoveProperty(String key) {
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 38457
diff changeset
   900
        return (String)System.getProperties().remove(key);
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 38457
diff changeset
   901
    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   902
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   903
    /**
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   904
     * Checks incubating status of modules in the configuration
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   905
     */
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   906
    private static void checkIncubatingStatus(Configuration cf) {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   907
        String incubating = null;
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   908
        for (ResolvedModule resolvedModule : cf.modules()) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   909
            ModuleReference mref = resolvedModule.reference();
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   910
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   911
            // emit warning if the WARN_INCUBATING module resolution bit set
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   912
            if (ModuleResolution.hasIncubatingWarning(mref)) {
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   913
                String mn = mref.descriptor().name();
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   914
                if (incubating == null) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   915
                    incubating = mn;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   916
                } else {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   917
                    incubating += ", " + mn;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   918
                }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   919
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   920
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   921
        if (incubating != null)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   922
            warn("Using incubator modules: " + incubating);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   923
    }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   924
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   925
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   926
     * Throws a RuntimeException with the given message
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   927
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   928
    static void fail(String m) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   929
        throw new RuntimeException(m);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   930
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   931
41414
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   932
    static void warn(String m) {
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   933
        System.err.println("WARNING: " + m);
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   934
    }
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   935
42774
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   936
    static void warnUnknownModule(String option, String mn) {
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43813
diff changeset
   937
        warn("Unknown module: " + mn + " specified to " + option);
42774
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   938
    }
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   939
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   940
    static String unableToParse(String option, String text, String value) {
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   941
        return "Unable to parse " +  option + " " + text + ": " + value;
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   942
    }
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   943
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   944
    private static final String ADD_MODULES  = "--add-modules";
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   945
    private static final String ADD_EXPORTS  = "--add-exports";
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   946
    private static final String ADD_OPENS    = "--add-opens";
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   947
    private static final String ADD_READS    = "--add-reads";
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   948
    private static final String PATCH_MODULE = "--patch-module";
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   949
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   950
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   951
    /*
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   952
     * Returns the command-line option name corresponds to the specified
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   953
     * system property prefix.
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   954
     */
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   955
    static String option(String prefix) {
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   956
        switch (prefix) {
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   957
            case "jdk.module.addexports.":
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   958
                return ADD_EXPORTS;
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   959
            case "jdk.module.addopens.":
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   960
                return ADD_OPENS;
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   961
            case "jdk.module.addreads.":
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   962
                return ADD_READS;
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   963
            case "jdk.module.patch.":
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   964
                return PATCH_MODULE;
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   965
            case "jdk.module.addmods.":
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   966
                return ADD_MODULES;
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   967
            default:
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   968
                throw new IllegalArgumentException(prefix);
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   969
        }
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   970
    }
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   971
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   972
    /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   973
     * Returns an iterator that yields all elements of the first iterator
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   974
     * followed by all the elements of the second iterator.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   975
     */
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   976
    static <T> Iterator<T> concat(Iterator<T> iterator1, Iterator<T> iterator2) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   977
        return new Iterator<T>() {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   978
            @Override
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   979
            public boolean hasNext() {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   980
                return iterator1.hasNext() || iterator2.hasNext();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   981
            }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   982
            @Override
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   983
            public T next() {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   984
                if (iterator1.hasNext()) return iterator1.next();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   985
                if (iterator2.hasNext()) return iterator2.next();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   986
                throw new NoSuchElementException();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   987
            }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   988
        };
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   989
    }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   990
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   991
    /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   992
     * Wraps a (potentially not thread safe) ModuleFinder created during startup
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   993
     * for use after startup.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   994
     */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   995
    static class SafeModuleFinder implements ModuleFinder {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   996
        private final Set<ModuleReference> mrefs;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   997
        private volatile Map<String, ModuleReference> nameToModule;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   998
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   999
        SafeModuleFinder(ModuleFinder finder) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1000
            this.mrefs = Collections.unmodifiableSet(finder.findAll());
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1001
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1002
        @Override
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1003
        public Optional<ModuleReference> find(String name) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1004
            Objects.requireNonNull(name);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1005
            Map<String, ModuleReference> nameToModule = this.nameToModule;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1006
            if (nameToModule == null) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1007
                this.nameToModule = nameToModule = mrefs.stream()
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1008
                        .collect(Collectors.toMap(m -> m.descriptor().name(),
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1009
                                                  Function.identity()));
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1010
            }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1011
            return Optional.ofNullable(nameToModule.get(name));
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1012
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1013
        @Override
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1014
        public Set<ModuleReference> findAll() {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1015
            return mrefs;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1016
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1017
    }
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1018
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1019
    /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1020
     * Counters for startup performance analysis.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1021
     */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1022
    static class Counters {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1023
        private static final boolean PUBLISH_COUNTERS;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1024
        private static final boolean PRINT_COUNTERS;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1025
        private static Map<String, Long> counters;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1026
        static {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1027
            String s = System.getProperty("jdk.module.boot.usePerfData");
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1028
            if (s == null) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1029
                PUBLISH_COUNTERS = false;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1030
                PRINT_COUNTERS = false;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1031
            } else {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1032
                PUBLISH_COUNTERS = true;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1033
                PRINT_COUNTERS = s.equals("debug");
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1034
                counters = new LinkedHashMap<>();  // preserve insert order
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1035
            }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1036
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1037
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1038
        /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1039
         * Add a counter
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1040
         */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1041
        static void add(String name, long start) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1042
            if (PUBLISH_COUNTERS || PRINT_COUNTERS) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1043
                counters.put(name, (System.nanoTime() - start));
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1044
            }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1045
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1046
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1047
        /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1048
         * Publish the counters to the instrumentation buffer or stdout.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1049
         */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1050
        static void publish() {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1051
            if (PUBLISH_COUNTERS || PRINT_COUNTERS) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1052
                for (Map.Entry<String, Long> e : counters.entrySet()) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1053
                    String name = e.getKey();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1054
                    long value = e.getValue();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1055
                    if (PUBLISH_COUNTERS)
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1056
                        PerfCounter.newPerfCounter(name).set(value);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1057
                    if (PRINT_COUNTERS)
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1058
                        System.out.println(name + " = " + value);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1059
                }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1060
            }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1061
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1062
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1063
}