src/java.base/share/classes/jdk/internal/module/Modules.java
author alanb
Tue, 19 Nov 2019 11:35:29 +0000
changeset 59133 580fb715b29d
parent 52427 3c6aa484536c
permissions -rw-r--r--
8233922: Service binding augments module graph with observable incubator modules Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
59133
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 52427
diff changeset
     2
 * Copyright (c) 2015, 2019, 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
59133
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 52427
diff changeset
    28
import java.io.PrintStream;
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
    29
import java.lang.module.Configuration;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.lang.module.ModuleDescriptor;
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
    31
import java.lang.module.ModuleFinder;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
    32
import java.lang.module.ModuleReference;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
    33
import java.lang.module.ResolvedModule;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.net.URI;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
    35
import java.security.AccessController;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
    36
import java.security.PrivilegedAction;
59133
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 52427
diff changeset
    37
import java.util.Collection;
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
    38
import java.util.List;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
    39
import java.util.Map;
50744
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents: 47866
diff changeset
    40
import java.util.Optional;
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
    41
import java.util.Set;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
    42
import java.util.function.Function;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
    43
import java.util.stream.Collectors;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
59133
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 52427
diff changeset
    45
import jdk.internal.access.JavaLangModuleAccess;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import jdk.internal.loader.BootLoader;
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
    47
import jdk.internal.loader.BuiltinClassLoader;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import jdk.internal.loader.ClassLoaders;
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 50744
diff changeset
    49
import jdk.internal.access.JavaLangAccess;
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 50744
diff changeset
    50
