jdk/src/java.base/share/classes/jdk/internal/loader/Loader.java
author alanb
Thu, 01 Dec 2016 08:57:53 +0000
changeset 42338 a60f280f803c
parent 41817 b90ad1de93ea
child 44359 c6761862ca0b
permissions -rw-r--r--
8169069: Module system implementation refresh (11/2016) Reviewed-by: plevart, chegar, psandoz, mchung, alanb, dfuchs, naoto, coffeys, weijun Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com, claes.redestad@oracle.com, mark.reinhold@oracle.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.lang.module.Configuration;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.lang.module.ModuleDescriptor;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.lang.module.ModuleReader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.lang.module.ModuleReference;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.lang.module.ResolvedModule;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.lang.reflect.Layer;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.net.MalformedURLException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.net.URI;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.net.URL;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.nio.ByteBuffer;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.security.AccessControlContext;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.security.AccessController;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.security.CodeSigner;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.security.CodeSource;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import java.security.Permission;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import java.security.PermissionCollection;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import java.security.PrivilegedAction;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import java.security.PrivilegedActionException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import java.security.PrivilegedExceptionAction;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
import java.security.SecureClassLoader;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
    51
import java.util.ArrayList;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
import java.util.Collection;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
    53
import java.util.Collections;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
    54
import java.util.Enumeration;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
import java.util.HashMap;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
    56
import java.util.List;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
import java.util.Optional;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
import java.util.concurrent.ConcurrentHashMap;
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 38571
diff changeset
    60
import java.util.stream.Stream;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
    62
import jdk.internal.misc.SharedSecrets;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
    63
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
 * A class loader that loads classes and resources from a collection of
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
 * modules, or from a single module where the class loader is a member
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
 * of a pool of class loaders.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
 * <p> The delegation model used by this ClassLoader differs to the regular
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
 * delegation model. When requested to load a class then this ClassLoader first
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
 * maps the class name to its package name. If there a module defined to the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
 * Loader containing the package then the class loader attempts to load from
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
 * that module. If the package is instead defined to a module in a "remote"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
 * ClassLoader then this class loader delegates directly to that class loader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
 * The map of package name to remote class loader is created based on the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
 * modules read by modules defined to this class loader. If the package is not
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
 * local or remote then this class loader will delegate to the parent class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
 * loader. This allows automatic modules (for example) to link to types in the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
 * unnamed module of the parent class loader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
 *
38571
75eeea87c73d 8156143: Module.getResourceAsStream throws unspecified SecurityException with module in custom Layer
alanb
parents: 36511
diff changeset
    82
 * @see Layer#defineModulesWithOneLoader
