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