jdk/src/java.base/share/classes/java/lang/module/ModuleFinder.java
author alanb
Fri, 17 Jun 2016 08:41:39 +0100
changeset 39050 9de41b79ec7e
parent 38457 3d019217e322
child 41482 05e75e5f3afb
permissions -rw-r--r--
8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class 8159248: ModuleFinder.of not clear that FindException thrown if module descriptor cannot be derived for automatic module Reviewed-by: chegar, mchung
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) 2014, 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 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.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.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.nio.file.Paths;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.security.AccessController;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.security.Permission;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.security.PrivilegedAction;
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
    36
import java.util.Arrays;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.util.Collections;
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
    38
import java.util.HashMap;
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
    39
import java.util.HashSet;
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
    40
import java.util.List;
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
    41
import java.util.Map;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.util.Objects;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.util.Optional;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
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
    45
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 36511
diff changeset
    46
import sun.security.action.GetPropertyAction;
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
 * <a href="Configuration.html#resolution">resolution</a> or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
 * <a href="Configuration.html#servicebinding">service binding</a>.
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
public interface ModuleFinder {
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
     * Finds a reference to a module of a given name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
     * <p> A {@code ModuleFinder} provides a consistent view of the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
     * modules that it locates. If {@code find} is invoked several times to
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
     * locate the same module (by name) then it will return the same result
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
     * each time. If a module is located then it is guaranteed to be a member
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
     * of the set of modules returned by the {@link #findAll() findAll}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
     * method. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
     * @param  name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
     *         The name of the module to find
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
     * @return A reference to a module with the given name or an empty
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
     *         {@code Optional} if not found
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
     * @throws FindException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
     *         If an error occurs finding the module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
     * @throws SecurityException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
     *         If denied by the security manager
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
    Optional<ModuleReference> find(String name);
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
     * Returns the set of all module references that this finder can locate.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
     * <p> A {@code ModuleFinder} provides a consistent view of the modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
     * that it locates. If {@link #findAll() findAll} is invoked several times
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
     * then it will return the same (equals) result each time. For each {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
     * ModuleReference} element in the returned set then it is guaranteed that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
     * {@link #find find} will locate the {@code ModuleReference} if invoked
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
     * to find that module. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
     * @apiNote This is important to have for methods such as {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
     * Configuration#resolveRequiresAndUses resolveRequiresAndUses} that need
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
     * to scan the module path to find modules that provide a specific service.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
     * @return The set of all module references that this finder locates
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
     * @throws FindException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
     *         If an error occurs finding all modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
     * @throws SecurityException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
     *         If denied by the security manager
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
    Set<ModuleReference> findAll();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
     * Returns a module finder that locates the <em>system modules</em>. The
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
     * system modules are typically linked into the Java run-time image.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
     * The module finder will always find {@code java.base}.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
     * <p> If there is a security manager set then its {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
     * SecurityManager#checkPermission(Permission) checkPermission} method is
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
     * invoked to check that the caller has been granted {@link FilePermission}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
     * to recursively read the directory that is the value of the system
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
     * property {@code java.home}. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
     * @return A {@code ModuleFinder} that locates the system modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
     * @throws SecurityException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
     *         If denied by the security manager
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
    static ModuleFinder ofSystem() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
        String home;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
        SecurityManager sm = System.getSecurityManager();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
        if (sm != null) {
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 36511
diff changeset
   160
            PrivilegedAction<String> pa = new GetPropertyAction("java.home");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
            home = AccessController.doPrivileged(pa);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
            Permission p = new FilePermission(home + File.separator + "-", "read");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
            sm.checkPermission(p);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
            home = System.getProperty("java.home");
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
        Path modules = Paths.get(home, "lib", "modules");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
        if (Files.isRegularFile(modules)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
            return new SystemModuleFinder();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
            Path mlib = Paths.get(home, "modules");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
            if (Files.isDirectory(mlib)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
                return of(mlib);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
                throw new InternalError("Unable to detect the run-time image");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
     * Returns a module finder that locates modules on the file system by
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
     * searching a sequence of directories and/or packaged modules.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
     * Each element in the given array is one of:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
     * <ol>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
     *     <li><p> A path to a directory of modules.</p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
     *     <li><p> A path to the <em>top-level</em> directory of an
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
     *         <em>exploded module</em>. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
     *     <li><p> A path to a <em>packaged module</em>. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
     * </ol>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
     * The module finder locates modules by searching each directory, exploded
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   194
     * module, or packaged module in array index order. It finds the first
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
     * occurrence of a module with a given name and ignores other modules of
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
     * that name that appear later in the sequence.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   197
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
     * <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
   199
     * the directory is a packaged module or the top-level directory of an
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
     * exploded module. The module finder's {@link #find(String) find} or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
     * {@link #findAll() findAll} methods throw {@link FindException} if a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
     * directory containing more than one module with the same name is
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
     * encountered. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
     * <p> If an element in the array is a path to a directory, and that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
     * directory contains a file named {@code module-info.class}, then the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
     * directory is treated as an exploded module rather than a directory of
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
     * modules. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
     * <p> The module finder returned by this method supports modules that are
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
     * packaged as JAR files. A JAR file with a {@code module-info.class} in
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   212
     * the top-level directory of the JAR file (or overridden by a versioned
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   213
     * entry in a {@link java.util.jar.JarFile#isMultiRelease() multi-release}
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   214
     * JAR file) is a modular JAR and is an <em>explicit module</em>.
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   215
     * A JAR file that does not have a {@code module-info.class} in the
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   216
     * top-level directory is an {@link ModuleDescriptor#isAutomatic automatic}
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   217
     * module. The {@link ModuleDescriptor} for an automatic module is created as
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   218
     * follows:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
     * <ul>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
     *     <li><p> The module {@link ModuleDescriptor#name() name}, and {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
     *     ModuleDescriptor#version() version} if applicable, is derived from
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
     *     the file name of the JAR file as follows: </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
     *     <ul>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
     *         <li><p> The {@code .jar} suffix is removed. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
     *         <li><p> If the name matches the regular expression {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
     *         "-(\\d+(\\.|$))"} then the module name will be derived from the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
     *         subsequence proceeding the hyphen of the first occurrence. The
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
     *         subsequence after the hyphen is parsed as a {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
     *         ModuleDescriptor.Version} and ignored if it cannot be parsed as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
     *         a {@code Version}. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
     *         <li><p> For the module name, then all non-alphanumeric
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
     *         characters ({@code [^A-Za-z0-9])} are replaced with a dot
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
     *         ({@code "."}), all repeating dots are replaced with one dot,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
     *         and all leading and trailing dots are removed. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
     *         <li><p> As an example, a JAR file named {@code foo-bar.jar} will
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
     *         derive a module name {@code foo.bar} and no version. A JAR file
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
     *         named {@code foo-1.2.3-SNAPSHOT.jar} will derive a module name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
     *         {@code foo} and {@code 1.2.3-SNAPSHOT} as the version. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
     *     </ul></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
     *     <li><p> It {@link ModuleDescriptor#requires() requires} {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
     *     java.base}. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
     *     <li><p> All entries in the JAR file with names ending with {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
     *     .class} are assumed to be class files where the name corresponds
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   254
     *     to the fully qualified name of the class. The packages of all
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   255
     *     classes are {@link ModuleDescriptor#exports() exported}. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   256
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   257
     *     <li><p> The contents of all entries starting with {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   258
     *     META-INF/services/} are assumed to be service configuration files
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   259
     *     (see {@link java.util.ServiceLoader}). The name of the file
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   260
     *     (that follows {@code META-INF/services/}) is assumed to be the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   261
     *     fully-qualified binary name of a service type. The entries in the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   262
     *     file are assumed to be the fully-qualified binary names of
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
     *     provider classes. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   264
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   265
     *     <li><p> If the JAR file has a {@code Main-Class} attribute in its
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
     *     main manifest then its value is the {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
     *     ModuleDescriptor#mainClass() main class}. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
     * </ul>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   270
     *
39050
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 38457
diff changeset
   271
     * <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
   272
     * {@link ModuleDescriptor.Builder ModuleDescriptor.Builder} API) for an
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 38457
diff changeset
   273
     * automatic module then {@code FindException} is thrown. This can arise,
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 38457
diff changeset
   274
     * for example, when a legal Java identifier name cannot be derived from
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 38457
diff changeset
   275
     * the file name of the JAR file or where a package name derived from an
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 38457
diff changeset
   276
     * entry ending with {@code .class} is not a legal Java identifier. </p>
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 38457
diff changeset
   277
     *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   278
     * <p> In addition to JAR files, an implementation may also support modules
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   279
     * that are packaged in other implementation specific module formats. When
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   280
     * a file is encountered that is not recognized as a packaged module then
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   281
     * {@code FindException} is thrown. An implementation may choose to ignore
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   282
     * some files, {@link java.nio.file.Files#isHidden hidden} files for
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   283
     * example. Paths to files that do not exist are always ignored. </p>
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   284
     *
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   285
     * <p> As with automatic modules, the contents of a packaged or exploded
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   286
     * module may need to be <em>scanned</em> in order to determine the packages
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   287
     * in the module. If a {@code .class} file that corresponds to a class in an
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   288
     * unnamed package is encountered then {@code FindException} is thrown. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   289
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   290
     * <p> Finders created by this method are lazy and do not eagerly check
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   291
     * that the given file paths are directories or packaged modules.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   292
     * Consequently, the {@code find} or {@code findAll} methods will only
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   293
     * fail if invoking these methods results in searching a directory or
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   294
     * packaged module and an error is encountered. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   295
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   296
     * @param entries
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   297
     *        A possibly-empty array of paths to directories of modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   298
     *        or paths to packaged or exploded modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   299
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   300
     * @return A {@code ModuleFinder} that locates modules on the file system
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   301
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   302
    static ModuleFinder of(Path... entries) {
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   303
        // special case zero entries
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   304
        if (entries.length == 0) {
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   305
            return new ModuleFinder() {
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   306
                @Override
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   307
                public Optional<ModuleReference> find(String name) {
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   308
                    Objects.requireNonNull(name);
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   309
                    return Optional.empty();
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   310
                }
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   311
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   312
                @Override
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   313
                public Set<ModuleReference> findAll() {
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   314
                    return Collections.emptySet();
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   315
                }
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   316
            };
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   317
        }
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   318
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   319
        return new ModulePath(entries);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   320
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   321
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   322
    /**
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   323
     * 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
   324
     * module finders. The {@link #find(String) find} method of the resulting
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   325
     * module finder will locate a module by invoking the {@code find} method
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   326
     * 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
   327
     * found or all module finders have been searched. The {@link #findAll()
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   328
     * findAll} method of the resulting module finder will return a set of
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   329
     * modules that includes all modules located by the first module finder.
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   330
     * 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
   331
     * subsequent module finder that are not located by previous module finders
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   332
     * in the sequence.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   333
     *
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   334
     * <p> When locating modules then any exceptions or errors thrown by the
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   335
     * {@code find} or {@code findAll} methods of the underlying module finders
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   336
     * will be propogated to the caller of the resulting module finder's
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   337
     * {@code find} or {@code findAll} methods. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   338
     *
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   339
     * @param finders
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   340
     *        The array of module finders
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   341
     *
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   342
     * @return A {@code ModuleFinder} that composes a sequence of module finders
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   343
     */
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   344
    static ModuleFinder compose(ModuleFinder... finders) {
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   345
        final List<ModuleFinder> finderList = Arrays.asList(finders);
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   346
        finderList.forEach(Objects::requireNonNull);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   347
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   348
        return new ModuleFinder() {
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   349
            private final Map<String, ModuleReference> nameToModule = new HashMap<>();
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   350
            private Set<ModuleReference> allModules;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   351
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   352
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   353
            public Optional<ModuleReference> find(String name) {
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   354
                // cached?
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   355
                ModuleReference mref = nameToModule.get(name);
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   356
                if (mref != null)
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   357
                    return Optional.of(mref);
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   358
                Optional<ModuleReference> omref = finderList.stream()
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   359
                        .map(f -> f.find(name))
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   360
                        .flatMap(Optional::stream)
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   361
                        .findFirst();
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   362
                omref.ifPresent(m -> nameToModule.put(name, m));
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   363
                return omref;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   364
            }
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   365
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   366
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   367
            public Set<ModuleReference> findAll() {
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   368
                if (allModules != null)
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   369
                    return allModules;
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   370
                // seed with modules already found
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   371
                Set<ModuleReference> result = new HashSet<>(nameToModule.values());
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   372
                finderList.stream()
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   373
                          .flatMap(f -> f.findAll().stream())
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   374
                          .forEach(mref -> {
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   375
                              String name = mref.descriptor().name();
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   376
                              if (nameToModule.putIfAbsent(name, mref) == null) {
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   377
                                  result.add(mref);
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   378
                              }
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   379
                          });
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37780
diff changeset
   380
                allModules = Collections.unmodifiableSet(result);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   381
                return allModules;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   382
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   383
        };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   384
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   385
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   386
}