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