src/java.base/share/classes/jdk/internal/module/ModuleBootstrap.java
author jiangli
Sun, 08 Jul 2018 12:43:05 -0400
changeset 50951 b96466cdfc45
parent 50738 6cc2dc161c64
child 51327 a19fda433921
permissions -rw-r--r--
8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module. Summary: Support system module archiving with unnamed initial module at dump time. Reviewed-by: erikj, coleenp, mchung, iklam, ccheung Contributed-by: alan.bateman@oracle.com, jiangli.zhou@oracle.com
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;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   175
50951
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   176
        // 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
   177
        // 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
   178
        // 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
   179
        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
   180
        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
   181
                && !haveModulePath
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   182
                && 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
   183
                && 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
   184
                && !isPatched) {
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   185
            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
   186
            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
   187
            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
   188
        } else {
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   189
            boolean canArchive = false;
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
            }
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   209
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   210
            // Module graph can be archived at CDS dump time. Only allow the
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   211
            // unnamed module case for now.
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   212
            if (canArchive && (mainModule == null)) {
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   213
                ArchivedModuleGraph.archive(mainModule, systemModules, systemModuleFinder);
b96466cdfc45 8202035: Archive the set of ModuleDescriptor and ModuleReference objects for observable system modules with unnamed initial module.
jiangli
parents: 50738
diff changeset
   214
            }
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   215
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   216
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   217
        Counters.add("jdk.module.boot.1.systemModulesTime", t1);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   219
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   220
        // 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
   221
        // 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
   222
        // loaded then resources in java.base are available for error messages
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   223
        // needed from here on.
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   224
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   225
        long t2 = System.nanoTime();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   226
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   227
        ModuleReference base = systemModuleFinder.find(JAVA_BASE).orElse(null);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   228
        if (base == null)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
            throw new InternalError(JAVA_BASE + " not found");
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   230
        URI baseUri = base.location().orElse(null);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   231
        if (baseUri == null)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   232
            throw new InternalError(JAVA_BASE + " does not have a location");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
        BootLoader.loadModule(base);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   234
        Modules.defineModule(null, base.descriptor(), baseUri);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   236
        Counters.add("jdk.module.boot.2.defineBaseTime", t2);
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   237
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   238
50700
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents: 49285
diff changeset
   239
        // Step 2a: Scan all modules when --validate-modules specified
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   240
47010
d8dd5c878ce5 8186145: tools/launcher/modules/validate/ValidateModulesTest.java fails when launched with -XX:+EnableJVMCI
mchung
parents: 46096
diff changeset
   241
        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
   242
            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
   243
            if (errors > 0) {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents: 49285
diff changeset
   244
                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
   245
            }
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   246
        }
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   247
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   248
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   249
        // 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
   250
        // the set of root modules to resolve.
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   251
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   252
        long t3 = System.nanoTime();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   254
        ModuleFinder savedModuleFinder = null;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   255
        ModuleFinder finder;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   256
        Set<String> roots;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   257
        if (needResolution) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   258
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   259
            // 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
   260
            if (upgradeModulePath != null)
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   261
                systemModuleFinder = ModuleFinder.compose(upgradeModulePath,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   262
                                                          systemModuleFinder);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   264
            // 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
   265
            if (appModulePath != null) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   266
                finder = ModuleFinder.compose(systemModuleFinder, appModulePath);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   267
            } else {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   268
                finder = systemModuleFinder;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   269
            }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   270
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   271
            // The root modules to resolve
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   272
            roots = new HashSet<>();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   273
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   274
            // 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
   275
            if (mainModule != null)
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   276
                roots.add(mainModule);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   277
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   278
            // additional module(s) specified by --add-modules
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   279
            boolean addAllDefaultModules = false;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   280
            boolean addAllSystemModules = false;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   281
            boolean addAllApplicationModules = false;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   282
            for (String mod : addModules) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   283
                switch (mod) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   284
                    case ALL_DEFAULT:
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   285
                        addAllDefaultModules = 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
                    case ALL_SYSTEM:
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   288
                        addAllSystemModules = true;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   289
                        break;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   290
                    case ALL_MODULE_PATH:
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   291
                        addAllApplicationModules = true;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   292
                        break;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   293
                    default:
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   294
                        roots.add(mod);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   295
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
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
            // --limit-modules
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   299
            savedModuleFinder = finder;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   300
            if (!limitModules.isEmpty()) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   301
                finder = limitFinder(finder, limitModules, roots);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   302
            }
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   303
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   304
            // 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
   305
            // 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
   306
            // 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
   307
            // 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
   308
            // 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
   309
            // default set of roots.
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   310
            if (mainModule == null || addAllDefaultModules) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   311
                roots.addAll(DefaultRoots.compute(systemModuleFinder, finder));
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   312
            }
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   313
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   314
            // 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
   315
            // modules will be resolved.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   316
            if (addAllSystemModules) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   317
                ModuleFinder f = finder;  // observable modules
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   318
                systemModuleFinder.findAll()
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   319
                    .stream()
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   320
                    .map(ModuleReference::descriptor)
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   321
                    .map(ModuleDescriptor::name)
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   322
                    .filter(mn -> f.find(mn).isPresent())  // observable
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   323
                    .forEach(mn -> roots.add(mn));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   324
            }
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   325
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   326
            // 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
   327
            // 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
   328
            if (appModulePath != null && addAllApplicationModules) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   329
                ModuleFinder f = finder;  // observable modules
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   330
                appModulePath.findAll()
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   331
                    .stream()
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   332
                    .map(ModuleReference::descriptor)
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   333
                    .map(ModuleDescriptor::name)
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   334
                    .filter(mn -> f.find(mn).isPresent())  // observable
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   335
                    .forEach(mn -> roots.add(mn));
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   336
            }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   337
        } else {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   338
            // no resolution case
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   339
            finder = systemModuleFinder;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   340
            roots = null;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   341
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   342
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   343
        Counters.add("jdk.module.boot.3.optionsAndRootsTime", t3);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   344
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   345
        // 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
   346
        // 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
   347
        // 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
   348
        // readability graph created at link time.
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   349
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   350
        long t4 = System.nanoTime();
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   351
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   352
        Configuration cf;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   353
        if (needResolution) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   354
            cf = JLMA.resolveAndBind(finder, roots, traceOutput);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   355
        } else {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   356
            Map<String, Set<String>> map = systemModules.moduleReads();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   357
            cf = JLMA.newConfiguration(systemModuleFinder, map);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   358
        }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   359
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   360
        // 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
   361
        if (isPatched) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   362
            patcher.patchedModules()
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   363
                    .stream()
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   364
                    .filter(mn -> !cf.findModule(mn).isPresent())
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   365
                    .forEach(mn -> warnUnknownModule(PATCH_MODULE, mn));
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   366
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   367
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   368
        Counters.add("jdk.module.boot.4.resolveTime", t4);
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   369
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   370
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   371
        // Step 5: Map the modules in the configuration to class loaders.
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   372
        // The static configuration provides the mapping of standard and JDK
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   373
        // modules to the boot and platform loaders. All other modules (JDK
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   374
        // tool modules, and both explicit and automatic modules on the
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   375
        // application module path) are defined to the application class
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   376
        // loader.
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   377
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   378
        long t5 = System.nanoTime();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   379
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   380
        // mapping of modules to class loaders
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   381
        Function<String, ClassLoader> clf = ModuleLoaderMap.mappingFunction(cf);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   382
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   383
        // 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
   384
        // loaded from the runtime image
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   385
        if (haveModulePath) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   386
            for (ResolvedModule resolvedModule : cf.modules()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   387
                ModuleReference mref = resolvedModule.reference();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   388
                String name = mref.descriptor().name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   389
                ClassLoader cl = clf.apply(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   390
                if (cl == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   391
                    if (upgradeModulePath != null
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   392
                            && upgradeModulePath.find(name).isPresent())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   393
                        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
   394
                    if (!systemModuleFinder.find(name).isPresent())
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   395
                        fail(name + ": cannot be loaded from application module path");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   396
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   397
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   398
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   399
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   400
        // 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
   401
        if (systemModules.hasSplitPackages() || isPatched || haveModulePath) {
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   402
            checkSplitPackages(cf, clf);
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42774
diff changeset
   403
        }
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   404
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   405
        // load/register the modules with the built-in class loaders
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   406
        loadModules(cf, clf);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   407
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   408
        Counters.add("jdk.module.boot.5.loadModulesTime", t5);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   409
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   410
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   411
        // Step 6: Define all modules to the VM
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   412
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   413
        long t6 = System.nanoTime();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   414
        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
   415
        Counters.add("jdk.module.boot.6.layerCreateTime", t6);
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   416
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
        // Step 7: Miscellaneous
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   419
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   420
        // check incubating status
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   421
        if (systemModules.hasIncubatorModules() || haveModulePath) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   422
            checkIncubatingStatus(cf);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   423
        }
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   424
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   425
        // --add-reads, --add-exports/--add-opens, and --illegal-access
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   426
        long t7 = System.nanoTime();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   427
        addExtraReads(bootLayer);
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   428
        boolean extraExportsOrOpens = addExtraExportsAndOpens(bootLayer);
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   429
        addIllegalAccess(upgradeModulePath, systemModules, bootLayer, extraExportsOrOpens);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   430
        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
   431
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   432
        // save module finders for later use
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   433
        if (savedModuleFinder != null) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   434
            unlimitedFinder = new SafeModuleFinder(savedModuleFinder);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   435
            if (savedModuleFinder != finder)
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   436
                limitedFinder = new SafeModuleFinder(finder);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   437
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   438
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   439
        // total time to initialize
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   440
        Counters.add("jdk.module.boot.totalTime", t0);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   441
        Counters.publish();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   442
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   443
        return bootLayer;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   444
    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   445
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   446
    /**
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   447
     * Load/register the modules to the built-in class loaders.
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   448
     */
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   449
    private static void loadModules(Configuration cf,
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   450
                                    Function<String, ClassLoader> clf) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   451
        for (ResolvedModule resolvedModule : cf.modules()) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   452
            ModuleReference mref = resolvedModule.reference();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   453
            String name = resolvedModule.name();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   454
            ClassLoader loader = clf.apply(name);
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   455
            if (loader == null) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   456
                // skip java.base as it is already loaded
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   457
                if (!name.equals(JAVA_BASE)) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   458
                    BootLoader.loadModule(mref);
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   459
                }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   460
            } else if (loader instanceof BuiltinClassLoader) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   461
                ((BuiltinClassLoader) loader).loadModule(mref);
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   462
            }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   463
        }
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
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   466
    /**
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   467
     * 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
   468
     * loaders.
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
    private static void checkSplitPackages(Configuration cf,
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   471
                                           Function<String, ClassLoader> clf) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   472
        Map<String, String> packageToModule = new HashMap<>();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   473
        for (ResolvedModule resolvedModule : cf.modules()) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   474
            ModuleDescriptor descriptor = resolvedModule.reference().descriptor();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   475
            String name = descriptor.name();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   476
            ClassLoader loader = clf.apply(name);
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   477
            if (loader == null || loader instanceof BuiltinClassLoader) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   478
                for (String p : descriptor.packages()) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   479
                    String other = packageToModule.putIfAbsent(p, name);
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   480
                    if (other != null) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   481
                        String msg = "Package " + p + " in both module "
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   482
                                     + name + " and module " + other;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   483
                        throw new LayerInstantiationException(msg);
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   484
                    }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   485
                }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   486
            }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   487
        }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   488
    }
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
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   491
     * Returns a ModuleFinder that limits observability to the given root
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   492
     * modules, their transitive dependences, plus a set of other modules.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   493
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   494
    private static ModuleFinder limitFinder(ModuleFinder finder,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   495
                                            Set<String> roots,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   496
                                            Set<String> otherMods)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   497
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   498
        // resolve all root modules
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42774
diff changeset
   499
        Configuration cf = Configuration.empty().resolve(finder,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42774
diff changeset
   500
                                                         ModuleFinder.of(),
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42774
diff changeset
   501
                                                         roots);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   502
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   503
        // module name -> reference
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   504
        Map<String, ModuleReference> map = new HashMap<>();
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   505
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   506
        // root modules and their transitive dependences
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   507
        cf.modules().stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   508
            .map(ResolvedModule::reference)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   509
            .forEach(mref -> map.put(mref.descriptor().name(), mref));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   510
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   511
        // additional modules
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   512
        otherMods.stream()
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   513
            .map(finder::find)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   514
            .flatMap(Optional::stream)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   515
            .forEach(mref -> map.putIfAbsent(mref.descriptor().name(), mref));
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   516
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   517
        // set of modules that are observable
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   518
        Set<ModuleReference> mrefs = new HashSet<>(map.values());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   519
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   520
        return new ModuleFinder() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   521
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   522
            public Optional<ModuleReference> find(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   523
                return Optional.ofNullable(map.get(name));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   524
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   525
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   526
            public Set<ModuleReference> findAll() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   527
                return mrefs;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   528
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   529
        };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   530
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   531
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   532
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   533
     * 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
   534
     * system property and optionally patched by --patch-module
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   535
     */
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   536
    private static ModuleFinder finderFor(String prop) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   537
        String s = System.getProperty(prop);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   538
        if (s == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   539
            return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   540
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   541
            String[] dirs = s.split(File.pathSeparator);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   542
            Path[] paths = new Path[dirs.length];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   543
            int i = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   544
            for (String dir: dirs) {
49285
4d2e3f5abb48 8194746: (fs) Add equivalents of Paths.get to Path interface
bpb
parents: 47471
diff changeset
   545
                paths[i++] = Path.of(dir);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   546
            }
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42774
diff changeset
   547
            return ModulePath.of(patcher, paths);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   548
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   549
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   550
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   551
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   552
     * Initialize the module patcher for the initial configuration passed on the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   553
     * value of the --patch-module options.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   554
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   555
    private static ModulePatcher initModulePatcher() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   556
        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
   557
                                               File.pathSeparator,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   558
                                               false);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   559
        return new ModulePatcher(map);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   560
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   561
41114
f83e9aebbab4 8165634: Support multiple --add-modules options on the command line
hseigel
parents: 40261
diff changeset
   562
    /**
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   563
     * 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
   564
     */
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   565
    private static Set<String> addModules() {
41114
f83e9aebbab4 8165634: Support multiple --add-modules options on the command line
hseigel
parents: 40261
diff changeset
   566
        String prefix = "jdk.module.addmods.";
f83e9aebbab4 8165634: Support multiple --add-modules options on the command line
hseigel
parents: 40261
diff changeset
   567
        int index = 0;
f83e9aebbab4 8165634: Support multiple --add-modules options on the command line
hseigel
parents: 40261
diff changeset
   568
        // the system property is removed after decoding
f83e9aebbab4 8165634: Support multiple --add-modules options on the command line
hseigel
parents: 40261
diff changeset
   569
        String value = getAndRemoveProperty(prefix + index);
f83e9aebbab4 8165634: Support multiple --add-modules options on the command line
hseigel
parents: 40261
diff changeset
   570
        if (value == null) {
f83e9aebbab4 8165634: Support multiple --add-modules options on the command line
hseigel
parents: 40261
diff changeset
   571
            return Collections.emptySet();
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   572
        } else {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   573
            Set<String> modules = new HashSet<>();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   574
            while (value != null) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   575
                for (String s : value.split(",")) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   576
                    if (s.length() > 0) modules.add(s);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   577
                }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   578
                index++;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   579
                value = getAndRemoveProperty(prefix + index);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   580
            }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   581
            return modules;