75eeea87c73d 8156143: Module.getResourceAsStream throws unspecified SecurityException with module in custom Layer
alanb
parents: 36511
diff changeset
    83
 * @see Layer#defineModulesWithManyLoaders
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
public final class Loader extends SecureClassLoader {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
    static {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
        ClassLoader.registerAsParallelCapable();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
    // the loader pool is in a pool, can be null
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
    private final LoaderPool pool;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
    // parent ClassLoader, can be null
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
    private final ClassLoader parent;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
    // maps a module name to a module reference
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
    private final Map<String, ModuleReference> nameToModule;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
    // maps package name to a module loaded by this class loader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
    private final Map<String, LoadedModule> localPackageToModule;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
    // maps package name to a remote class loader, populated post initialization
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
    private final Map<String, ClassLoader> remotePackageToLoader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
        = new ConcurrentHashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
    // maps a module reference to a module reader, populated lazily
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
    private final Map<ModuleReference, ModuleReader> moduleToReader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
        = new ConcurrentHashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
    // ACC used when loading classes and resources */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
    private final AccessControlContext acc;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
     * A module defined/loaded to a {@code Loader}.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
    private static class LoadedModule {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
        private final ModuleReference mref;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
        private final URL url;          // may be null
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
        private final CodeSource cs;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
        LoadedModule(ModuleReference mref) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
            URL url = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
            if (mref.location().isPresent()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
                try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
                    url = mref.location().get().toURL();
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   128
                } catch (MalformedURLException | IllegalArgumentException e) { }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
            this.mref = mref;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
            this.url = url;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
            this.cs = new CodeSource(url, (CodeSigner[]) null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
        ModuleReference mref() { return mref; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
        String name() { return mref.descriptor().name(); }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
        URL location() { return url; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
        CodeSource codeSource() { return cs; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
     * Creates a {@code Loader} in a loader pool that loads classes/resources
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
     * from one module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
    public Loader(ResolvedModule resolvedModule,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
                  LoaderPool pool,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
                  ClassLoader parent)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
    {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   150
        super("Loader-" + resolvedModule.name(), parent);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
        this.pool = pool;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
        this.parent = parent;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
        ModuleReference mref = resolvedModule.reference();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
        ModuleDescriptor descriptor = mref.descriptor();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
        String mn = descriptor.name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
        this.nameToModule = Map.of(mn, mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
        Map<String, LoadedModule> localPackageToModule = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
        LoadedModule lm = new LoadedModule(mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
        descriptor.packages().forEach(pn -> localPackageToModule.put(pn, lm));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
        this.localPackageToModule = localPackageToModule;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
        this.acc = AccessController.getContext();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
     * Creates a {@code Loader} that loads classes/resources from a collection
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
     * of modules.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
     * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
     *         If two or more modules have the same package
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
    public Loader(Collection<ResolvedModule> modules, ClassLoader parent) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
        super(parent);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
        this.pool = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
        this.parent = parent;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
        Map<String, ModuleReference> nameToModule = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
        Map<String, LoadedModule> localPackageToModule = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
        for (ResolvedModule resolvedModule : modules) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
            ModuleReference mref = resolvedModule.reference();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
            ModuleDescriptor descriptor = mref.descriptor();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
            nameToModule.put(descriptor.name(), mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
            descriptor.packages().forEach(pn -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
                LoadedModule lm = new LoadedModule(mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
                if (localPackageToModule.put(pn, lm) != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
                    throw new IllegalArgumentException("Package "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
                        + pn + " in more than one module");
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
        this.nameToModule = nameToModule;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
        this.localPackageToModule = localPackageToModule;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   197
        this.acc = AccessController.getContext();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
     * Completes initialization of this Loader. This method populates
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
     * remotePackageToLoader with the packages of the remote modules, where
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
     * "remote modules" are the modules read by modules defined to this loader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
     * @param cf the Configuration containing at least modules to be defined to
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
     *           this class loader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   209
     * @param parentLayers the parent Layers
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
     */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   211
    public Loader initRemotePackageMap(Configuration cf,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   212
                                       List<Layer> parentLayers)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   213
    {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   214
        for (String name : nameToModule.keySet()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   215
            ResolvedModule resolvedModule = cf.findModule(name).get();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   216
            assert resolvedModule.configuration() == cf;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
            for (ResolvedModule other : resolvedModule.reads()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
                String mn = other.name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
                ClassLoader loader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
                if (other.configuration() == cf) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
                    // The module reads another module in the newly created
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
                    // layer. If all modules are defined to the same class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
                    // loader then the packages are local.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
                    if (pool == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
                        assert nameToModule.containsKey(mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
                        continue;
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
                    loader = pool.loaderFor(mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
                    assert loader != null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   237
                    // find the layer for the target module
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   238
                    Layer layer = parentLayers.stream()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   239
                        .map(parent -> findLayer(parent, other.configuration()))
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   240
                        .flatMap(Optional::stream)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   241
                        .findAny()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   242
                        .orElseThrow(() ->
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   243
                            new InternalError("Unable to find parent layer"));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   245
                    // find the class loader for the module
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
                    // For now we use the platform loader for modules defined to the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
                    // boot loader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
                    assert layer.findModule(mn).isPresent();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
                    loader = layer.findLoader(mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
                    if (loader == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
                        loader = ClassLoaders.platformClassLoader();
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
                // find the packages that are exported to the target module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   255
                String target = resolvedModule.name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   256
                ModuleDescriptor descriptor = other.reference().descriptor();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   257
                for (ModuleDescriptor.Exports e : descriptor.exports()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   258
                    boolean delegate;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   259
                    if (e.isQualified()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   260
                        // qualified export in same configuration
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   261
                        delegate = (other.configuration() == cf)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   262
                                && e.targets().contains(target);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
                    } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   264
                        // unqualified
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   265
                        delegate = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
                    if (delegate) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
                        String pn = e.source();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   270
                        ClassLoader l = remotePackageToLoader.putIfAbsent(pn, loader);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   271
                        if (l != null && l != loader) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   272
                            throw new IllegalArgumentException("Package "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   273
                                + pn + " cannot be imported from multiple loaders");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   274
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   275
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   276
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   277
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   278
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   279
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   280
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   281
        return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   282
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   283
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   284
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   285
     * Find the layer corresponding to the given configuration in the tree
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   286
     * of layers rooted at the given parent.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   287
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   288
    private Optional<Layer> findLayer(Layer parent, Configuration cf) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   289
        return SharedSecrets.getJavaLangReflectModuleAccess().layers(parent)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   290
                .filter(l -> l.configuration() == cf)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   291
                .findAny();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   292
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   293
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   294
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   295
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   296
     * Returns the loader pool that this loader is in or {@code null} if this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   297
     * loader is not in a loader pool.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   298
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   299
    public LoaderPool pool() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   300
        return pool;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   301
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   302
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   303
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   304
    // -- resources --
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   305
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   306
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   307
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   308
     * Returns a URL to a resource of the given name in a module defined to
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   309
     * this class loader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   310
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   311
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   312
    protected URL findResource(String mn, String name) throws IOException {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   313
        ModuleReference mref = (mn != null) ? nameToModule.get(mn) : null;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   314
        if (mref == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   315
            return null;   // not defined to this class loader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   316
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   317
        // locate resource
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   318
        URL url = null;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   319
        try {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   320
            url = AccessController.doPrivileged(
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   321
                new PrivilegedExceptionAction<URL>() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   322
                    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   323
                    public URL run() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   324
                        Optional<URI> ouri = moduleReaderFor(mref).find(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   325
                        if (ouri.isPresent()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   326
                            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   327
                                return ouri.get().toURL();
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   328
                            } catch (MalformedURLException |
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   329
                                     IllegalArgumentException e) { }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   330
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   331
                        return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   332
                    }
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   333
                });
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   334
        } catch (PrivilegedActionException pae) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   335
            throw (IOException) pae.getCause();
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   336
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   337
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   338
        // check access with permissions restricted by ACC
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   339
        if (url != null && System.getSecurityManager() != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   340
            try {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   341
                URL urlToCheck = url;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   342
                url = AccessController.doPrivileged(
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   343
                    new PrivilegedExceptionAction<URL>() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   344
                        @Override
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   345
                        public URL run() throws IOException {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   346
                            return URLClassPath.checkURL(urlToCheck);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   347
                        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   348
                    }, acc);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   349
            } catch (PrivilegedActionException pae) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   350
                url = null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   351
            }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   352
        }
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   353
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   354
        return url;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   355
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   356
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   357
    @Override
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   358
    public URL findResource(String name) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   359
        URL url = null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   360
        String pn = ResourceHelper.getPackageName(name);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   361
        LoadedModule module = localPackageToModule.get(pn);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   362
        if (module != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   363
            if (name.endsWith(".class") || isOpen(module.mref(), pn)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   364
                try {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   365
                    url = findResource(module.name(), name);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   366
                } catch (IOException ioe) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   367
                    // ignore
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   368
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   369
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   370
        } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   371
            for (ModuleReference mref : nameToModule.values()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   372
                try {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   373
                    url = findResource(mref.descriptor().name(), name);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   374
                    if (url != null)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   375
                        break;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   376
                } catch (IOException ioe) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   377
                    // ignore
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   378
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   379
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   380
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   381
        return url;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   382
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   383
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   384
    @Override
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   385
    public Enumeration<URL> findResources(String name) throws IOException {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   386
        List<URL> urls = new ArrayList<>();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   387
        String pn = ResourceHelper.getPackageName(name);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   388
        LoadedModule module = localPackageToModule.get(pn);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   389
        if (module != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   390
            if (name.endsWith(".class") || isOpen(module.mref(), pn)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   391
                try {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   392
                    URL url = findResource(module.name(), name);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   393
                    if (url != null)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   394
                        urls.add(url);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   395
                } catch (IOException ioe) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   396
                    // ignore
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   397
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   398
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   399
        } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   400
            for (ModuleReference mref : nameToModule.values()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   401
                try {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   402
                    URL url = findResource(mref.descriptor().name(), name);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   403
                    if (url != null)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   404
                        urls.add(url);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   405
                } catch (IOException ioe) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   406
                    // ignore
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   407
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   408
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   409
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   410
        return Collections.enumeration(urls);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   411
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   412
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   413
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   414
    // -- finding/loading classes
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   415
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   416
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   417
     * Finds the class with the specified binary name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   418
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   419
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   420
    protected Class<?> findClass(String cn) throws ClassNotFoundException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   421
        Class<?> c = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   422
        LoadedModule loadedModule = findLoadedModule(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   423
        if (loadedModule != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   424
            c = findClassInModuleOrNull(loadedModule, cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   425
        if (c == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   426
            throw new ClassNotFoundException(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   427
        return c;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   428
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   429
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   430
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   431
     * Finds the class with the specified binary name in a given module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   432
     * This method returns {@code null} if the class cannot be found.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   433
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   434
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   435
    protected Class<?> findClass(String mn, String cn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   436
        Class<?> c = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   437
        LoadedModule loadedModule = findLoadedModule(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   438
        if (loadedModule != null && loadedModule.name().equals(mn))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   439
            c = findClassInModuleOrNull(loadedModule, cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   440
        return c;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   441
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   442
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   443
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   444
     * Loads the class with the specified binary name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   445
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   446
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   447
    protected Class<?> loadClass(String cn, boolean resolve)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   448
        throws ClassNotFoundException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   449
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   450
        SecurityManager sm = System.getSecurityManager();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   451
        if (sm != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   452
            String pn = packageName(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   453
            if (!pn.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   454
                sm.checkPackageAccess(pn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   455
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   456
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   457
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   458
        synchronized (getClassLoadingLock(cn)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   459
            // check if already loaded
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   460
            Class<?> c = findLoadedClass(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   461
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   462
            if (c == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   463
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   464
                LoadedModule loadedModule = findLoadedModule(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   465
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   466
                if (loadedModule != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   467
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   468
                    // class is in module defined to this class loader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   469
                    c = findClassInModuleOrNull(loadedModule, cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   470
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   471
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   472
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   473
                    // type in another module or visible via the parent loader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   474
                    String pn = packageName(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   475
                    ClassLoader loader = remotePackageToLoader.get(pn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   476
                    if (loader == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   477
                        // type not in a module read by any of the modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   478
                        // defined to this loader, so delegate to parent
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   479
                        // class loader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   480
                        loader = parent;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   481
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   482
                    if (loader == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   483
                        c = BootLoader.loadClassOrNull(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   484
                    } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   485
                        c = loader.loadClass(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   486
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   487
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   488
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   489
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   490
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   491
            if (c == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   492
                throw new ClassNotFoundException(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   493
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   494
            if (resolve)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   495
                resolveClass(c);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   496
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   497
            return c;
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   501
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   502
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   503
     * Finds the class with the specified binary name if in a module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   504
     * defined to this ClassLoader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   505
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   506
     * @return the resulting Class or {@code null} if not found
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   507
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   508
    private Class<?> findClassInModuleOrNull(LoadedModule loadedModule, String cn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   509
        PrivilegedAction<Class<?>> pa = () -> defineClass(cn, loadedModule);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   510
        return AccessController.doPrivileged(pa, acc);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   511
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   512
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   513
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   514
     * Defines the given binary class name to the VM, loading the class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   515
     * bytes from the given module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   516
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   517
     * @return the resulting Class or {@code null} if an I/O error occurs
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   518
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   519
    private Class<?> defineClass(String cn, LoadedModule loadedModule) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   520
        ModuleReader reader = moduleReaderFor(loadedModule.mref());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   521
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   522
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   523
            // read class file
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   524
            String rn = cn.replace('.', '/').concat(".class");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   525
            ByteBuffer bb = reader.read(rn).orElse(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   526
            if (bb == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   527
                // class not found
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   528
                return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   529
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   530
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   531
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   532
                return defineClass(cn, bb, loadedModule.codeSource());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   533
            } finally {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   534
                reader.release(bb);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   535
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   536
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   537
        } catch (IOException ioe) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   538
            // TBD on how I/O errors should be propagated
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   539
            return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   540
        }
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
    // -- permissions
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   545
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   546
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   547
     * Returns the permissions for the given CodeSource.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   548
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   549
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   550
    protected PermissionCollection getPermissions(CodeSource cs) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   551
        PermissionCollection perms = super.getPermissions(cs);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   552
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   553
        URL url = cs.getLocation();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   554
        if (url == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   555
            return perms;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   556
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   557
        // add the permission to access the resource
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   558
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   559
            Permission p = url.openConnection().getPermission();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   560
            if (p != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   561
                // for directories then need recursive access
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   562
                if (p instanceof FilePermission) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   563
                    String path = p.getName();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   564
                    if (path.endsWith(File.separator)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   565
                        path += "-";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   566
                        p = new FilePermission(path, "read");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   567
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   568
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   569
                perms.add(p);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   570
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   571
        } catch (IOException ioe) { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   572
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   573
        return perms;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   574
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   575
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   576
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   577
    // -- miscellaneous supporting methods
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   578
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   579
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   580
     * Find the candidate module for the given class name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   581
     * Returns {@code null} if none of the modules defined to this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   582
     * class loader contain the API package for the class.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   583
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   584
    private LoadedModule findLoadedModule(String cn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   585
        String pn = packageName(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   586
        return pn.isEmpty() ? null : localPackageToModule.get(pn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   587
    }
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
     * Returns the package name for the given class name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   591
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   592
    private String packageName(String cn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   593
        int pos = cn.lastIndexOf('.');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   594
        return (pos < 0) ? "" : cn.substring(0, pos);
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
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   599
     * Returns the ModuleReader for the given module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   600
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   601
    private ModuleReader moduleReaderFor(ModuleReference mref) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   602
        return moduleToReader.computeIfAbsent(mref, m -> createModuleReader(mref));
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
     * Creates a ModuleReader for the given module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   607
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   608
    private ModuleReader createModuleReader(ModuleReference mref) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   609
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   610
            return mref.open();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   611
        } catch (IOException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   612
            // Return a null module reader to avoid a future class load
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   613
            // attempting to open the module again.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   614
            return new NullModuleReader();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   615
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   616
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   617
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   618
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   619
     * A ModuleReader that doesn't read any resources.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   620
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   621
    private static class NullModuleReader implements ModuleReader {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   622
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   623
        public Optional<URI> find(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   624
            return Optional.empty();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   625
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   626
        @Override
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 38571
diff changeset
   627
        public Stream<String> list() {
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 38571
diff changeset
   628
            return Stream.empty();
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 38571
diff changeset
   629
        }
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 38571
diff changeset
   630
        @Override
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   631
        public void close() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   632
            throw new InternalError("Should not get here");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   633
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   634
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   635
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   636
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   637
     * Returns true if the given module opens the given package
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   638
     * unconditionally.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   639
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   640
     * @implNote This method currently iterates over each of the open
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   641
     * packages. This will be replaced once the ModuleDescriptor.Opens
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   642
     * API is updated.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   643
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   644
    private boolean isOpen(ModuleReference mref, String pn) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   645
        ModuleDescriptor descriptor = mref.descriptor();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   646
        if (descriptor.isOpen())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   647
            return true;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   648
        for (ModuleDescriptor.Opens opens : descriptor.opens()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   649
            String source = opens.source();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   650
            if (!opens.isQualified() && source.equals(pn)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   651
                return true;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   652
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   653
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   654
        return false;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   655
    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   656
}