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