41114
f83e9aebbab4 8165634: Support multiple --add-modules options on the command line
hseigel
parents: 40261
diff changeset
   582
        }
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   583
    }
41114
f83e9aebbab4 8165634: Support multiple --add-modules options on the command line
hseigel
parents: 40261
diff changeset
   584
46096
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
     * 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
   587
     */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   588
    private static Set<String> limitModules() {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   589
        String value = getAndRemoveProperty("jdk.module.limitmods");
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   590
        if (value == null) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   591
            return Collections.emptySet();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   592
        } else {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   593
            Set<String> names = new HashSet<>();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   594
            for (String name : value.split(",")) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   595
                if (name.length() > 0) names.add(name);
41114
f83e9aebbab4 8165634: Support multiple --add-modules options on the command line
hseigel
parents: 40261
diff changeset
   596
            }
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   597
            return names;
41114
f83e9aebbab4 8165634: Support multiple --add-modules options on the command line
hseigel
parents: 40261
diff changeset
   598
        }
f83e9aebbab4 8165634: Support multiple --add-modules options on the command line
hseigel
parents: 40261
diff changeset
   599
    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   600
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   601
    /**
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 38457
diff changeset
   602
     * Process the --add-reads options to add any additional read edges that
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   603
     * are specified on the command-line.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   604
     */
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44364
diff changeset
   605
    private static void addExtraReads(ModuleLayer bootLayer) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   606
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   607
        // decode the command line options
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   608
        Map<String, List<String>> map = decode("jdk.module.addreads.");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   609
        if (map.isEmpty())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   610
            return;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   611
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   612
        for (Map.Entry<String, List<String>> e : map.entrySet()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   613
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   614
            // the key is $MODULE
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   615
            String mn = e.getKey();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   616
            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
   617
            if (!om.isPresent()) {
42774
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   618
                warnUnknownModule(ADD_READS, mn);
41414
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   619
                continue;
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   620
            }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   621
            Module m = om.get();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   622
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   623
            // the value is the set of other modules (by name)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   624
            for (String name : e.getValue()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   625
                if (ALL_UNNAMED.equals(name)) {
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   626
                    Modules.addReadsAllUnnamed(m);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   627
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   628
                    om = bootLayer.findModule(name);
41414
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   629
                    if (om.isPresent()) {
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   630
                        Modules.addReads(m, om.get());
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   631
                    } else {
42774
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   632
                        warnUnknownModule(ADD_READS, name);
41414
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   633
                    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   634
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   635
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   636
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   637
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   638
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   639
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   640
     * Process the --add-exports and --add-opens options to export/open
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   641
     * additional packages specified on the command-line.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   642
     */
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   643
    private static boolean addExtraExportsAndOpens(ModuleLayer bootLayer) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   644
        boolean extraExportsOrOpens = false;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   645
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   646
        // --add-exports
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   647
        String prefix = "jdk.module.addexports.";
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   648
        Map<String, List<String>> extraExports = decode(prefix);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   649
        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
   650
            addExtraExportsOrOpens(bootLayer, extraExports, false);
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   651
            extraExportsOrOpens = true;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   652
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   653
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   654
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   655
        // --add-opens
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   656
        prefix = "jdk.module.addopens.";
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   657
        Map<String, List<String>> extraOpens = decode(prefix);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   658
        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
   659
            addExtraExportsOrOpens(bootLayer, extraOpens, true);
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   660
            extraExportsOrOpens = true;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   661
        }
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42774
diff changeset
   662
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   663
        return extraExportsOrOpens;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   664
    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   665
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44364
diff changeset
   666
    private static void addExtraExportsOrOpens(ModuleLayer bootLayer,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   667
                                               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
   668
                                               boolean opens)
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   669
    {
42774
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   670
        String option = opens ? ADD_OPENS : ADD_EXPORTS;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   671
        for (Map.Entry<String, List<String>> e : map.entrySet()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   672
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   673
            // the key is $MODULE/$PACKAGE
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   674
            String key = e.getKey();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   675
            String[] s = key.split("/");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   676
            if (s.length != 2)
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43813
diff changeset
   677
                fail(unableToParse(option, "<module>/<package>", key));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   678
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   679
            String mn = s[0];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   680
            String pn = s[1];
41414
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   681
            if (mn.isEmpty() || pn.isEmpty())
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
            // The exporting module is in the boot layer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   685
            Module m;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   686
            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
   687
            if (!om.isPresent()) {
42774
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   688
                warnUnknownModule(option, mn);
41414
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   689
                continue;
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   690
            }
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   691
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   692
            m = om.get();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   693
41414
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   694
            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
   695
                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
   696
                continue;
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   697
            }
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   698
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   699
            // the value is the set of modules to export to (by name)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   700
            for (String name : e.getValue()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   701
                boolean allUnnamed = false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   702
                Module other = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   703
                if (ALL_UNNAMED.equals(name)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   704
                    allUnnamed = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   705
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   706
                    om = bootLayer.findModule(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   707
                    if (om.isPresent()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   708
                        other = om.get();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   709
                    } else {
42774
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   710
                        warnUnknownModule(option, name);
41414
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   711
                        continue;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   712
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   713
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   714
                if (allUnnamed) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   715
                    if (opens) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   716
                        Modules.addOpensToAllUnnamed(m, pn);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   717
                    } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   718
                        Modules.addExportsToAllUnnamed(m, pn);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   719
                    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   720
                } else {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   721
                    if (opens) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   722
                        Modules.addOpens(m, pn, other);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   723
                    } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   724
                        Modules.addExports(m, pn, other);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   725
                    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   726
                }
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   727
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   728
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   729
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   730
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   731
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   732
    /**
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   733
     * Process the --illegal-access option (and its default) to open packages
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   734
     * 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
   735
     */
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   736
    private static void addIllegalAccess(ModuleFinder upgradeModulePath,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   737
                                         SystemModules systemModules,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   738
                                         ModuleLayer bootLayer,
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   739
                                         boolean extraExportsOrOpens) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   740
        String value = getAndRemoveProperty("jdk.module.illegalAccess");
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   741
        IllegalAccessLogger.Mode mode = IllegalAccessLogger.Mode.ONESHOT;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   742
        if (value != null) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   743
            switch (value) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   744
                case "deny":
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   745
                    return;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   746
                case "permit":
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   747
                    break;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   748
                case "warn":
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   749
                    mode = IllegalAccessLogger.Mode.WARN;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   750
                    break;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   751
                case "debug":
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   752
                    mode = IllegalAccessLogger.Mode.DEBUG;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   753
                    break;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   754
                default:
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   755
                    fail("Value specified to --illegal-access not recognized:"
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   756
                            + " '" + value + "'");
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   757
                    return;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   758
            }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   759
        }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   760
        IllegalAccessLogger.Builder builder
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   761
            = new IllegalAccessLogger.Builder(mode, System.err);
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   762
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   763
        Map<String, Set<String>> map1 = systemModules.concealedPackagesToOpen();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   764
        Map<String, Set<String>> map2 = systemModules.exportedPackagesToOpen();
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   765
        if (map1.isEmpty() && map2.isEmpty()) {
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   766
            // need to generate (exploded build)
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   767
            IllegalAccessMaps maps = IllegalAccessMaps.generate(limitedFinder());
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   768
            map1 = maps.concealedPackagesToOpen();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   769
            map2 = maps.exportedPackagesToOpen();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   770
        }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   771
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   772
        // open specific packages in the system modules
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   773
        for (Module m : bootLayer.modules()) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   774
            ModuleDescriptor descriptor = m.getDescriptor();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   775
            String name = m.getName();
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
            // skip open modules
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   778
            if (descriptor.isOpen()) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   779
                continue;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   780
            }
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 modules loaded from the upgrade module path
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   783
            if (upgradeModulePath != null
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   784
                && upgradeModulePath.find(name).isPresent()) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   785
                continue;
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
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   788
            Set<String> concealedPackages = map1.getOrDefault(name, Set.of());
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   789
            Set<String> exportedPackages = map2.getOrDefault(name, Set.of());
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   790
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   791
            // refresh the set of concealed and exported packages if needed
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   792
            if (extraExportsOrOpens) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   793
                concealedPackages = new HashSet<>(concealedPackages);
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   794
                exportedPackages = new HashSet<>(exportedPackages);
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   795
                Iterator<String> iterator = concealedPackages.iterator();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   796
                while (iterator.hasNext()) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   797
                    String pn = iterator.next();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   798
                    if (m.isExported(pn, BootLoader.getUnnamedModule())) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   799
                        // concealed package is exported to ALL-UNNAMED
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   800
                        iterator.remove();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   801
                        exportedPackages.add(pn);
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   802
                    }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   803
                }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   804
                iterator = exportedPackages.iterator();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   805
                while (iterator.hasNext()) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   806
                    String pn = iterator.next();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   807
                    if (m.isOpen(pn, BootLoader.getUnnamedModule())) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   808
                        // exported package is opened to ALL-UNNAMED
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   809
                        iterator.remove();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   810
                    }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   811
                }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   812
            }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   813
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   814
            // log reflective access to all types in concealed packages
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   815
            builder.logAccessToConcealedPackages(m, concealedPackages);
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
            // log reflective access to non-public members/types in exported packages
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   818
            builder.logAccessToExportedPackages(m, exportedPackages);
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   819
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   820
            // open the packages to unnamed modules
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   821
            JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   822
            jla.addOpensToAllUnnamed(m, concat(concealedPackages.iterator(),
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   823
                                               exportedPackages.iterator()));
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
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   826
        builder.complete();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   827
    }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   828
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   829
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   830
     * Decodes the values of --add-reads, -add-exports, --add-opens or
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   831
     * --patch-modules options that are encoded in system properties.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   832
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   833
     * @param prefix the system property prefix
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   834
     * @praam regex the regex for splitting the RHS of the option value
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   835
     */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   836
    private static Map<String, List<String>> decode(String prefix,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   837
                                                    String regex,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   838
                                                    boolean allowDuplicates) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   839
        int index = 0;
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 38457
diff changeset
   840
        // the system property is removed after decoding
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 38457
diff changeset
   841
        String value = getAndRemoveProperty(prefix + index);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   842
        if (value == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   843
            return Collections.emptyMap();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   844
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   845
        Map<String, List<String>> map = new HashMap<>();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   846
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   847
        while (value != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   848
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   849
            int pos = value.indexOf('=');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   850
            if (pos == -1)
42774
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   851
                fail(unableToParse(option(prefix), "<module>=<value>", value));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   852
            if (pos == 0)
42774
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   853
                fail(unableToParse(option(prefix), "<module>=<value>", value));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   854
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   855
            // key is <module> or <module>/<package>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   856
            String key = value.substring(0, pos);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   857
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   858
            String rhs = value.substring(pos+1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   859
            if (rhs.isEmpty())
42774
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   860
                fail(unableToParse(option(prefix), "<module>=<value>", value));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   861
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   862
            // value is <module>(,<module>)* or <file>(<pathsep><file>)*
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   863
            if (!allowDuplicates && map.containsKey(key))
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43813
diff changeset
   864
                fail(key + " specified more than once to " + option(prefix));
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   865
            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
   866
            int ntargets = 0;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   867
            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
   868
                if (s.length() > 0) {
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   869
                    values.add(s);
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   870
                    ntargets++;
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   871
                }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   872
            }
42774
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   873
            if (ntargets == 0)
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   874
                fail("Target must be specified: " + option(prefix) + " " + value);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   875
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   876
            index++;
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 38457
diff changeset
   877
            value = getAndRemoveProperty(prefix + index);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   878
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   879
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   880
        return map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   881
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   882
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 38457
diff changeset
   883
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   884
     * Decodes the values of --add-reads, -add-exports or --add-opens
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   885
     * which use the "," to separate the RHS of the option value.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   886
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   887
    private static Map<String, List<String>> decode(String prefix) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   888
        return decode(prefix, ",", true);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   889
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   890
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41414
diff changeset
   891
    /**
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 38457
diff changeset
   892
     * Gets and remove the named system property
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 38457
diff changeset
   893
     */
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 38457
diff changeset
   894
    private static String getAndRemoveProperty(String key) {
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 38457
diff changeset
   895
        return (String)System.getProperties().remove(key);
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 38457
diff changeset
   896
    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   897
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   898
    /**
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   899
     * Checks incubating status of modules in the configuration
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   900
     */
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   901
    private static void checkIncubatingStatus(Configuration cf) {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   902
        String incubating = null;
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   903
        for (ResolvedModule resolvedModule : cf.modules()) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   904
            ModuleReference mref = resolvedModule.reference();
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   905
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   906
            // emit warning if the WARN_INCUBATING module resolution bit set
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   907
            if (ModuleResolution.hasIncubatingWarning(mref)) {
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   908
                String mn = mref.descriptor().name();
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   909
                if (incubating == null) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   910
                    incubating = mn;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   911
                } else {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   912
                    incubating += ", " + mn;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   913
                }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   914
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   915
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   916
        if (incubating != null)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   917
            warn("Using incubator modules: " + incubating);
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
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   921
     * Throws a RuntimeException with the given message
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   922
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   923
    static void fail(String m) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   924
        throw new RuntimeException(m);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   925
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   926
41414
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   927
    static void warn(String m) {
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   928
        System.err.println("WARNING: " + m);
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   929
    }
7fd4548e9733 8162401: Support multiple --add-exports and --add-reads with the same module/package
mchung
parents: 41114
diff changeset
   930
42774
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   931
    static void warnUnknownModule(String option, String mn) {
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43813
diff changeset
   932
        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
   933
    }
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   934
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   935
    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
   936
        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
   937
    }
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
    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
   940
    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
   941
    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
   942
    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
   943
    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
   944
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   945
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   946
    /*
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   947
     * 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
   948
     * system property prefix.
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
    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
   951
        switch (prefix) {
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   952
            case "jdk.module.addexports.":
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   953
                return ADD_EXPORTS;
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   954
            case "jdk.module.addopens.":
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   955
                return ADD_OPENS;
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   956
            case "jdk.module.addreads.":
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   957
                return ADD_READS;
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   958
            case "jdk.module.patch.":
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   959
                return PATCH_MODULE;
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   960
            case "jdk.module.addmods.":
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   961
                return ADD_MODULES;
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   962
            default:
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   963
                throw new IllegalArgumentException(prefix);
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   964
        }
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   965
    }
74bcf37d15d8 8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents: 42770
diff changeset
   966
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   967
    /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   968
     * 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
   969
     * 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
   970
     */
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   971
    static <T> Iterator<T> concat(Iterator<T> iterator1, Iterator<T> iterator2) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   972
        return new Iterator<T>() {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   973
            @Override
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   974
            public boolean hasNext() {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   975
                return iterator1.hasNext() || iterator2.hasNext();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   976
            }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   977
            @Override
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   978
            public T next() {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   979
                if (iterator1.hasNext()) return iterator1.next();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   980
                if (iterator2.hasNext()) return iterator2.next();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   981
                throw new NoSuchElementException();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   982
            }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   983
        };
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   984
    }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   985
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   986
    /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   987
     * 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
   988
     * for use after startup.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   989
     */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   990
    static class SafeModuleFinder implements ModuleFinder {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   991
        private final Set<ModuleReference> mrefs;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   992
        private volatile Map<String, ModuleReference> nameToModule;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   993
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   994
        SafeModuleFinder(ModuleFinder finder) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   995
            this.mrefs = Collections.unmodifiableSet(finder.findAll());
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   996
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   997
        @Override
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   998
        public Optional<ModuleReference> find(String name) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   999
            Objects.requireNonNull(name);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1000
            Map<String, ModuleReference> nameToModule = this.nameToModule;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1001
            if (nameToModule == null) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1002
                this.nameToModule = nameToModule = mrefs.stream()
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1003
                        .collect(Collectors.toMap(m -> m.descriptor().name(),
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1004
                                                  Function.identity()));
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1005
            }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1006
            return Optional.ofNullable(nameToModule.get(name));
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1007
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1008
        @Override
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1009
        public Set<ModuleReference> findAll() {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1010
            return mrefs;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1011
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1012
    }
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1013
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1014
    /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1015
     * Counters for startup performance analysis.
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
    static class Counters {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1018
        private static final boolean PUBLISH_COUNTERS;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1019
        private static final boolean PRINT_COUNTERS;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1020
        private static Map<String, Long> counters;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1021
        static {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1022
            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
  1023
            if (s == null) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1024
                PUBLISH_COUNTERS = false;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1025
                PRINT_COUNTERS = false;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1026
            } else {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1027
                PUBLISH_COUNTERS = true;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1028
                PRINT_COUNTERS = s.equals("debug");
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1029
                counters = new LinkedHashMap<>();  // preserve insert order
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1030
            }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1031
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1032
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1033
        /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1034
         * Add a counter
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
        static void add(String name, long start) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1037
            if (PUBLISH_COUNTERS || PRINT_COUNTERS) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1038
                counters.put(name, (System.nanoTime() - start));
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1039
            }
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
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1042
        /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1043
         * 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
  1044
         */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1045
        static void publish() {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1046
            if (PUBLISH_COUNTERS || PRINT_COUNTERS) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1047
                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
  1048
                    String name = e.getKey();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1049
                    long value = e.getValue();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1050
                    if (PUBLISH_COUNTERS)
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1051
                        PerfCounter.newPerfCounter(name).set(value);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1052
                    if (PRINT_COUNTERS)
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1053
                        System.out.println(name + " = " + value);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1054
                }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1055
            }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1056
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1057
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1058
}