import jdk.internal.access.SharedSecrets;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
/**
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
    53
 * A helper class for creating and updating modules. This class is intended to
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
    54
 * support command-line options, tests, and the instrumentation API. It is also
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
    55
 * used by the VM to load modules or add read edges when agents are instrumenting
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
    56
 * code that need to link to supporting classes.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
 * The parameters that are package names in this API are the fully-qualified
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
 * names of the packages as defined in section 6.5.3 of <cite>The Java&trade;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
 * Language Specification </cite>, for example, {@code "java.lang"}.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
public class Modules {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
    private Modules() { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
    66
    private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
59133
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 52427
diff changeset
    67
    private static final JavaLangModuleAccess JLMA = SharedSecrets.getJavaLangModuleAccess();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
     * Creates a new Module. The module has the given ModuleDescriptor and
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
     * is defined to the given class loader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
     *
47866
39db80b32b69 8191632: Typos in comments due to duplicating words
igerasim
parents: 47216
diff changeset
    73
     * The resulting Module is in a larval state in that it does not read
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
     * any other module and does not have any exports.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
     * The URI is for information purposes only.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
    public static Module defineModule(ClassLoader loader,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
                                      ModuleDescriptor descriptor,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
                                      URI uri)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
    {
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
    82
        return JLA.defineModule(loader, descriptor, uri);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
    /**
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
    86
     * Updates m1 to read m2.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
     * Same as m1.addReads(m2) but without a caller check.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
    public static void addReads(Module m1, Module m2) {
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
    90
        JLA.addReads(m1, m2);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
    /**
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
    94
     * Update module m to read all unnamed modules.
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    95
     */
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    96
    public static void addReadsAllUnnamed(Module m) {
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
    97
        JLA.addReadsAllUnnamed(m);
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
    98
    }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
    99
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   100
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
     * Updates module m1 to export a package to module m2.
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   102
     * Same as m1.addExports(pn, m2) but without a caller check
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
    public static void addExports(Module m1, String pn, Module m2) {
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   105
        JLA.addExports(m1, pn, m2);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
    /**
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   109
     * Updates module m to export a package to all unnamed modules.
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   110
     */
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   111
    public static void addExportsToAllUnnamed(Module m, String pn) {
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   112
        JLA.addExportsToAllUnnamed(m, pn);
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   113
    }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   114
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   115
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   116
     * Updates module m1 to open a package to module m2.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   117
     * Same as m1.addOpens(pn, m2) but without a caller check.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   118
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   119
    public static void addOpens(Module m1, String pn, Module m2) {
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   120
        JLA.addOpens(m1, pn, m2);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   121
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   122
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   123
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   124
     * Updates module m to open a package to all unnamed modules.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   125
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   126
    public static void addOpensToAllUnnamed(Module m, String pn) {
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   127
        JLA.addOpensToAllUnnamed(m, pn);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   128
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   129
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   130
    /**
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   131
     * Updates module m to use a service.
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   132
     * Same as m2.addUses(service) but without a caller check.
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   133
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   134
    public static void addUses(Module m, Class<?> service) {
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   135
        JLA.addUses(m, service);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   136
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   137
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   138
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   139
     * Updates module m to provide a service
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   140
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   141
    public static void addProvides(Module m, Class<?> service, Class<?> impl) {
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   142
        ModuleLayer layer = m.getLayer();
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   143
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   144
        PrivilegedAction<ClassLoader> pa = m::getClassLoader;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   145
        ClassLoader loader = AccessController.doPrivileged(pa);
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   146
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   147
        ClassLoader platformClassLoader = ClassLoaders.platformClassLoader();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   148
        if (layer == null || loader == null || loader == platformClassLoader) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   149
            // update ClassLoader catalog
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   150
            ServicesCatalog catalog;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   151
            if (loader == null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   152
                catalog = BootLoader.getServicesCatalog();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   153
            } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   154
                catalog = ServicesCatalog.getServicesCatalog(loader);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   155
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   156
            catalog.addProvider(m, service, impl);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   157
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   158
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   159
        if (layer != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   160
            // update Layer catalog
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   161
            JLA.getServicesCatalog(layer).addProvider(m, service, impl);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   162
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   163
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   164
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   165
    /**
59133
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 52427
diff changeset
   166
     * Resolves a collection of root modules, with service binding and the empty
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 52427
diff changeset
   167
     * Configuration as the parent to create a Configuration for the boot layer.
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 52427
diff changeset
   168
     *
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 52427
diff changeset
   169
     * This method is intended to be used to create the Configuration for the
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 52427
diff changeset
   170
     * boot layer during startup or at a link-time.
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 52427
diff changeset
   171
     */
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 52427
diff changeset
   172
    public static Configuration newBootLayerConfiguration(ModuleFinder finder,
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 52427
diff changeset
   173
                                                          Collection<String> roots,
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 52427
diff changeset
   174
                                                          PrintStream traceOutput)
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 52427
diff changeset
   175
    {
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 52427
diff changeset
   176
        return JLMA.resolveAndBind(finder, roots, traceOutput);
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 52427
diff changeset
   177
    }
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 52427
diff changeset
   178
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 52427
diff changeset
   179
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
     * Called by the VM when code in the given Module has been transformed by
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
     * an agent and so may have been instrumented to call into supporting
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
     * classes on the boot class path or application class path.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
    public static void transformedByAgent(Module m) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
        addReads(m, BootLoader.getUnnamedModule());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
        addReads(m, ClassLoaders.appClassLoader().getUnnamedModule());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
    }
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   188
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   189
    /**
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   190
     * Called by the VM to load a system module, typically "java.instrument" or
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   191
     * "jdk.management.agent". If the module is not loaded then it is resolved
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   192
     * and loaded (along with any dependences that weren't previously loaded)
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   193
     * into a child layer.
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   194
     */
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   195
    public static synchronized Module loadModule(String name) {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   196
        ModuleLayer top = topLayer;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   197
        if (top == null)
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   198
            top = ModuleLayer.boot();
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   199
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   200
        Module module = top.findModule(name).orElse(null);
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   201
        if (module != null) {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   202
            // module already loaded
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   203
            return module;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   204
        }
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   205
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   206
        // resolve the module with the top-most layer as the parent
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   207
        ModuleFinder empty = ModuleFinder.of();
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   208
        ModuleFinder finder = ModuleBootstrap.unlimitedFinder();
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   209
        Set<String> roots = Set.of(name);
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   210
        Configuration cf = top.configuration().resolveAndBind(empty, finder, roots);
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   211
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   212
        // create the child layer
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   213
        Function<String, ClassLoader> clf = ModuleLoaderMap.mappingFunction(cf);
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   214
        ModuleLayer newLayer = top.defineModules(cf, clf);
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   215
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   216
        // add qualified exports/opens to give access to modules in child layer
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   217
        Map<String, Module> map = newLayer.modules().stream()
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   218
                                          .collect(Collectors.toMap(Module::getName,
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   219
                                                  Function.identity()));
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   220
        ModuleLayer layer = top;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   221
        while (layer != null) {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   222
            for (Module m : layer.modules()) {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   223
                // qualified exports
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   224
                m.getDescriptor().exports().stream()
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   225
                    .filter(ModuleDescriptor.Exports::isQualified)
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   226
                    .forEach(e -> e.targets().forEach(target -> {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   227
                        Module other = map.get(target);
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   228
                        if (other != null) {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   229
                            addExports(m, e.source(), other);
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   230
                        }}));
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   231
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   232
                // qualified opens
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   233
                m.getDescriptor().opens().stream()
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   234
                    .filter(ModuleDescriptor.Opens::isQualified)
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   235
                    .forEach(o -> o.targets().forEach(target -> {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   236
                        Module other = map.get(target);
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   237
                        if (other != null) {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   238
                            addOpens(m, o.source(), other);
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   239
                        }}));
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   240
            }
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   241
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   242
            List<ModuleLayer> parents = layer.parents();
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   243
            assert parents.size() <= 1;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   244
            layer = parents.isEmpty() ? null : parents.get(0);
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   245
        }
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   246
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   247
        // update security manager before making types visible
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   248
        JLA.addNonExportedPackages(newLayer);
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   249
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   250
        // update the built-in class loaders to make the types visible
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   251
        for (ResolvedModule resolvedModule : cf.modules()) {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   252
            ModuleReference mref = resolvedModule.reference();
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   253
            String mn = mref.descriptor().name();
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   254
            ClassLoader cl = clf.apply(mn);
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   255
            if (cl == null) {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   256
                BootLoader.loadModule(mref);
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   257
            } else {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   258
                ((BuiltinClassLoader) cl).loadModule(mref);
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   259
            }
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   260
        }
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   261
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   262
        // new top layer
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   263
        topLayer = newLayer;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   264
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   265
        // return module
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   266
        return newLayer.findModule(name)
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   267
                       .orElseThrow(() -> new InternalError("module not loaded"));
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   268
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   269
    }
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   270
50744
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents: 47866
diff changeset
   271
    /**
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents: 47866
diff changeset
   272
     * Finds the module with the given name in the boot layer or any child
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents: 47866
diff changeset
   273
     * layers created to load the "java.instrument" or "jdk.management.agent"
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents: 47866
diff changeset
   274
     * modules into a running VM.
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents: 47866
diff changeset
   275
     */
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents: 47866
diff changeset
   276
    public static Optional<Module> findLoadedModule(String name) {
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents: 47866
diff changeset
   277
        ModuleLayer top = topLayer;
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents: 47866
diff changeset
   278
        if (top == null)
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents: 47866
diff changeset
   279
            top = ModuleLayer.boot();
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents: 47866
diff changeset
   280
        return top.findModule(name);
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents: 47866
diff changeset
   281
    }
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents: 47866
diff changeset
   282
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents: 47866
diff changeset
   283
    // the top-most layer
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents: 47866
diff changeset
   284
    private static volatile ModuleLayer topLayer;
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   285
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   286
}