jdk/src/java.base/share/classes/java/lang/module/Configuration.java
author jjg
Thu, 24 Aug 2017 16:52:21 -0700
changeset 47004 b7e72fc752c9
parent 46096 62c77b334012
permissions -rw-r--r--
8186684: Fix broken links in java.base API docs Reviewed-by: sherman, martin, mchung, bpb, lancea
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: 42338
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
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    28
import java.io.PrintStream;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
    29
import java.util.ArrayDeque;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
    30
import java.util.ArrayList;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.util.Collection;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.util.Collections;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
    33
import java.util.Deque;
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
    34
import java.util.HashMap;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
    35
import java.util.HashSet;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
    36
import java.util.List;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.util.Map;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
    38
import java.util.Map.Entry;
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;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.util.stream.Collectors;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
    43
import java.util.stream.Stream;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
    45
import jdk.internal.module.ModuleReferenceImpl;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
    46
import jdk.internal.module.ModuleTarget;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
    47
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
/**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
    49
 * A configuration that is the result of <a href="package-summary.html#resolution">
47004
b7e72fc752c9 8186684: Fix broken links in java.base API docs
jjg
parents: 46096
diff changeset
    50
 * resolution</a> or resolution with
b7e72fc752c9 8186684: Fix broken links in java.base API docs
jjg
parents: 46096
diff changeset
    51
 * <a href="{@docRoot}/java/lang/module/Configuration.html#service-binding">service binding</a>.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
 *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
    53
 * <p> A configuration encapsulates the <em>readability graph</em> that is the
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
    54
 * output of resolution. A readability graph is a directed graph whose vertices
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
    55
 * are of type {@link ResolvedModule} and the edges represent the readability
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
    56
 * amongst the modules. {@code Configuration} defines the {@link #modules()
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
    57
 * modules()} method to get the set of resolved modules in the graph. {@code
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
    58
 * ResolvedModule} defines the {@link ResolvedModule#reads() reads()} method to
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
    59
 * get the set of modules that a resolved module reads. The modules that are
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
    60
 * read may be in the same configuration or may be in {@link #parents() parent}
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
    61
 * configurations. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
 *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
    63
 * <p> Configuration defines the {@link #resolve(ModuleFinder,List,ModuleFinder,Collection)
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
    64
 * resolve} method to resolve a collection of root modules, and the {@link
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
    65
 * #resolveAndBind(ModuleFinder,List,ModuleFinder,Collection) resolveAndBind}
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
    66
 * method to do resolution with service binding. There are instance and
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
    67
 * static variants of both methods. The instance methods create a configuration
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
    68
 * with the receiver as the parent configuration. The static methods are for
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
    69
 * more advanced cases where there can be more than one parent configuration. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
 *
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
    71
 * <p> Each {@link java.lang.ModuleLayer layer} of modules in the Java virtual
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
    72
 * machine is created from a configuration. The configuration for the {@link
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
    73
 * java.lang.ModuleLayer#boot() boot} layer is obtained by invoking {@code
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
    74
 * ModuleLayer.boot().configuration()}. The configuration for the boot layer
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
    75
 * will often be the parent when creating new configurations. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
 * <h3> Example </h3>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
 *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
    79
 * <p> The following example uses the {@link
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
    80
 * #resolve(ModuleFinder,ModuleFinder,Collection) resolve} method to resolve a
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
    81
 * module named <em>myapp</em> with the configuration for the boot layer as the
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
    82
 * parent configuration. It prints the name of each resolved module and the
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
    83
 * names of the modules that each module reads. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
 * <pre>{@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
 *    ModuleFinder finder = ModuleFinder.of(dir1, dir2, dir3);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
 *
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
    88
 *    Configuration parent = ModuleLayer.boot().configuration();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
 *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
    90
 *    Configuration cf = parent.resolve(finder, ModuleFinder.of(), Set.of("myapp"));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
 *    cf.modules().forEach(m -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
 *        System.out.format("%s -> %s%n",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
 *            m.name(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
 *            m.reads().stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
 *                .map(ResolvedModule::name)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
 *                .collect(Collectors.joining(", ")));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
 *    });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
 * }</pre>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
 * @since 9
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   101
 * @spec JPMS
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   102
 * @see java.lang.ModuleLayer
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
public final class Configuration {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
    // @see Configuration#empty()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
    private static final Configuration EMPTY_CONFIGURATION = new Configuration();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   109
    // parent configurations, in search order
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   110
    private final List<Configuration> parents;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
    private final Map<ResolvedModule, Set<ResolvedModule>> graph;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
    private final Set<ResolvedModule> modules;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
    private final Map<String, ResolvedModule> nameToModule;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   116
    // constraint on target platform
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   117
    private final String targetPlatform;
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   118
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   119
    String targetPlatform() { return targetPlatform; }
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   120
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
    private Configuration() {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   122
        this.parents = Collections.emptyList();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
        this.graph = Collections.emptyMap();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
        this.modules = Collections.emptySet();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
        this.nameToModule = Collections.emptyMap();
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   126
        this.targetPlatform = null;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   129
    private Configuration(List<Configuration> parents, Resolver resolver) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   130
        Map<ResolvedModule, Set<ResolvedModule>> g = resolver.finish(this);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   132
        @SuppressWarnings(value = {"rawtypes", "unchecked"})
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   133
        Entry<String, ResolvedModule>[] nameEntries
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   134
            = (Entry<String, ResolvedModule>[])new Entry[g.size()];
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   135
        ResolvedModule[] moduleArray = new ResolvedModule[g.size()];
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   136
        int i = 0;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   137
        for (ResolvedModule resolvedModule : g.keySet()) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   138
            moduleArray[i] = resolvedModule;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   139
            nameEntries[i] = Map.entry(resolvedModule.name(), resolvedModule);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   140
            i++;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   143
        this.parents = Collections.unmodifiableList(parents);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   144
        this.graph = g;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   145
        this.modules = Set.of(moduleArray);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   146
        this.nameToModule = Map.ofEntries(nameEntries);
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   147
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   148
        this.targetPlatform = resolver.targetPlatform();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
    /**
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   152
     * Creates the Configuration for the boot layer from a pre-generated
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   153
     * readability graph.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   154
     *
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   155
     * @apiNote This method is coded for startup performance.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   156
     */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   157
    Configuration(ModuleFinder finder, Map<String, Set<String>> map) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   158
        int moduleCount = map.size();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   159
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   160
        // create map of name -> ResolvedModule
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   161
        @SuppressWarnings(value = {"rawtypes", "unchecked"})
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   162
        Entry<String, ResolvedModule>[] nameEntries
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   163
            = (Entry<String, ResolvedModule>[])new Entry[moduleCount];
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   164
        ResolvedModule[] moduleArray = new ResolvedModule[moduleCount];
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   165
        String targetPlatform = null;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   166
        int i = 0;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   167
        for (String name : map.keySet()) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   168
            ModuleReference mref = finder.find(name).orElse(null);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   169
            assert mref != null;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   170
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   171
            if (targetPlatform == null && mref instanceof ModuleReferenceImpl) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   172
                ModuleTarget target = ((ModuleReferenceImpl)mref).moduleTarget();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   173
                if (target != null) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   174
                    targetPlatform = target.targetPlatform();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   175
                }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   176
            }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   177
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   178
            ResolvedModule resolvedModule = new ResolvedModule(this, mref);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   179
            moduleArray[i] = resolvedModule;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   180
            nameEntries[i] = Map.entry(name, resolvedModule);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   181
            i++;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   182
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   183
        Map<String, ResolvedModule> nameToModule = Map.ofEntries(nameEntries);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   184
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   185
        // create entries for readability graph
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   186
        @SuppressWarnings(value = {"rawtypes", "unchecked"})
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   187
        Entry<ResolvedModule, Set<ResolvedModule>>[] moduleEntries
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   188
            = (Entry<ResolvedModule, Set<ResolvedModule>>[])new Entry[moduleCount];
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   189
        i = 0;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   190
        for (ResolvedModule resolvedModule : moduleArray) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   191
            Set<String> names = map.get(resolvedModule.name());
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   192
            ResolvedModule[] readsArray = new ResolvedModule[names.size()];
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   193
            int j = 0;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   194
            for (String name : names) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   195
                readsArray[j++] = nameToModule.get(name);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   196
            }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   197
            moduleEntries[i++] = Map.entry(resolvedModule, Set.of(readsArray));
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   198
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   199
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   200
        this.parents = List.of(empty());
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   201
        this.graph = Map.ofEntries(moduleEntries);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   202
        this.modules = Set.of(moduleArray);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   203
        this.nameToModule = nameToModule;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   204
        this.targetPlatform = targetPlatform;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   205
    }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   206
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   207
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
     * Resolves a collection of root modules, with this configuration as its
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   209
     * parent, to create a new configuration. This method works exactly as
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   210
     * specified by the static {@link
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   211
     * #resolve(ModuleFinder,List,ModuleFinder,Collection) resolve}
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   212
     * method when invoked with this configuration as the parent. In other words,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   213
     * if this configuration is {@code cf} then this method is equivalent to
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   214
     * invoking:
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   215
     * <pre> {@code
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   216
     *     Configuration.resolve(before, List.of(cf), after, roots);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   217
     * }</pre>
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   218
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   219
     * @param  before
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   220
     *         The <em>before</em> module finder to find modules
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   221
     * @param  after
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   222
     *         The <em>after</em> module finder to locate modules when not
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   223
     *         located by the {@code before} module finder or in parent
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   224
     *         configurations
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   225
     * @param  roots
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   226
     *         The possibly-empty collection of module names of the modules
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   227
     *         to resolve
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   228
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   229
     * @return The configuration that is the result of resolving the given
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   230
     *         root modules
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   231
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   232
     * @throws FindException
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   233
     *         If resolution fails for any of the observability-related reasons
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   234
     *         specified by the static {@code resolve} method
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   235
     * @throws ResolutionException
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   236
     *         If resolution fails any of the consistency checks specified by
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   237
     *         the static {@code resolve} method
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   238
     * @throws SecurityException
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   239
     *         If locating a module is denied by the security manager
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   240
     */
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   241
    public Configuration resolve(ModuleFinder before,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   242
                                 ModuleFinder after,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   243
                                 Collection<String> roots)
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   244
    {
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   245
        return resolve(before, List.of(this), after, roots);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   246
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   247
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   248
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   249
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   250
     * Resolves a collection of root modules, with service binding, and with
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   251
     * this configuration as its parent, to create a new configuration.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   252
     * This method works exactly as specified by the static {@link
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   253
     * #resolveAndBind(ModuleFinder,List,ModuleFinder,Collection)
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   254
     * resolveAndBind} method when invoked with this configuration
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   255
     * as the parent. In other words, if this configuration is {@code cf} then
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   256
     * this method is equivalent to invoking:
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   257
     * <pre> {@code
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   258
     *     Configuration.resolveAndBind(before, List.of(cf), after, roots);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   259
     * }</pre>
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   260
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   261
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   262
     * @param  before
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   263
     *         The <em>before</em> module finder to find modules
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   264
     * @param  after
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   265
     *         The <em>after</em> module finder to locate modules when not
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   266
     *         located by the {@code before} module finder or in parent
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   267
     *         configurations
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   268
     * @param  roots
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   269
     *         The possibly-empty collection of module names of the modules
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   270
     *         to resolve
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   271
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   272
     * @return The configuration that is the result of resolving, with service
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   273
     *         binding, the given root modules
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   274
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   275
     * @throws FindException
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   276
     *         If resolution fails for any of the observability-related reasons
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   277
     *         specified by the static {@code resolve} method
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   278
     * @throws ResolutionException
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   279
     *         If resolution fails any of the consistency checks specified by
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   280
     *         the static {@code resolve} method
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   281
     * @throws SecurityException
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   282
     *         If locating a module is denied by the security manager
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   283
     */
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   284
    public Configuration resolveAndBind(ModuleFinder before,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   285
                                        ModuleFinder after,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   286
                                        Collection<String> roots)
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   287
    {
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   288
        return resolveAndBind(before, List.of(this), after, roots);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   289
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   290
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   291
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   292
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   293
     * Resolves a collection of root modules, with service binding, and with
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   294
     * the empty configuration as its parent.
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   295
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   296
     * This method is used to create the configuration for the boot layer.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   297
     */
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   298
    static Configuration resolveAndBind(ModuleFinder finder,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   299
                                        Collection<String> roots,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   300
                                        PrintStream traceOutput)
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   301
    {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   302
        List<Configuration> parents = List.of(empty());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   303
        Resolver resolver = new Resolver(finder, parents, ModuleFinder.of(), traceOutput);
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   304
        resolver.resolve(roots).bind();
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   305
        return new Configuration(parents, resolver);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   306
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   307
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   308
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   309
     * Resolves a collection of root modules to create a configuration.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   310
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   311
     * <p> Each root module is located using the given {@code before} module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   312
     * finder. If a module is not found then it is located in the parent
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   313
     * configuration as if by invoking the {@link #findModule(String)
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   314
     * findModule} method on each parent in iteration order. If not found then
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   315
     * the module is located using the given {@code after} module finder. The
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   316
     * same search order is used to locate transitive dependences. Root modules
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   317
     * or dependences that are located in a parent configuration are resolved
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   318
     * no further and are not included in the resulting configuration. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   319
     *
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   320
     * <p> When all modules have been enumerated then a readability graph
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   321
     * is computed, and in conjunction with the module exports and service use,
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   322
     * checked for consistency. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   323
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   324
     * <p> Resolution may fail with {@code FindException} for the following
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   325
     * <em>observability-related</em> reasons: </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   326
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   327
     * <ul>
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   328
     *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   329
     *     <li><p> A root module, or a direct or transitive dependency, is not
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   330
     *     found. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   331
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   332
     *     <li><p> An error occurs when attempting to find a module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   333
     *     Possible errors include I/O errors, errors detected parsing a module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   334
     *     descriptor ({@code module-info.class}) or two versions of the same
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   335
     *     module are found in the same directory. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   336
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   337
     * </ul>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   338
     *
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   339
     * <p> Resolution may fail with {@code ResolutionException} if any of the
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   340
     * following consistency checks fail: </p>
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   341
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   342
     * <ul>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   343
     *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   344
     *     <li><p> A cycle is detected, say where module {@code m1} requires
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   345
     *     module {@code m2} and {@code m2} requires {@code m1}. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   346
     *
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   347
     *     <li><p> A module reads two or more modules with the same name. This
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   348
     *     includes the case where a module reads another with the same name as
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   349
     *     itself. </p></li>
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   350
     *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   351
     *     <li><p> Two or more modules in the configuration export the same
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   352
     *     package to a module that reads both. This includes the case where a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   353
     *     module {@code M} containing package {@code p} reads another module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   354
     *     that exports {@code p} to {@code M}. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   355
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   356
     *     <li><p> A module {@code M} declares that it "{@code uses p.S}" or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   357
     *     "{@code provides p.S with ...}" but package {@code p} is neither in
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   358
     *     module {@code M} nor exported to {@code M} by any module that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   359
     *     {@code M} reads. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   360
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   361
     * </ul>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   362
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   363
     * @implNote In the implementation then observability of modules may depend
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   364
     * on referential integrity or other checks that ensure different builds of
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   365
     * tightly coupled modules or modules for specific operating systems or
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   366
     * architectures are not combined in the same configuration.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   367
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   368
     * @param  before
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   369
     *         The <em>before</em> module finder to find modules
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   370
     * @param  parents
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   371
     *         The list parent configurations in search order
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   372
     * @param  after
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   373
     *         The <em>after</em> module finder to locate modules when not
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   374
     *         located by the {@code before} module finder or in parent
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   375
     *         configurations
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   376
     * @param  roots
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   377
     *         The possibly-empty collection of module names of the modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   378
     *         to resolve
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   379
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   380
     * @return The configuration that is the result of resolving the given
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   381
     *         root modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   382
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   383
     * @throws FindException
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   384
     *         If resolution fails for any of observability-related reasons
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   385
     *         specified above
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   386
     * @throws ResolutionException
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   387
     *         If resolution fails for any of the consistency checks specified
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   388
     *         above
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   389
     * @throws IllegalArgumentException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   390
     *         If the list of parents is empty, or the list has two or more
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   391
     *         parents with modules for different target operating systems,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   392
     *         architectures, or versions
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   393
     *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   394
     * @throws SecurityException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   395
     *         If locating a module is denied by the security manager
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   396
     */
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   397
    public static Configuration resolve(ModuleFinder before,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   398
                                        List<Configuration> parents,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   399
                                        ModuleFinder after,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   400
                                        Collection<String> roots)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   401
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   402
        Objects.requireNonNull(before);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   403
        Objects.requireNonNull(after);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   404
        Objects.requireNonNull(roots);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   405
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   406
        List<Configuration> parentList = new ArrayList<>(parents);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   407
        if (parentList.isEmpty())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   408
            throw new IllegalArgumentException("'parents' is empty");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   409
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   410
        Resolver resolver = new Resolver(before, parentList, after, null);
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   411
        resolver.resolve(roots);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   412
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   413
        return new Configuration(parentList, resolver);
36511
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
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   417
     * Resolves a collection of root modules, with service binding, to create
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   418
     * configuration.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   419
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   420
     * <p> This method works exactly as specified by {@link
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   421
     * #resolve(ModuleFinder,List,ModuleFinder,Collection)
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   422
     * resolve} except that the graph of resolved modules is augmented
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   423
     * with modules induced by the service-use dependence relation. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   424
     *
47004
b7e72fc752c9 8186684: Fix broken links in java.base API docs
jjg
parents: 46096
diff changeset
   425
     * <p><a id="service-binding"></a>More specifically, the root modules are
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   426
     * resolved as if by calling {@code resolve}. The resolved modules, and
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   427
     * all modules in the parent configurations, with {@link ModuleDescriptor#uses()
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   428
     * service dependences} are then examined. All modules found by the given
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   429
     * module finders that {@link ModuleDescriptor#provides() provide} an
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   430
     * implementation of one or more of the service types are added to the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   431
     * module graph and then resolved as if by calling the {@code
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   432
     * resolve} method. Adding modules to the module graph may introduce new
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   433
     * service-use dependences and so the process works iteratively until no
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   434
     * more modules are added. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   435
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   436
     * <p> As service binding involves resolution then it may fail with {@code
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   437
     * FindException} or {@code ResolutionException} for exactly the same
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   438
     * reasons specified in {@code resolve}. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   439
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   440
     * @param  before
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   441
     *         The <em>before</em> module finder to find modules
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   442
     * @param  parents
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   443
     *         The list parent configurations in search order
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   444
     * @param  after
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   445
     *         The <em>after</em> module finder to locate modules when not
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   446
     *         located by the {@code before} module finder or in parent
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   447
     *         configurations
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   448
     * @param  roots
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   449
     *         The possibly-empty collection of module names of the modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   450
     *         to resolve
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   451
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   452
     * @return The configuration that is the result of resolving, with service
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   453
     *         binding, the given root modules
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   454
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   455
     * @throws FindException
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   456
     *         If resolution fails for any of the observability-related reasons
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   457
     *         specified by the static {@code resolve} method
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   458
     * @throws ResolutionException
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   459
     *         If resolution fails any of the consistency checks specified by
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   460
     *         the static {@code resolve} method
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   461
     * @throws IllegalArgumentException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   462
     *         If the list of parents is empty, or the list has two or more
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   463
     *         parents with modules for different target operating systems,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   464
     *         architectures, or versions
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   465
     * @throws SecurityException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   466
     *         If locating a module is denied by the security manager
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   467
     */
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   468
    public static Configuration resolveAndBind(ModuleFinder before,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   469
                                               List<Configuration> parents,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   470
                                               ModuleFinder after,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   471
                                               Collection<String> roots)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   472
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   473
        Objects.requireNonNull(before);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   474
        Objects.requireNonNull(after);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   475
        Objects.requireNonNull(roots);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   476
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   477
        List<Configuration> parentList = new ArrayList<>(parents);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   478
        if (parentList.isEmpty())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   479
            throw new IllegalArgumentException("'parents' is empty");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   480
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   481
        Resolver resolver = new Resolver(before, parentList, after, null);
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   482
        resolver.resolve(roots).bind();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   483
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   484
        return new Configuration(parentList, resolver);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   485
    }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   486
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   487
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   488
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   489
     * Returns the <em>empty</em> configuration. There are no modules in the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   490
     * empty configuration. It has no parents.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   491
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   492
     * @return The empty configuration
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   493
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   494
    public static Configuration empty() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   495
        return EMPTY_CONFIGURATION;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   496
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   497
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   498
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   499
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   500
     * Returns an unmodifiable list of this configuration's parents, in search
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   501
     * order. If this is the {@linkplain #empty empty configuration} then an
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   502
     * empty list is returned.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   503
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   504
     * @return A possibly-empty unmodifiable list of this parent configurations
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   505
     */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   506
    public List<Configuration> parents() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   507
        return parents;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   508
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   509
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   510
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   511
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   512
     * Returns an immutable set of the resolved modules in this configuration.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   513
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   514
     * @return A possibly-empty unmodifiable set of the resolved modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   515
     *         in this configuration
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   516
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   517
    public Set<ResolvedModule> modules() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   518
        return modules;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   519
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   520
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   521
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   522
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   523
     * Finds a resolved module in this configuration, or if not in this
47004
b7e72fc752c9 8186684: Fix broken links in java.base API docs
jjg
parents: 46096
diff changeset
   524
     * configuration, the {@linkplain #parents() parent} configurations.
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   525
     * Finding a module in parent configurations is equivalent to invoking
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   526
     * {@code findModule} on each parent, in search order, until the module
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   527
     * is found or all parents have been searched. In a <em>tree of
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   528
     * configurations</em> then this is equivalent to a depth-first search.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   529
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   530
     * @param  name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   531
     *         The module name of the resolved module to find
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   532
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   533
     * @return The resolved module with the given name or an empty {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   534
     *         Optional} if there isn't a module with this name in this
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   535
     *         configuration or any parent configurations
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   536
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   537
    public Optional<ResolvedModule> findModule(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   538
        Objects.requireNonNull(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   539
        ResolvedModule m = nameToModule.get(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   540
        if (m != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   541
            return Optional.of(m);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   542
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   543
        if (!parents.isEmpty()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   544
            return configurations()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   545
                    .skip(1)  // skip this configuration
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   546
                    .map(cf -> cf.nameToModule)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   547
                    .filter(map -> map.containsKey(name))
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   548
                    .map(map -> map.get(name))
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   549
                    .findFirst();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   550
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   551
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   552
        return Optional.empty();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   553
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   554
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   555
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   556
    Set<ModuleDescriptor> descriptors() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   557
        if (modules.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   558
            return Collections.emptySet();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   559
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   560
            return modules.stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   561
                    .map(ResolvedModule::reference)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   562
                    .map(ModuleReference::descriptor)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   563
                    .collect(Collectors.toSet());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   564
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   565
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   566
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   567
    Set<ResolvedModule> reads(ResolvedModule m) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   568
        return Collections.unmodifiableSet(graph.get(m));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   569
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   570
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   571
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   572
     * Returns an ordered stream of configurations. The first element is this
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   573
     * configuration, the remaining elements are the parent configurations
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   574
     * in DFS order.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   575
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   576
     * @implNote For now, the assumption is that the number of elements will
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   577
     * be very low and so this method does not use a specialized spliterator.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   578
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   579
    Stream<Configuration> configurations() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   580
        List<Configuration> allConfigurations = this.allConfigurations;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   581
        if (allConfigurations == null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   582
            allConfigurations = new ArrayList<>();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   583
            Set<Configuration> visited = new HashSet<>();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   584
            Deque<Configuration> stack = new ArrayDeque<>();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   585
            visited.add(this);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   586
            stack.push(this);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   587
            while (!stack.isEmpty()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   588
                Configuration layer = stack.pop();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   589
                allConfigurations.add(layer);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   590
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   591
                // push in reverse order
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   592
                for (int i = layer.parents.size() - 1; i >= 0; i--) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   593
                    Configuration parent = layer.parents.get(i);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   594
                    if (!visited.contains(parent)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   595
                        visited.add(parent);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   596
                        stack.push(parent);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   597
                    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   598
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   599
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   600
            this.allConfigurations = Collections.unmodifiableList(allConfigurations);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   601
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   602
        return allConfigurations.stream();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   603
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   604
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   605
    private volatile List<Configuration> allConfigurations;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   606
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   607
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   608
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   609
     * Returns a string describing this configuration.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   610
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   611
     * @return A possibly empty string describing this configuration
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   612
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   613
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   614
    public String toString() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   615
        return modules().stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   616
                .map(ResolvedModule::name)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   617
                .collect(Collectors.joining(", "));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   618
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   619
}