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