jdk/src/java.base/share/classes/java/lang/module/ModuleFinder.java
author alanb
Fri, 07 Apr 2017 08:05:54 +0000
changeset 44545 83b611b88ac8
parent 44359 c6761862ca0b
child 45004 ea3137042a61
permissions -rw-r--r--
8177530: Module system implementation refresh (4/2017) Reviewed-by: mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
     2
 * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
package java.lang.module;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.nio.file.Paths;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.security.AccessController;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.security.Permission;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.security.PrivilegedAction;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.util.Collections;
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
    35
import java.util.HashMap;
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
    36
import java.util.HashSet;
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
    37
import java.util.List;
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
    38
import java.util.Map;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.util.Objects;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.util.Optional;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.util.Set;
39050
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 38457
diff changeset
    42
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    43
import jdk.internal.module.ModuleBootstrap;
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
    44
import jdk.internal.module.ModulePatcher;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    45
import jdk.internal.module.ModulePath;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    46
import jdk.internal.module.SystemModuleFinder;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
 * A finder of modules. A {@code ModuleFinder} is used to find modules during
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    50
 * <a href="package-summary.html#resolution">resolution</a> or
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    51
 * <a href="package-summary.html#servicebinding">service binding</a>.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
 * <p> A {@code ModuleFinder} can only find one module with a given name. A
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
 * {@code ModuleFinder} that finds modules in a sequence of directories, for
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
 * example, will locate the first occurrence of a module of a given name and
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
 * will ignore other modules of that name that appear in directories later in
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
 * the sequence. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
 * <p> Example usage: </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
 * <pre>{@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
 *     Path dir1, dir2, dir3;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
 *     ModuleFinder finder = ModuleFinder.of(dir1, dir2, dir3);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
 *     Optional<ModuleReference> omref = finder.find("jdk.foo");
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
    67
 *     omref.ifPresent(mref -> ... );
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
 * }</pre>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
 * <p> The {@link #find(String) find} and {@link #findAll() findAll} methods
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
    72
 * defined here can fail for several reasons. These include I/O errors, errors
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
    73
 * detected parsing a module descriptor ({@code module-info.class}), or in the
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
    74
 * case of {@code ModuleFinder} returned by {@link #of ModuleFinder.of}, that
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
    75
 * two or more modules with the same name are found in a directory.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
 * When an error is detected then these methods throw {@link FindException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
 * FindException} with an appropriate {@link Throwable#getCause cause}.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
 * The behavior of a {@code ModuleFinder} after a {@code FindException} is
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
 * thrown is undefined. For example, invoking {@code find} after an exception
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
 * is thrown may or may not scan the same modules that lead to the exception.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
 * It is recommended that a module finder be discarded after an exception is
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
 * thrown. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
 * <p> A {@code ModuleFinder} is not required to be thread safe. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
 * @since 9
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    87
 * @spec JPMS
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
public interface ModuleFinder {
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
     * Finds a reference to a module of a given name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
     * <p> A {@code ModuleFinder} provides a consistent view of the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
     * modules that it locates. If {@code find} is invoked several times to
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
     * locate the same module (by name) then it will return the same result
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
     * each time. If a module is located then it is guaranteed to be a member
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
     * of the set of modules returned by the {@link #findAll() findAll}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
     * method. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
     * @param  name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
     *         The name of the module to find
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
     * @return A reference to a module with the given name or an empty
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
     *         {@code Optional} if not found
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
     * @throws FindException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
     *         If an error occurs finding the module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
     * @throws SecurityException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
     *         If denied by the security manager
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
    Optional<ModuleReference> find(String name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
     * Returns the set of all module references that this finder can locate.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
     * <p> A {@code ModuleFinder} provides a consistent view of the modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
     * that it locates. If {@link #findAll() findAll} is invoked several times
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
     * then it will return the same (equals) result each time. For each {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
     * ModuleReference} element in the returned set then it is guaranteed that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
     * {@link #find find} will locate the {@code ModuleReference} if invoked
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
     * to find that module. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
     * @apiNote This is important to have for methods such as {@link
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   127
     * Configuration#resolveAndBind resolveAndBind} that need to scan the
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   128
     * module path to find modules that provide a specific service.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
     * @return The set of all module references that this finder locates
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
     * @throws FindException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
     *         If an error occurs finding all modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
     * @throws SecurityException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
     *         If denied by the security manager
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
    Set<ModuleReference> findAll();
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
     * Returns a module finder that locates the <em>system modules</em>. The
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   142
     * system modules are the modules in the Java run-time image.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
     * The module finder will always find {@code java.base}.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
     * <p> If there is a security manager set then its {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
     * SecurityManager#checkPermission(Permission) checkPermission} method is
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   147
     * invoked to check that the caller has been granted
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   148
     * {@link RuntimePermission RuntimePermission("accessSystemModules")}
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   149
     * to access the system modules. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
     * @return A {@code ModuleFinder} that locates the system modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
     * @throws SecurityException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
     *         If denied by the security manager
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
    static ModuleFinder ofSystem() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
        SecurityManager sm = System.getSecurityManager();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
        if (sm != null) {
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   159
            sm.checkPermission(new RuntimePermission("accessSystemModules"));
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   160
            PrivilegedAction<ModuleFinder> pa = ModuleFinder::privilegedOfSystem;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   161
            return AccessController.doPrivileged(pa);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
        } else {
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   163
            return privilegedOfSystem();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
        }
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   165
    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   167
    /**
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   168
     * Returns a module finder that locates the system modules. This method
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   169
     * assumes it has permissions to access the runtime image.
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   170
     */
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   171
    private static ModuleFinder privilegedOfSystem() {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   172
        String home = System.getProperty("java.home");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
        Path modules = Paths.get(home, "lib", "modules");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
        if (Files.isRegularFile(modules)) {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   175
            return SystemModuleFinder.getInstance();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
        } else {
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   177
            Path dir = Paths.get(home, "modules");
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   178
            if (Files.isDirectory(dir)) {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   179
                return privilegedOf(ModuleBootstrap.patcher(), dir);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
                throw new InternalError("Unable to detect the run-time image");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
        }
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
    /**
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   187
     * Returns a module finder that locates the system modules in an exploded
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   188
     * image. The image may be patched.
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   189
     */
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   190
    private static ModuleFinder privilegedOf(ModulePatcher patcher, Path dir) {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   191
        ModuleFinder finder = ModulePath.of(patcher, dir);
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   192
        return new ModuleFinder() {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   193
            @Override
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   194
            public Optional<ModuleReference> find(String name) {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   195
                PrivilegedAction<Optional<ModuleReference>> pa = () -> finder.find(name);
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   196
                return AccessController.doPrivileged(pa);
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   197
            }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   198
            @Override
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   199
            public Set<ModuleReference> findAll() {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   200
                PrivilegedAction<Set<ModuleReference>> pa = finder::findAll;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   201
                return AccessController.doPrivileged(pa);
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   202
            }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   203
        };
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   204
    }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   205
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   206
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
     * Returns a module finder that locates modules on the file system by
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
     * searching a sequence of directories and/or packaged modules.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
     * Each element in the given array is one of:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
     * <ol>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
     *     <li><p> A path to a directory of modules.</p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   213
     *     <li><p> A path to the <em>top-level</em> directory of an
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   214
     *         <em>exploded module</em>. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   215
     *     <li><p> A path to a <em>packaged module</em>. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   216
     * </ol>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
     * The module finder locates modules by searching each directory, exploded
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
     * module, or packaged module in array index order. It finds the first
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
     * occurrence of a module with a given name and ignores other modules of
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
     * that name that appear later in the sequence.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
     * <p> If an element is a path to a directory of modules then each entry in
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
     * the directory is a packaged module or the top-level directory of an
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   225
     * exploded module. It is an error if a directory contains more than one
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   226
     * module with the same name. If an element is a path to a directory, and
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   227
     * that directory contains a file named {@code module-info.class}, then the
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
     * directory is treated as an exploded module rather than a directory of
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
     * modules. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
     *
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   231
     * <p id="automatic-modules"> The module finder returned by this method
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   232
     * supports modules packaged as JAR files. A JAR file with a {@code
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   233
     * module-info.class} in its top-level directory, or in a versioned entry
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   234
     * in a {@linkplain java.util.jar.JarFile#isMultiRelease() multi-release}
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   235
     * JAR file, is a modular JAR file and thus defines an <em>explicit</em>
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   236
     * module. A JAR file that does not have a {@code module-info.class} in its
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   237
     * top-level directory defines an <em>automatic module</em>, as follows:
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   238
     * </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
     * <ul>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
     *     <li><p> The module {@link ModuleDescriptor#name() name}, and {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
     *     ModuleDescriptor#version() version} if applicable, is derived from
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
     *     the file name of the JAR file as follows: </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
     *     <ul>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
     *         <li><p> The {@code .jar} suffix is removed. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
     *         <li><p> If the name matches the regular expression {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
     *         "-(\\d+(\\.|$))"} then the module name will be derived from the
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41482
diff changeset
   252
     *         subsequence preceding the hyphen of the first occurrence. The
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
     *         subsequence after the hyphen is parsed as a {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   254
     *         ModuleDescriptor.Version} and ignored if it cannot be parsed as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   255
     *         a {@code Version}. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   256
     *
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   257
     *         <li><p> All non-alphanumeric characters ({@code [^A-Za-z0-9]})
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   258
     *         in the module name are replaced with a dot ({@code "."}), all
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   259
     *         repeating dots are replaced with one dot, and all leading and
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   260
     *         trailing dots are removed. </p></li>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   261
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   262
     *         <li><p> As an example, a JAR file named {@code foo-bar.jar} will
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
     *         derive a module name {@code foo.bar} and no version. A JAR file
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   264
     *         named {@code foo-bar-1.2.3-SNAPSHOT.jar} will derive a module
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   265
     *         name {@code foo.bar} and {@code 1.2.3-SNAPSHOT} as the version.
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   266
     *         </p></li>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
     *     </ul></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   270
     *     <li><p> The set of packages in the module is derived from the
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   271
     *     non-directory entries in the JAR file that have names ending in
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   272
     *     "{@code .class}". A candidate package name is derived from the name
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   273
     *     using the characters up to, but not including, the last forward slash.
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   274
     *     All remaining forward slashes are replaced with dot ({@code "."}). If
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   275
     *     the resulting string is a legal package name then it is assumed to be
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   276
     *     a package name. For example, if the JAR file contains the entry
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   277
     *     "{@code p/q/Foo.class}" then the package name derived is
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   278
     *     "{@code p.q}".</p></li>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   279
     *
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41482
diff changeset
   280
     *     <li><p> The contents of entries starting with {@code
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   281
     *     META-INF/services/} are assumed to be service configuration files
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41482
diff changeset
   282
     *     (see {@link java.util.ServiceLoader}). If the name of a file
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   283
     *     (that follows {@code META-INF/services/}) is a legal class name
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   284
     *     then it is assumed to be the fully-qualified class name of a service
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   285
     *     type. The entries in the file are assumed to be the fully-qualified
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   286
     *     class names of provider classes. </p></li>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   287
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   288
     *     <li><p> If the JAR file has a {@code Main-Class} attribute in its
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   289
     *     main manifest then its value is the module {@link
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   290
     *     ModuleDescriptor#mainClass() main class}. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   291
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   292
     * </ul>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   293
     *
39050
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 38457
diff changeset
   294
     * <p> If a {@code ModuleDescriptor} cannot be created (by means of the
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 38457
diff changeset
   295
     * {@link ModuleDescriptor.Builder ModuleDescriptor.Builder} API) for an
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   296
     * automatic module then {@code FindException} is thrown. This can arise
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   297
     * when a legal module name cannot be derived from the file name of the JAR
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   298
     * file, where the JAR file contains a {@code .class} in the top-level
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   299
     * directory of the JAR file, where an entry in a service configuration
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   300
     * file is not a legal class name or its package name is not in the set of
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   301
     * packages derived for the module, or where the module main class is not
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   302
     * a legal class name or its package is not in the module. </p>
39050
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 38457
diff changeset
   303
     *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   304
     * <p> In addition to JAR files, an implementation may also support modules
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   305
     * that are packaged in other implementation specific module formats. If
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   306
     * an element in the array specified to this method is a path to a directory
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   307
     * of modules then entries in the directory that not recognized as modules
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   308
     * are ignored. If an element in the array is a path to a packaged module
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   309
     * that is not recognized then a {@code FindException} is thrown when the
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   310
     * file is encountered. Paths to files that do not exist are always ignored.
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   311
     * </p>
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   312
     *
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   313
     * <p> As with automatic modules, the contents of a packaged or exploded
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   314
     * module may need to be <em>scanned</em> in order to determine the packages
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   315
     * in the module. Whether {@linkplain java.nio.file.Files#isHidden(Path)
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   316
     * hidden files} are ignored or not is implementation specific and therefore
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   317
     * not specified. If a {@code .class} file (other than {@code
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41482
diff changeset
   318
     * module-info.class}) is found in the top-level directory then it is
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41482
diff changeset
   319
     * assumed to be a class in the unnamed package and so {@code FindException}
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41482
diff changeset
   320
     * is thrown. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   321
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   322
     * <p> Finders created by this method are lazy and do not eagerly check
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   323
     * that the given file paths are directories or packaged modules.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   324
     * Consequently, the {@code find} or {@code findAll} methods will only
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   325
     * fail if invoking these methods results in searching a directory or
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   326
     * packaged module and an error is encountered. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   327
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   328
     * @param entries
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   329
     *        A possibly-empty array of paths to directories of modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   330
     *        or paths to packaged or exploded modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   331
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   332
     * @return A {@code ModuleFinder} that locates modules on the file system
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   333
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   334
    static ModuleFinder of(Path... entries) {
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   335
        // special case zero entries
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   336
        if (entries.length == 0) {
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   337
            return new ModuleFinder() {
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   338
                @Override
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   339
                public Optional<ModuleReference> find(String name) {
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   340
                    Objects.requireNonNull(name);
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   341
                    return Optional.empty();
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   342
                }
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   343
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   344
                @Override
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   345
                public Set<ModuleReference> findAll() {
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   346
                    return Collections.emptySet();
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   347
                }
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   348
            };
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   349
        }
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   350
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   351
        return ModulePath.of(entries);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   352
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   353
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   354
    /**
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   355
     * Returns a module finder that is composed from a sequence of zero or more
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   356
     * module finders. The {@link #find(String) find} method of the resulting
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   357
     * module finder will locate a module by invoking the {@code find} method
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   358
     * of each module finder, in array index order, until either the module is
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   359
     * found or all module finders have been searched. The {@link #findAll()
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   360
     * findAll} method of the resulting module finder will return a set of
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   361
     * modules that includes all modules located by the first module finder.
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   362
     * The set of modules will include all modules located by the second or
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   363
     * subsequent module finder that are not located by previous module finders
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   364
     * in the sequence.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   365
     *
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   366
     * <p> When locating modules then any exceptions or errors thrown by the
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   367
     * {@code find} or {@code findAll} methods of the underlying module finders
41482
05e75e5f3afb 8134373: use collections convenience factories in the JDK
smarks
parents: 39050
diff changeset
   368
     * will be propagated to the caller of the resulting module finder's
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   369
     * {@code find} or {@code findAll} methods. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   370
     *
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   371
     * @param finders
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   372
     *        The array of module finders
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   373
     *
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   374
     * @return A {@code ModuleFinder} that composes a sequence of module finders
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   375
     */
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   376
    static ModuleFinder compose(ModuleFinder... finders) {
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41482
diff changeset
   377
        // copy the list and check for nulls
41482
05e75e5f3afb 8134373: use collections convenience factories in the JDK
smarks
parents: 39050
diff changeset
   378
        final List<ModuleFinder> finderList = List.of(finders);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   379
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   380
        return new ModuleFinder() {
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   381
            private final Map<String, ModuleReference> nameToModule = new HashMap<>();
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   382
            private Set<ModuleReference> allModules;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   383
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   384
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   385
            public Optional<ModuleReference> find(String name) {
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   386
                // cached?
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   387
                ModuleReference mref = nameToModule.get(name);
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   388
                if (mref != null)
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   389
                    return Optional.of(mref);
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   390
                Optional<ModuleReference> omref = finderList.stream()
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   391
                        .map(f -> f.find(name))
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   392
                        .flatMap(Optional::stream)
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   393
                        .findFirst();
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   394
                omref.ifPresent(m -> nameToModule.put(name, m));
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   395
                return omref;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   396
            }
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   397
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   398
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   399
            public Set<ModuleReference> findAll() {
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   400
                if (allModules != null)
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   401
                    return allModules;
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   402
                // seed with modules already found
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   403
                Set<ModuleReference> result = new HashSet<>(nameToModule.values());
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   404
                finderList.stream()
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   405
                          .flatMap(f -> f.findAll().stream())
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   406
                          .forEach(mref -> {
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   407
                              String name = mref.descriptor().name();
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   408
                              if (nameToModule.putIfAbsent(name, mref) == null) {
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   409
                                  result.add(mref);
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   410
                              }
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   411
                          });
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   412
                allModules = Collections.unmodifiableSet(result);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   413
                return allModules;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   414
            }
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   418
}