src/java.base/share/classes/jdk/internal/loader/BuiltinClassLoader.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 57792 1b6806340400
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
53660
929f0c7e019b 8218419: Can't get annotations that are present on packages in -Xbootclasspath/a
mchung
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.loader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.io.IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.io.InputStream;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
    30
import java.lang.module.ModuleDescriptor;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.lang.module.ModuleReference;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.lang.module.ModuleReader;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
    33
import java.lang.ref.SoftReference;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.net.MalformedURLException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.net.URI;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.net.URL;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.nio.ByteBuffer;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.security.AccessController;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.security.CodeSigner;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.security.CodeSource;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.security.PermissionCollection;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.security.PrivilegedAction;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.security.PrivilegedActionException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.security.PrivilegedExceptionAction;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import java.security.SecureClassLoader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import java.util.ArrayList;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import java.util.Enumeration;
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
    49
import java.util.Iterator;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
import java.util.Map;
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
    52
import java.util.NoSuchElementException;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
import java.util.Optional;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
import java.util.concurrent.ConcurrentHashMap;
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
    55
import java.util.function.Function;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
import java.util.jar.Attributes;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
import java.util.jar.Manifest;
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 37779
diff changeset
    58
import java.util.stream.Stream;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 52159
diff changeset
    60
import jdk.internal.access.SharedSecrets;
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
    61
import jdk.internal.misc.VM;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
import jdk.internal.module.ModulePatcher.PatchedModuleReader;
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44003
diff changeset
    63
import jdk.internal.module.Resources;
54471
f22759e92191 8222144: BuiltinClassLoader should create the CodeSource for jrt URLs lazily
redestad
parents: 53660
diff changeset
    64
import jdk.internal.vm.annotation.Stable;
57792
1b6806340400 8229773: Resolve permissions for code source URLs lazily
redestad
parents: 57714
diff changeset
    65
