jdk/src/java.base/share/classes/jdk/internal/module/Modules.java
author alanb
Fri, 07 Apr 2017 08:05:54 +0000
changeset 44545 83b611b88ac8
parent 44359 c6761862ca0b
child 45004 ea3137042a61
permissions -rw-r--r--
8177530: Module system implementation refresh (4/2017) Reviewed-by: mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
     2
 * Copyright (c) 2015, 2016, 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.lang.module.ModuleDescriptor;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.net.URI;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
    30
import java.security.AccessController;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
    31
import java.security.PrivilegedAction;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import jdk.internal.loader.BootLoader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import jdk.internal.loader.ClassLoaders;
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
    35
import jdk.internal.misc.JavaLangAccess;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import jdk.internal.misc.SharedSecrets;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
/**
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
    39
 * 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
    40
 * support command-line options, tests, and the instrumentation API. It is also
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
    41
 * used by the VM to add read edges when agents are instrumenting code that
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
    42
 * need to link to supporting classes.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
 * The parameters that are package names in this API are the fully-qualified
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
 * 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
    46
 * Language Specification </cite>, for example, {@code "java.lang"}.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
public class Modules {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
    private Modules() { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
    52
    private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
     * Creates a new Module. The module has the given ModuleDescriptor and
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
     * is defined to the given class loader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
     *
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    58
     * The resulting Module is in a larval state in that it does not not read
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
     * any other module and does not have any exports.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
     * The URI is for information purposes only.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
    public static Module defineModule(ClassLoader loader,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
                                      ModuleDescriptor descriptor,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
                                      URI uri)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
    {
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
    67
        return JLA.defineModule(loader, descriptor, uri);
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
    /**
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
    71
     * Updates m1 to read m2.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
     * Same as m1.addReads(m2) but without a caller check.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
    public static void addReads(Module m1, Module m2) {
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
    75
        JLA.addReads(m1, m2);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
    /**
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
    79
     * Update module m to read all unnamed modules.
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    80
     */
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    81
    public static void addReadsAllUnnamed(Module m) {
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
    82
        JLA.addReadsAllUnnamed(m);
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
    83
    }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
    84
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
    85
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
     * Updates module m1 to export a package to module m2.
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
    87
     * Same as m1.addExports(pn, m2) but without a caller check
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
    public static void addExports(Module m1, String pn, Module m2) {
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
    90
        JLA.addExports(m1, pn, 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
     * Updates module m to export a package to all unnamed modules.
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
    public static void addExportsToAllUnnamed(Module m, String pn) {
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
    97
        JLA.addExportsToAllUnnamed(m, pn);
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
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   101
     * Updates module m1 to open a package to module m2.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   102
     * Same as m1.addOpens(pn, m2) but without a caller check.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   103
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   104
    public static void addOpens(Module m1, String pn, Module m2) {
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   105
        JLA.addOpens(m1, pn, m2);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   106
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   107
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   108
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   109
     * Updates module m to open a package to all unnamed modules.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   110
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   111
    public static void addOpensToAllUnnamed(Module m, String pn) {
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   112
        JLA.addOpensToAllUnnamed(m, pn);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   113
    }
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
    /**
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   116
     * Updates module m to use a service.
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   117
     * Same as m2.addUses(service) but without a caller check.
42338
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 addUses(Module m, Class<?> service) {
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   120
        JLA.addUses(m, service);
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 provide a service
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 addProvides(Module m, Class<?> service, Class<?> impl) {
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   127
        ModuleLayer layer = m.getLayer();
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   128
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   129
        if (layer == null || layer == ModuleLayer.boot()) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   130
            // update ClassLoader catalog
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   131
            PrivilegedAction<ClassLoader> pa = m::getClassLoader;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   132
            ClassLoader loader = AccessController.doPrivileged(pa);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   133
            ServicesCatalog catalog;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   134
            if (loader == null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   135
                catalog = BootLoader.getServicesCatalog();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   136
            } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   137
                catalog = ServicesCatalog.getServicesCatalog(loader);
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
            catalog.addProvider(m, service, impl);
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
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   142
        if (layer != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   143
            // update Layer catalog
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   144
            JLA.getServicesCatalog(layer).addProvider(m, service, impl);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   145
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   146
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   147
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   148
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
     * Called by the VM when code in the given Module has been transformed by
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
     * an agent and so may have been instrumented to call into supporting
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
     * classes on the boot class path or application class path.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
    public static void transformedByAgent(Module m) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
        addReads(m, BootLoader.getUnnamedModule());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
        addReads(m, ClassLoaders.appClassLoader().getUnnamedModule());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
}