import sun.security.util.LazyCodeSourcePermissionCollection;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
 * The platform or application class loader. Resources loaded from modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
 * defined to the boot class loader are also loaded via an instance of this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
 * ClassLoader type.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
 * <p> This ClassLoader supports loading of classes and resources from modules.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
 * Modules are defined to the ClassLoader by invoking the {@link #loadModule}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
 * method. Defining a module to this ClassLoader has the effect of making the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
 * types in the module visible. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
 * <p> This ClassLoader also supports loading of classes and resources from a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
 * class path of URLs that are specified to the ClassLoader at construction
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
 * time. The class path may expand at runtime (the Class-Path attribute in JAR
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
 * files or via instrumentation agents). </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
 * <p> The delegation model used by this ClassLoader differs to the regular
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
 * delegation model. When requested to load a class then this ClassLoader first
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
 * maps the class name to its package name. If there is a module defined to a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
 * BuiltinClassLoader containing this package then the class loader delegates
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
 * directly to that class loader. If there isn't a module containing the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
 * package then it delegates the search to the parent class loader and if not
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
 * found in the parent then it searches the class path. The main difference
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
 * between this and the usual delegation model is that it allows the platform
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
 * class loader to delegate to the application class loader, important with
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
 * upgraded modules defined to the platform class loader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
public class BuiltinClassLoader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
    extends SecureClassLoader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
{
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
    static {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
        if (!ClassLoader.registerAsParallelCapable())
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   100
            throw new InternalError("Unable to register as parallel capable");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
    // parent ClassLoader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
    private final BuiltinClassLoader parent;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
48995
58bec53828ba 8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents: 47216
diff changeset
   106
    // the URL class path, or null if there is no class path
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
    private final URLClassPath ucp;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
     * A module defined/loaded by a built-in class loader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
     * A LoadedModule encapsulates a ModuleReference along with its CodeSource
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36674
diff changeset
   114
     * URL to avoid needing to create this URL when defining classes.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
    private static class LoadedModule {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
        private final BuiltinClassLoader loader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
        private final ModuleReference mref;
54471
f22759e92191 8222144: BuiltinClassLoader should create the CodeSource for jrt URLs lazily
redestad
parents: 53660
diff changeset
   119
        private final URI uri;                      // may be null
f22759e92191 8222144: BuiltinClassLoader should create the CodeSource for jrt URLs lazily
redestad
parents: 53660
diff changeset
   120
        private @Stable URL codeSourceURL;          // may be null
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
        LoadedModule(BuiltinClassLoader loader, ModuleReference mref) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
            URL url = null;
54471
f22759e92191 8222144: BuiltinClassLoader should create the CodeSource for jrt URLs lazily
redestad
parents: 53660
diff changeset
   124
            this.uri = mref.location().orElse(null);
f22759e92191 8222144: BuiltinClassLoader should create the CodeSource for jrt URLs lazily
redestad
parents: 53660
diff changeset
   125
f22759e92191 8222144: BuiltinClassLoader should create the CodeSource for jrt URLs lazily
redestad
parents: 53660
diff changeset
   126
            // for non-jrt schemes we need to resolve the codeSourceURL
f22759e92191 8222144: BuiltinClassLoader should create the CodeSource for jrt URLs lazily
redestad
parents: 53660
diff changeset
   127
            // eagerly during bootstrap since the handler might be
f22759e92191 8222144: BuiltinClassLoader should create the CodeSource for jrt URLs lazily
redestad
parents: 53660
diff changeset
   128
            // overridden
f22759e92191 8222144: BuiltinClassLoader should create the CodeSource for jrt URLs lazily
redestad
parents: 53660
diff changeset
   129
            if (uri != null && !"jrt".equals(uri.getScheme())) {
f22759e92191 8222144: BuiltinClassLoader should create the CodeSource for jrt URLs lazily
redestad
parents: 53660
diff changeset
   130
                url = createURL(uri);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
            this.loader = loader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
            this.mref = mref;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
            this.codeSourceURL = url;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
        BuiltinClassLoader loader() { return loader; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
        ModuleReference mref() { return mref; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
        String name() { return mref.descriptor().name(); }
54471
f22759e92191 8222144: BuiltinClassLoader should create the CodeSource for jrt URLs lazily
redestad
parents: 53660
diff changeset
   140
f22759e92191 8222144: BuiltinClassLoader should create the CodeSource for jrt URLs lazily
redestad
parents: 53660
diff changeset
   141
        URL codeSourceURL() {
f22759e92191 8222144: BuiltinClassLoader should create the CodeSource for jrt URLs lazily
redestad
parents: 53660
diff changeset
   142
            URL url = codeSourceURL;
f22759e92191 8222144: BuiltinClassLoader should create the CodeSource for jrt URLs lazily
redestad
parents: 53660
diff changeset
   143
            if (url == null && uri != null) {
f22759e92191 8222144: BuiltinClassLoader should create the CodeSource for jrt URLs lazily
redestad
parents: 53660
diff changeset
   144
                codeSourceURL = url = createURL(uri);
f22759e92191 8222144: BuiltinClassLoader should create the CodeSource for jrt URLs lazily
redestad
parents: 53660
diff changeset
   145
            }
f22759e92191 8222144: BuiltinClassLoader should create the CodeSource for jrt URLs lazily
redestad
parents: 53660
diff changeset
   146
            return url;
f22759e92191 8222144: BuiltinClassLoader should create the CodeSource for jrt URLs lazily
redestad
parents: 53660
diff changeset
   147
        }
f22759e92191 8222144: BuiltinClassLoader should create the CodeSource for jrt URLs lazily
redestad
parents: 53660
diff changeset
   148
f22759e92191 8222144: BuiltinClassLoader should create the CodeSource for jrt URLs lazily
redestad
parents: 53660
diff changeset
   149
        private URL createURL(URI uri) {
f22759e92191 8222144: BuiltinClassLoader should create the CodeSource for jrt URLs lazily
redestad
parents: 53660
diff changeset
   150
            URL url = null;
f22759e92191 8222144: BuiltinClassLoader should create the CodeSource for jrt URLs lazily
redestad
parents: 53660
diff changeset
   151
            try {
f22759e92191 8222144: BuiltinClassLoader should create the CodeSource for jrt URLs lazily
redestad
parents: 53660
diff changeset
   152
                url = uri.toURL();
f22759e92191 8222144: BuiltinClassLoader should create the CodeSource for jrt URLs lazily
redestad
parents: 53660
diff changeset
   153
            } catch (MalformedURLException | IllegalArgumentException e) {
f22759e92191 8222144: BuiltinClassLoader should create the CodeSource for jrt URLs lazily
redestad
parents: 53660
diff changeset
   154
            }
f22759e92191 8222144: BuiltinClassLoader should create the CodeSource for jrt URLs lazily
redestad
parents: 53660
diff changeset
   155
            return url;
f22759e92191 8222144: BuiltinClassLoader should create the CodeSource for jrt URLs lazily
redestad
parents: 53660
diff changeset
   156
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
    // maps package name to loaded module for modules in the boot layer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
    private static final Map<String, LoadedModule> packageToModule
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   162
        = new ConcurrentHashMap<>(1024);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
    // maps a module name to a module reference
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
    private final Map<String, ModuleReference> nameToModule;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
    // maps a module reference to a module reader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
    private final Map<ModuleReference, ModuleReader> moduleToReader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   170
    // cache of resource name -> list of URLs.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   171
    // used only for resources that are not in module packages
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   172
    private volatile SoftReference<Map<String, List<URL>>> resourceCache;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
     * Create a new instance.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
     */
41911
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41817
diff changeset
   177
    BuiltinClassLoader(String name, BuiltinClassLoader parent, URLClassPath ucp) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
        // ensure getParent() returns null when the parent is the boot loader
41911
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41817
diff changeset
   179
        super(name, parent == null || parent == ClassLoaders.bootLoader() ? null : parent);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
        this.parent = parent;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
        this.ucp = ucp;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
57714
9f44485e7441 8229407: Avoid ConcurrentHashMap resizes during bootstrap
redestad
parents: 54471
diff changeset
   184
        this.nameToModule = new ConcurrentHashMap<>(32);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
        this.moduleToReader = new ConcurrentHashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
    /**
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44003
diff changeset
   189
     * Returns {@code true} if there is a class path associated with this
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44003
diff changeset
   190
     * class loader.
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44003
diff changeset
   191
     */
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44003
diff changeset
   192
    boolean hasClassPath() {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44003
diff changeset
   193
        return ucp != null;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44003
diff changeset
   194
    }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44003
diff changeset
   195
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44003
diff changeset
   196
    /**
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44359
diff changeset
   197
     * Register a module this class loader. This has the effect of making the
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44359
diff changeset
   198
     * types in the module visible.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
    public void loadModule(ModuleReference mref) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
        String mn = mref.descriptor().name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
        if (nameToModule.putIfAbsent(mn, mref) != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
            throw new InternalError(mn + " already defined to this loader");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
        LoadedModule loadedModule = new LoadedModule(this, mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
        for (String pn : mref.descriptor().packages()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
            LoadedModule other = packageToModule.putIfAbsent(pn, loadedModule);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
            if (other != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
                throw new InternalError(pn + " in modules " + mn + " and "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
                                        + other.mref().descriptor().name());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   213
        }
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44359
diff changeset
   214
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44359
diff changeset
   215
        // clear resources cache if VM is already initialized
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44359
diff changeset
   216
        if (VM.isModuleSystemInited() && resourceCache != null) {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44359
diff changeset
   217
            resourceCache = null;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44359
diff changeset
   218
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
     * Returns the {@code ModuleReference} for the named module defined to
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
     * this class loader; or {@code null} if not defined.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
     * @param name The name of the module to find
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
    protected ModuleReference findModule(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
        return nameToModule.get(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
    // -- finding resources
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
     * Returns a URL to a resource of the given name in a module defined to
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
     * this class loader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
    public URL findResource(String mn, String name) throws IOException {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   240
        URL url = null;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   242
        if (mn != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   243
            // find in module
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   244
            ModuleReference mref = nameToModule.get(mn);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   245
            if (mref != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   246
                url = findResource(mref, name);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   247
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   248
        } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   249
            // find on class path
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   250
            url = findResourceOnClassPath(name);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   253
        return checkURL(url);  // check access before returning
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   254
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   255
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   256
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   257
     * Returns an input stream to a resource of the given name in a module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   258
     * defined to this class loader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   259
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   260
    public InputStream findResourceAsStream(String mn, String name)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   261
        throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   262
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
        // Need URL to resource when running with a security manager so that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   264
        // the right permission check is done.
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   265
        if (System.getSecurityManager() != null || mn == null) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
            URL url = findResource(mn, name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
            return (url != null) ? url.openStream() : null;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   268
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   270
        // find in module defined to this loader, no security manager
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   271
        ModuleReference mref = nameToModule.get(mn);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   272
        if (mref != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   273
            return moduleReaderFor(mref).open(name).orElse(null);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   274
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   275
            return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   276
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   277
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   278
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   279
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   280
     * Finds a resource with the given name in the modules defined to this
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   281
     * class loader or its class path.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   282
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   283
    @Override
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   284
    public URL findResource(String name) {
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44003
diff changeset
   285
        String pn = Resources.toPackageName(name);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   286
        LoadedModule module = packageToModule.get(pn);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   287
        if (module != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   288
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   289
            // resource is in a package of a module defined to this loader
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44003
diff changeset
   290
            if (module.loader() == this) {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44003
diff changeset
   291
                URL url;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   292
                try {
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44003
diff changeset
   293
                    url = findResource(module.name(), name); // checks URL
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   294
                } catch (IOException ioe) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   295
                    return null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   296
                }
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44003
diff changeset
   297
                if (url != null
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44003
diff changeset
   298
                    && (name.endsWith(".class")
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44003
diff changeset
   299
                        || url.toString().endsWith("/")
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44003
diff changeset
   300
                        || isOpen(module.mref(), pn))) {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44003
diff changeset
   301
                    return url;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44003
diff changeset
   302
                }
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   303
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   304
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   305
        } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   306
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   307
            // not in a module package but may be in module defined to this loader
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   308
            try {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   309
                List<URL> urls = findMiscResource(name);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   310
                if (!urls.isEmpty()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   311
                    URL url = urls.get(0);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   312
                    if (url != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   313
                        return checkURL(url); // check access before returning
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   314
                    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   315
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   316
            } catch (IOException ioe) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   317
                return null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   318
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   319
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   320
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   321
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   322
        // search class path
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   323
        URL url = findResourceOnClassPath(name);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   324
        return checkURL(url);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   325
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   326
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   327
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   328
     * Returns an enumeration of URL objects to all the resources with the
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   329
     * given name in modules defined to this class loader or on the class
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   330
     * path of this loader.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   331
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   332
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   333
    public Enumeration<URL> findResources(String name) throws IOException {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   334
        List<URL> checked = new ArrayList<>();  // list of checked URLs
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   335
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44003
diff changeset
   336
        String pn = Resources.toPackageName(name);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   337
        LoadedModule module = packageToModule.get(pn);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   338
        if (module != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   339
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   340
            // resource is in a package of a module defined to this loader
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44003
diff changeset
   341
            if (module.loader() == this) {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44003
diff changeset
   342
                URL url = findResource(module.name(), name); // checks URL
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44003
diff changeset
   343
                if (url != null
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44003
diff changeset
   344
                    && (name.endsWith(".class")
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44003
diff changeset
   345
                        || url.toString().endsWith("/")
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44003
diff changeset
   346
                        || isOpen(module.mref(), pn))) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   347
                    checked.add(url);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   348
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   349
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   350
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   351
        } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   352
            // not in a package of a module defined to this loader
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   353
            for (URL url : findMiscResource(name)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   354
                url = checkURL(url);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   355
                if (url != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   356
                    checked.add(url);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   357
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   358
            }
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   359
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   360
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   361
        // class path (not checked)
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   362
        Enumeration<URL> e = findResourcesOnClassPath(name);
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   363
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   364
        // concat the checked URLs and the (not checked) class path
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   365
        return new Enumeration<>() {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   366
            final Iterator<URL> iterator = checked.iterator();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   367
            URL next;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   368
            private boolean hasNext() {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   369
                if (next != null) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   370
                    return true;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   371
                } else if (iterator.hasNext()) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   372
                    next = iterator.next();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   373
                    return true;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   374
                } else {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   375
                    // need to check each URL
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   376
                    while (e.hasMoreElements() && next == null) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   377
                        next = checkURL(e.nextElement());
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   378
                    }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   379
                    return next != null;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   380
                }
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   381
            }
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   382
            @Override
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   383
            public boolean hasMoreElements() {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   384
                return hasNext();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   385
            }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   386
            @Override
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   387
            public URL nextElement() {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   388
                if (hasNext()) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   389
                    URL result = next;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   390
                    next = null;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   391
                    return result;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   392
                } else {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   393
                    throw new NoSuchElementException();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   394
                }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   395
            }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   396
        };
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   397
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   398
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   399
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   400
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   401
     * Returns the list of URLs to a "miscellaneous" resource in modules
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   402
     * defined to this loader. A miscellaneous resource is not in a module
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   403
     * package, e.g. META-INF/services/p.S.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   404
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   405
     * The cache used by this method avoids repeated searching of all modules.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   406
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   407
    private List<URL> findMiscResource(String name) throws IOException {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   408
        SoftReference<Map<String, List<URL>>> ref = this.resourceCache;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   409
        Map<String, List<URL>> map = (ref != null) ? ref.get() : null;
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44359
diff changeset
   410
        if (map == null) {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44359
diff changeset
   411
            map = new ConcurrentHashMap<>();
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44359
diff changeset
   412
            this.resourceCache = new SoftReference<>(map);
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44359
diff changeset
   413
        } else {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   414
            List<URL> urls = map.get(name);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   415
            if (urls != null)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   416
                return urls;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   417
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   418
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   419
        // search all modules for the resource
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   420
        List<URL> urls;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   421
        try {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   422
            urls = AccessController.doPrivileged(
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   423
                new PrivilegedExceptionAction<>() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   424
                    @Override
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   425
                    public List<URL> run() throws IOException {
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44003
diff changeset
   426
                        List<URL> result = null;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   427
                        for (ModuleReference mref : nameToModule.values()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   428
                            URI u = moduleReaderFor(mref).find(name).orElse(null);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   429
                            if (u != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   430
                                try {
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44003
diff changeset
   431
                                    if (result == null)
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44003
diff changeset
   432
                                        result = new ArrayList<>();
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   433
                                    result.add(u.toURL());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   434
                                } catch (MalformedURLException |
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   435
                                         IllegalArgumentException e) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   436
                                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   437
                            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   438
                        }
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44359
diff changeset
   439
                        return (result != null) ? result : Collections.emptyList();
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   440
                    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   441
                });
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   442
        } catch (PrivilegedActionException pae) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   443
            throw (IOException) pae.getCause();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   444
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   445
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44359
diff changeset
   446
        // only cache resources after VM is fully initialized
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   447
        if (VM.isModuleSystemInited()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   448
            map.putIfAbsent(name, urls);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   449
        }
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44359
diff changeset
   450
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   451
        return urls;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   452
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   453
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   454
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   455
     * Returns the URL to a resource in a module or {@code null} if not found.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   456
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   457
    private URL findResource(ModuleReference mref, String name) throws IOException {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   458
        URI u;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   459
        if (System.getSecurityManager() == null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   460
            u = moduleReaderFor(mref).find(name).orElse(null);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   461
        } else {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   462
            try {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   463
                u = AccessController.doPrivileged(new PrivilegedExceptionAction<> () {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   464
                    @Override
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   465
                    public URI run() throws IOException {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   466
                        return moduleReaderFor(mref).find(name).orElse(null);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   467
                    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   468
                });
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   469
            } catch (PrivilegedActionException pae) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   470
                throw (IOException) pae.getCause();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   471
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   472
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   473
        if (u != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   474
            try {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   475
                return u.toURL();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   476
            } catch (MalformedURLException | IllegalArgumentException e) { }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   477
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   478
        return null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   479
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   480
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   481
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   482
     * Returns the URL to a resource in a module. Returns {@code null} if not found
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   483
     * or an I/O error occurs.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   484
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   485
    private URL findResourceOrNull(ModuleReference mref, String name) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   486
        try {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   487
            return findResource(mref, name);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   488
        } catch (IOException ignore) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   489
            return null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   490
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   491
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   492
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   493
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   494
     * Returns a URL to a resource on the class path.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   495
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   496
    private URL findResourceOnClassPath(String name) {
44003
dc26a57d2570 8175385: ServiceLoader$LazyClassPathLookupIterator scans boot and platform modules for services
redestad
parents: 43712
diff changeset
   497
        if (hasClassPath()) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   498
            if (System.getSecurityManager() == null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   499
                return ucp.findResource(name, false);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   500
            } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   501
                PrivilegedAction<URL> pa = () -> ucp.findResource(name, false);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   502
                return AccessController.doPrivileged(pa);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   503
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   504
        } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   505
            // no class path
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   506
            return null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   507
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   508
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   509
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   510
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   511
     * Returns the URLs of all resources of the given name on the class path.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   512
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   513
    private Enumeration<URL> findResourcesOnClassPath(String name) {
44003
dc26a57d2570 8175385: ServiceLoader$LazyClassPathLookupIterator scans boot and platform modules for services
redestad
parents: 43712
diff changeset
   514
        if (hasClassPath()) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   515
            if (System.getSecurityManager() == null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   516
                return ucp.findResources(name, false);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   517
            } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   518
                PrivilegedAction<Enumeration<URL>> pa;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   519
                pa = () -> ucp.findResources(name, false);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   520
                return AccessController.doPrivileged(pa);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   521
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   522
        } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   523
            // no class path
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   524
            return Collections.emptyEnumeration();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   525
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   526
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   527
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   528
    // -- finding/loading classes
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   529
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   530
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   531
     * Finds the class with the specified binary name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   532
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   533
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   534
    protected Class<?> findClass(String cn) throws ClassNotFoundException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   535
        // no class loading until VM is fully initialized
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   536
        if (!VM.isModuleSystemInited())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   537
            throw new ClassNotFoundException(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   538
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   539
        // find the candidate module for this class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   540
        LoadedModule loadedModule = findLoadedModule(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   541
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   542
        Class<?> c = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   543
        if (loadedModule != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   544
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   545
            // attempt to load class in module defined to this loader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   546
            if (loadedModule.loader() == this) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   547
                c = findClassInModuleOrNull(loadedModule, cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   548
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   549
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   550
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   551
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   552
            // search class path
44003
dc26a57d2570 8175385: ServiceLoader$LazyClassPathLookupIterator scans boot and platform modules for services
redestad
parents: 43712
diff changeset
   553
            if (hasClassPath()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   554
                c = findClassOnClassPathOrNull(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   555
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   556
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   557
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   558
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   559
        // not found
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   560
        if (c == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   561
            throw new ClassNotFoundException(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   562
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   563
        return c;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   564
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   565
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   566
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   567
     * Finds the class with the specified binary name in a module.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   568
     * This method returns {@code null} if the class cannot be found
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   569
     * or not defined in the specified module.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   570
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   571
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   572
    protected Class<?> findClass(String mn, String cn) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   573
        if (mn != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   574
            // find the candidate module for this class
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   575
            LoadedModule loadedModule = findLoadedModule(mn, cn);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   576
            if (loadedModule == null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   577
                return null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   578
            }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   579
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   580
            // attempt to load class in module defined to this loader
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   581
            assert loadedModule.loader() == this;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   582
            return findClassInModuleOrNull(loadedModule, cn);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   583
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   584
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   585
        // search class path
44003
dc26a57d2570 8175385: ServiceLoader$LazyClassPathLookupIterator scans boot and platform modules for services
redestad
parents: 43712
diff changeset
   586
        if (hasClassPath()) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   587
            return findClassOnClassPathOrNull(cn);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   588
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   589
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   590
        return null;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   591
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   592
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   593
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   594
     * Loads the class with the specified binary name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   595
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   596
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   597
    protected Class<?> loadClass(String cn, boolean resolve)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   598
        throws ClassNotFoundException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   599
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   600
        Class<?> c = loadClassOrNull(cn, resolve);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   601
        if (c == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   602
            throw new ClassNotFoundException(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   603
        return c;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   604
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   605
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   606
    /**
49528
c1eb35eb5f38 8200125: Fix some classloader/module typos
martin
parents: 48995
diff changeset
   607
     * A variation of {@code loadClass} to load a class with the specified
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   608
     * binary name. This method returns {@code null} when the class is not
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   609
     * found.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   610
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   611
    protected Class<?> loadClassOrNull(String cn, boolean resolve) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   612
        synchronized (getClassLoadingLock(cn)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   613
            // check if already loaded
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   614
            Class<?> c = findLoadedClass(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   615
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   616
            if (c == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   617
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   618
                // find the candidate module for this class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   619
                LoadedModule loadedModule = findLoadedModule(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   620
                if (loadedModule != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   621
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   622
                    // package is in a module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   623
                    BuiltinClassLoader loader = loadedModule.loader();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   624
                    if (loader == this) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   625
                        if (VM.isModuleSystemInited()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   626
                            c = findClassInModuleOrNull(loadedModule, cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   627
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   628
                    } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   629
                        // delegate to the other loader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   630
                        c = loader.loadClassOrNull(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   631
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   632
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   633
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   634
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   635
                    // check parent
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   636
                    if (parent != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   637
                        c = parent.loadClassOrNull(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   638
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   639
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   640
                    // check class path
44003
dc26a57d2570 8175385: ServiceLoader$LazyClassPathLookupIterator scans boot and platform modules for services
redestad
parents: 43712
diff changeset
   641
                    if (c == null && hasClassPath() && VM.isModuleSystemInited()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   642
                        c = findClassOnClassPathOrNull(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   643
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   644
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   645
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   646
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   647
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   648
            if (resolve && c != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   649
                resolveClass(c);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   650
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   651
            return c;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   652
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   653
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   654
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   655
    /**
49528
c1eb35eb5f38 8200125: Fix some classloader/module typos
martin
parents: 48995
diff changeset
   656
     * A variation of {@code loadClass} to load a class with the specified
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   657
     * binary name. This method returns {@code null} when the class is not
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   658
     * found.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   659
     */
53660
929f0c7e019b 8218419: Can't get annotations that are present on packages in -Xbootclasspath/a
mchung
parents: 52427
diff changeset
   660
    protected final Class<?> loadClassOrNull(String cn) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   661
        return loadClassOrNull(cn, false);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   662
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   663
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   664
    /**
49528
c1eb35eb5f38 8200125: Fix some classloader/module typos
martin
parents: 48995
diff changeset
   665
     * Finds the candidate loaded module for the given class name.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   666
     * Returns {@code null} if none of the modules defined to this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   667
     * class loader contain the API package for the class.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   668
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   669
    private LoadedModule findLoadedModule(String cn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   670
        int pos = cn.lastIndexOf('.');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   671
        if (pos < 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   672
            return null; // unnamed package
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   673
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   674
        String pn = cn.substring(0, pos);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   675
        return packageToModule.get(pn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   676
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   677
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   678
    /**
49528
c1eb35eb5f38 8200125: Fix some classloader/module typos
martin
parents: 48995
diff changeset
   679
     * Finds the candidate loaded module for the given class name
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   680
     * in the named module.  Returns {@code null} if the named module
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   681
     * is not defined to this class loader or does not contain
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   682
     * the API package for the class.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   683
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   684
    private LoadedModule findLoadedModule(String mn, String cn) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   685
        LoadedModule loadedModule = findLoadedModule(cn);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   686
        if (loadedModule != null && mn.equals(loadedModule.name())) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   687
            return loadedModule;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   688
        } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   689
            return null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   690
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   691
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   692
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   693
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   694
     * Finds the class with the specified binary name if in a module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   695
     * defined to this ClassLoader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   696
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   697
     * @return the resulting Class or {@code null} if not found
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   698
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   699
    private Class<?> findClassInModuleOrNull(LoadedModule loadedModule, String cn) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   700
        if (System.getSecurityManager() == null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   701
            return defineClass(cn, loadedModule);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   702
        } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   703
            PrivilegedAction<Class<?>> pa = () -> defineClass(cn, loadedModule);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   704
            return AccessController.doPrivileged(pa);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   705
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   706
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   707
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   708
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   709
     * Finds the class with the specified binary name on the class path.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   710
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   711
     * @return the resulting Class or {@code null} if not found
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   712
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   713
    private Class<?> findClassOnClassPathOrNull(String cn) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   714
        String path = cn.replace('.', '/').concat(".class");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   715
        if (System.getSecurityManager() == null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   716
            Resource res = ucp.getResource(path, false);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   717
            if (res != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   718
                try {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   719
                    return defineClass(cn, res);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   720
                } catch (IOException ioe) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   721
                    // TBD on how I/O errors should be propagated
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   722
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   723
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   724
            return null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   725
        } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   726
            // avoid use of lambda here
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   727
            PrivilegedAction<Class<?>> pa = new PrivilegedAction<>() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   728
                public Class<?> run() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   729
                    Resource res = ucp.getResource(path, false);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   730
                    if (res != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   731
                        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   732
                            return defineClass(cn, res);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   733
                        } catch (IOException ioe) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   734
                            // TBD on how I/O errors should be propagated
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   735
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   736
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   737
                    return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   738
                }
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   739
            };
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   740
            return AccessController.doPrivileged(pa);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   741
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   742
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   743
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   744
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   745
     * Defines the given binary class name to the VM, loading the class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   746
     * bytes from the given module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   747
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   748
     * @return the resulting Class or {@code null} if an I/O error occurs
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   749
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   750
    private Class<?> defineClass(String cn, LoadedModule loadedModule) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   751
        ModuleReference mref = loadedModule.mref();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   752
        ModuleReader reader = moduleReaderFor(mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   753
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   754
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   755
            ByteBuffer bb = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   756
            URL csURL = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   757
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   758
            // locate class file, special handling for patched modules to
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   759
            // avoid locating the resource twice
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   760
            String rn = cn.replace('.', '/').concat(".class");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   761
            if (reader instanceof PatchedModuleReader) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   762
                Resource r = ((PatchedModuleReader)reader).findResource(rn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   763
                if (r != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   764
                    bb = r.getByteBuffer();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   765
                    csURL = r.getCodeSourceURL();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   766
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   767
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   768
                bb = reader.read(rn).orElse(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   769
                csURL = loadedModule.codeSourceURL();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   770
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   771
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   772
            if (bb == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   773
                // class not found
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   774
                return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   775
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   776
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   777
            CodeSource cs = new CodeSource(csURL, (CodeSigner[]) null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   778
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   779
                // define class to VM
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   780
                return defineClass(cn, bb, cs);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   781
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   782
            } finally {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   783
                reader.release(bb);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   784
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   785
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   786
        } catch (IOException ioe) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   787
            // TBD on how I/O errors should be propagated
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   788
            return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   789
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   790
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   791
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   792
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   793
     * Defines the given binary class name to the VM, loading the class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   794
     * bytes via the given Resource object.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   795
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   796
     * @return the resulting Class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   797
     * @throws IOException if reading the resource fails
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   798
     * @throws SecurityException if there is a sealing violation (JAR spec)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   799
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   800
    private Class<?> defineClass(String cn, Resource res) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   801
        URL url = res.getCodeSourceURL();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   802
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   803
        // if class is in a named package then ensure that the package is defined
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   804
        int pos = cn.lastIndexOf('.');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   805
        if (pos != -1) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   806
            String pn = cn.substring(0, pos);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   807
            Manifest man = res.getManifest();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   808
            defineOrCheckPackage(pn, man, url);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   809
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   810
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   811
        // defines the class to the runtime
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   812
        ByteBuffer bb = res.getByteBuffer();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   813
        if (bb != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   814
            CodeSigner[] signers = res.getCodeSigners();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   815
            CodeSource cs = new CodeSource(url, signers);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   816
            return defineClass(cn, bb, cs);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   817
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   818
            byte[] b = res.getBytes();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   819
            CodeSigner[] signers = res.getCodeSigners();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   820
            CodeSource cs = new CodeSource(url, signers);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   821
            return defineClass(cn, b, 0, b.length, cs);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   822
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   823
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   824
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   825
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   826
    // -- packages
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   827
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   828
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   829
     * Defines a package in this ClassLoader. If the package is already defined
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   830
     * then its sealing needs to be checked if sealed by the legacy sealing
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   831
     * mechanism.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   832
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   833
     * @throws SecurityException if there is a sealing violation (JAR spec)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   834
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   835
    protected Package defineOrCheckPackage(String pn, Manifest man, URL url) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   836
        Package pkg = getAndVerifyPackage(pn, man, url);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   837
        if (pkg == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   838
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   839
                if (man != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   840
                    pkg = definePackage(pn, man, url);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   841
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   842
                    pkg = definePackage(pn, null, null, null, null, null, null, null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   843
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   844
            } catch (IllegalArgumentException iae) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   845
                // defined by another thread so need to re-verify
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   846
                pkg = getAndVerifyPackage(pn, man, url);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   847
                if (pkg == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   848
                    throw new InternalError("Cannot find package: " + pn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   849
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   850
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   851
        return pkg;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   852
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   853
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   854
    /**
49528
c1eb35eb5f38 8200125: Fix some classloader/module typos
martin
parents: 48995
diff changeset
   855
     * Gets the Package with the specified package name. If defined
c1eb35eb5f38 8200125: Fix some classloader/module typos
martin
parents: 48995
diff changeset
   856
     * then verifies it against the manifest and code source.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   857
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   858
     * @throws SecurityException if there is a sealing violation (JAR spec)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   859
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   860
    private Package getAndVerifyPackage(String pn, Manifest man, URL url) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   861
        Package pkg = getDefinedPackage(pn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   862
        if (pkg != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   863
            if (pkg.isSealed()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   864
                if (!pkg.isSealed(url)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   865
                    throw new SecurityException(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   866
                        "sealing violation: package " + pn + " is sealed");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   867
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   868
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   869
                // can't seal package if already defined without sealing
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   870
                if ((man != null) && isSealed(pn, man)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   871
                    throw new SecurityException(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   872
                        "sealing violation: can't seal package " + pn +
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   873
                        ": already defined");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   874
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   875
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   876
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   877
        return pkg;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   878
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   879
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   880
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   881
     * Defines a new package in this ClassLoader. The attributes in the specified
49528
c1eb35eb5f38 8200125: Fix some classloader/module typos
martin
parents: 48995
diff changeset
   882
     * Manifest are used to get the package version and sealing information.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   883
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   884
     * @throws IllegalArgumentException if the package name duplicates an
52159
42244a052fbb 8194534: Manifest better support
weijun
parents: 49528
diff changeset
   885
     *      existing package either in this class loader or one of its ancestors
42244a052fbb 8194534: Manifest better support
weijun
parents: 49528
diff changeset
   886
     * @throws SecurityException if the package name is untrusted in the manifest
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   887
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   888
    private Package definePackage(String pn, Manifest man, URL url) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   889
        String specTitle = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   890
        String specVersion = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   891
        String specVendor = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   892
        String implTitle = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   893
        String implVersion = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   894
        String implVendor = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   895
        String sealed = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   896
        URL sealBase = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   897
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   898
        if (man != null) {
52159
42244a052fbb 8194534: Manifest better support
weijun
parents: 49528
diff changeset
   899
            Attributes attr = SharedSecrets.javaUtilJarAccess()
42244a052fbb 8194534: Manifest better support
weijun
parents: 49528
diff changeset
   900
                    .getTrustedAttributes(man, pn.replace('.', '/').concat("/"));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   901
            if (attr != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   902
                specTitle = attr.getValue(Attributes.Name.SPECIFICATION_TITLE);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   903
                specVersion = attr.getValue(Attributes.Name.SPECIFICATION_VERSION);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   904
                specVendor = attr.getValue(Attributes.Name.SPECIFICATION_VENDOR);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   905
                implTitle = attr.getValue(Attributes.Name.IMPLEMENTATION_TITLE);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   906
                implVersion = attr.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   907
                implVendor = attr.getValue(Attributes.Name.IMPLEMENTATION_VENDOR);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   908
                sealed = attr.getValue(Attributes.Name.SEALED);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   909
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   910
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   911
            attr = man.getMainAttributes();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   912
            if (attr != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   913
                if (specTitle == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   914
                    specTitle = attr.getValue(Attributes.Name.SPECIFICATION_TITLE);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   915
                if (specVersion == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   916
                    specVersion = attr.getValue(Attributes.Name.SPECIFICATION_VERSION);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   917
                if (specVendor == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   918
                    specVendor = attr.getValue(Attributes.Name.SPECIFICATION_VENDOR);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   919
                if (implTitle == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   920
                    implTitle = attr.getValue(Attributes.Name.IMPLEMENTATION_TITLE);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   921
                if (implVersion == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   922
                    implVersion = attr.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   923
                if (implVendor == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   924
                    implVendor = attr.getValue(Attributes.Name.IMPLEMENTATION_VENDOR);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   925
                if (sealed == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   926
                    sealed = attr.getValue(Attributes.Name.SEALED);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   927
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   928
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   929
            // package is sealed
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   930
            if ("true".equalsIgnoreCase(sealed))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   931
                sealBase = url;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   932
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   933
        return definePackage(pn,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   934
                specTitle,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   935
                specVersion,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   936
                specVendor,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   937
                implTitle,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   938
                implVersion,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   939
                implVendor,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   940
                sealBase);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   941
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   942
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   943
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   944
     * Returns {@code true} if the specified package name is sealed according to
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   945
     * the given manifest.
52159
42244a052fbb 8194534: Manifest better support
weijun
parents: 49528
diff changeset
   946
     *
42244a052fbb 8194534: Manifest better support
weijun
parents: 49528
diff changeset
   947
     * @throws SecurityException if the package name is untrusted in the manifest
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   948
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   949
    private boolean isSealed(String pn, Manifest man) {
52159
42244a052fbb 8194534: Manifest better support
weijun
parents: 49528
diff changeset
   950
        Attributes attr = SharedSecrets.javaUtilJarAccess()
42244a052fbb 8194534: Manifest better support
weijun
parents: 49528
diff changeset
   951
                .getTrustedAttributes(man, pn.replace('.', '/').concat("/"));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   952
        String sealed = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   953
        if (attr != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   954
            sealed = attr.getValue(Attributes.Name.SEALED);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   955
        if (sealed == null && (attr = man.getMainAttributes()) != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   956
            sealed = attr.getValue(Attributes.Name.SEALED);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   957
        return "true".equalsIgnoreCase(sealed);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   958
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   959
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   960
    // -- permissions
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   961
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   962
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   963
     * Returns the permissions for the given CodeSource.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   964
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   965
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   966
    protected PermissionCollection getPermissions(CodeSource cs) {
57792
1b6806340400 8229773: Resolve permissions for code source URLs lazily
redestad
parents: 57714
diff changeset
   967
        return new LazyCodeSourcePermissionCollection(super.getPermissions(cs), cs);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   968
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   969
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   970
    // -- miscellaneous supporting methods
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   971
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   972
    /**
49528
c1eb35eb5f38 8200125: Fix some classloader/module typos
martin
parents: 48995
diff changeset
   973
     * Returns the ModuleReader for the given module, creating it if needed.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   974
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   975
    private ModuleReader moduleReaderFor(ModuleReference mref) {
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   976
        ModuleReader reader = moduleToReader.get(mref);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   977
        if (reader == null) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   978
            // avoid method reference during startup
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   979
            Function<ModuleReference, ModuleReader> create = new Function<>() {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   980
                public ModuleReader apply(ModuleReference moduleReference) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   981
                    try {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   982
                        return mref.open();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   983
                    } catch (IOException e) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   984
                        // Return a null module reader to avoid a future class
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   985
                        // load attempting to open the module again.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   986
                        return new NullModuleReader();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   987
                    }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   988
                }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   989
            };
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   990
            reader = moduleToReader.computeIfAbsent(mref, create);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   991
        }
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   992
        return reader;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   993
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   994
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   995
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   996
     * A ModuleReader that doesn't read any resources.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   997
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   998
    private static class NullModuleReader implements ModuleReader {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   999
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1000
        public Optional<URI> find(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1001
            return Optional.empty();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1002
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1003
        @Override
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 37779
diff changeset
  1004
        public Stream<String> list() {
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 37779
diff changeset
  1005
            return Stream.empty();
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 37779
diff changeset
  1006
        }
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 37779
diff changeset
  1007
        @Override
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1008
        public void close() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1009
            throw new InternalError("Should not get here");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1010
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1011
    };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1012
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1013
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1014
     * Returns true if the given module opens the given package
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1015
     * unconditionally.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1016
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1017
     * @implNote This method currently iterates over each of the open
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1018
     * packages. This will be replaced once the ModuleDescriptor.Opens
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1019
     * API is updated.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1020
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1021
    private boolean isOpen(ModuleReference mref, String pn) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1022
        ModuleDescriptor descriptor = mref.descriptor();
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44003
diff changeset
  1023
        if (descriptor.isOpen() || descriptor.isAutomatic())
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1024
            return true;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1025
        for (ModuleDescriptor.Opens opens : descriptor.opens()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1026
            String source = opens.source();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1027
            if (!opens.isQualified() && source.equals(pn)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1028
                return true;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1029
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1030
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1031
        return false;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1032
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1033
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1034
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1035
     * Checks access to the given URL. We use URLClassPath for consistent
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1036
     * checking with java.net.URLClassLoader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1037
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1038
    private static URL checkURL(URL url) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1039
        return URLClassPath.checkURL(url);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1040
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1041
}