jdk/src/java.base/share/classes/java/lang/module/Configuration.java
author alanb
Fri, 10 Feb 2017 09:04:39 +0000
changeset 43712 5dfd0950317c
parent 42338 a60f280f803c
child 44359 c6761862ca0b
permissions -rw-r--r--
8173393: Module system implementation refresh (2/2017) Reviewed-by: dfuchs, psandoz, mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com, claes.redestad@oracle.com, alex.buckley@oracle.com, mark.reinhold@oracle.com, john.r.rose@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
 *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
    67
 * <p> Each {@link java.lang.reflect.Layer layer} of modules in the Java virtual
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
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
    69
 * java.lang.reflect.Layer#boot() boot} layer is obtained by invoking {@code
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
    70
 * Layer.boot().configuration()}. The configuration for the boot layer will
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
    71
 * 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
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
 *    Configuration parent = Layer.boot().configuration();
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
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
 * @see java.lang.reflect.Layer
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
    private final String osVersion;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   116
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   117
    String osName() { return osName; }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   118
    String osArch() { return osArch; }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   119
    String osVersion() { return osVersion; }
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();
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   126
        this.osName = null;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   127
        this.osArch = null;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   128
        this.osVersion = null;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   131
    private Configuration(List<Configuration> parents,
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   132
                          Resolver resolver,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   133
                          boolean check)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   134
    {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   135
        Map<ResolvedModule, Set<ResolvedModule>> g = resolver.finish(this, check);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   137
        @SuppressWarnings(value = {"rawtypes", "unchecked"})
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   138
        Entry<String, ResolvedModule>[] nameEntries
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   139
            = (Entry<String, ResolvedModule>[])new Entry[g.size()];
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   140
        ResolvedModule[] moduleArray = new ResolvedModule[g.size()];
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   141
        int i = 0;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   142
        for (ResolvedModule resolvedModule : g.keySet()) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   143
            moduleArray[i] = resolvedModule;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   144
            nameEntries[i] = Map.entry(resolvedModule.name(), resolvedModule);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   145
            i++;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   148
        this.parents = Collections.unmodifiableList(parents);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   149
        this.graph = g;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   150
        this.modules = Set.of(moduleArray);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   151
        this.nameToModule = Map.ofEntries(nameEntries);
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   152
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   153
        this.osName = resolver.osName();
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   154
        this.osArch = resolver.osArch();
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   155
        this.osVersion = resolver.osVersion();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
     * 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
   160
     * parent, to create a new configuration. This method works exactly as
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   161
     * specified by the static {@link
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   162
     * #resolve(ModuleFinder,List,ModuleFinder,Collection) resolve}
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   163
     * 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
   164
     * 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
   165
     * invoking:
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   166
     * <pre> {@code
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   167
     *     Configuration.resolve(before, List.of(cf), after, roots);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   168
     * }</pre>
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   169
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   170
     * @param  before
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   171
     *         The <em>before</em> module finder to find modules
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   172
     * @param  after
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   173
     *         The <em>after</em> module finder to locate modules when not
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   174
     *         located by the {@code before} module finder or in parent
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   175
     *         configurations
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   176
     * @param  roots
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   177
     *         The possibly-empty collection of module names of the modules
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   178
     *         to resolve
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   179
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   180
     * @return The configuration that is the result of resolving the given
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   181
     *         root modules
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   182
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   183
     * @throws FindException
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   184
     *         If resolution fails for any of the observability-related reasons
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   185
     *         specified by the static {@code resolve} method
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   186
     * @throws ResolutionException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   187
     *         If any of the post-resolution consistency checks specified by
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   188
     *         the  static {@code resolve} method fail
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   189
     * @throws SecurityException
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   190
     *         If locating a module is denied by the security manager
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
    public Configuration resolve(ModuleFinder before,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   193
                                 ModuleFinder after,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   194
                                 Collection<String> roots)
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   195
    {
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   196
        return resolve(before, List.of(this), after, roots);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   197
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   198
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   199
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   200
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   201
     * Resolves a collection of root modules, with service binding, and with
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   202
     * this configuration as its parent, to create a new configuration.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   203
     * This method works exactly as specified by the static {@link
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   204
     * #resolveAndBind(ModuleFinder,List,ModuleFinder,Collection)
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   205
     * resolveAndBind} method when invoked with this configuration
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   206
     * 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
   207
     * this method is equivalent to invoking:
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   208
     * <pre> {@code
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   209
     *     Configuration.resolveAndBind(before, List.of(cf), after, roots);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   210
     * }</pre>
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   211
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   212
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   213
     * @param  before
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   214
     *         The <em>before</em> module finder to find modules
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   215
     * @param  after
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   216
     *         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
   217
     *         located by the {@code before} module finder or in parent
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   218
     *         configurations
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   219
     * @param  roots
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   220
     *         The possibly-empty collection of module names of the modules
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   221
     *         to resolve
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   222
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   223
     * @return The configuration that is the result of resolving, with service
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   224
     *         binding, the given root modules
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   225
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   226
     * @throws FindException
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   227
     *         If resolution fails for any of the observability-related reasons
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   228
     *         specified by the static {@code resolve} method
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   229
     * @throws ResolutionException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   230
     *         If any of the post-resolution consistency checks specified by
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   231
     *         the  static {@code resolve} method fail
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   232
     * @throws SecurityException
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   233
     *         If locating a module is denied by the security manager
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
    public Configuration resolveAndBind(ModuleFinder before,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   236
                                        ModuleFinder after,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   237
                                        Collection<String> roots)
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   238
    {
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   239
        return resolveAndBind(before, List.of(this), after, roots);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   240
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   241
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   242
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
     * Resolves a collection of root modules, with service binding, and with
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   245
     * the empty configuration as its parent. The post resolution checks
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   246
     * are optionally run.
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
     * 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
   249
     */
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   250
    static Configuration resolveAndBind(ModuleFinder finder,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   251
                                        Collection<String> roots,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   252
                                        boolean check,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   253
                                        PrintStream traceOutput)
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
        List<Configuration> parents = List.of(empty());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   256
        Resolver resolver = new Resolver(finder, parents, ModuleFinder.of(), traceOutput);
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   257
        resolver.resolve(roots).bind();
42338
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
        return new Configuration(parents, resolver, check);
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
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   263
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   264
     * Resolves a collection of root modules to create a configuration.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   265
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
     * <p> Each root module is located using the given {@code before} module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
     * finder. If a module is not found then it is located in the parent
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
     * configuration as if by invoking the {@link #findModule(String)
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   269
     * 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
   270
     * 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
   271
     * same search order is used to locate transitive dependences. Root modules
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   272
     * or dependences that are located in a parent configuration are resolved
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   273
     * no further and are not included in the resulting configuration. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   274
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   275
     * <p> When all modules have been resolved then the resulting dependency
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   276
     * 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
   277
     * readability graph is constructed, and in conjunction with the module
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   278
     * exports and service use, checked for consistency. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   279
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   280
     * <p> Resolution may fail with {@code FindException} for the following
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   281
     * <em>observability-related</em> reasons: </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   282
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   283
     * <ul>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   284
     *     <li><p> A root module, or a direct or transitive dependency, is not
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   285
     *     found. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   286
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   287
     *     <li><p> An error occurs when attempting to find a module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   288
     *     Possible errors include I/O errors, errors detected parsing a module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   289
     *     descriptor ({@code module-info.class}) or two versions of the same
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   290
     *     module are found in the same directory. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   291
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   292
     *     <li><p> A module with the required name is found but the module
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   293
     *     requires a different {@link ModuleDescriptor#osName() operating
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   294
     *     system}, {@link ModuleDescriptor#osArch() architecture}, or {@link
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   295
     *     ModuleDescriptor#osVersion() version} to other modules that have
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   296
     *     been resolved for the new configuration or modules in the parent
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   297
     *     configurations. </p></li>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   298
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   299
     * </ul>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   300
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   301
     * <p> Post-resolution consistency checks may fail with {@code
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   302
     * ResolutionException} for the following reasons: </p>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   303
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   304
     * <ul>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   305
     *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   306
     *     <li><p> A cycle is detected, say where module {@code m1} requires
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   307
     *     module {@code m2} and {@code m2} requires {@code m1}. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   308
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   309
     *     <li><p> Two or more modules in the configuration export the same
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   310
     *     package to a module that reads both. This includes the case where a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   311
     *     module {@code M} containing package {@code p} reads another module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   312
     *     that exports {@code p} to {@code M}. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   313
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   314
     *     <li><p> A module {@code M} declares that it "{@code uses p.S}" or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   315
     *     "{@code provides p.S with ...}" but package {@code p} is neither in
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   316
     *     module {@code M} nor exported to {@code M} by any module that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   317
     *     {@code M} reads. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   318
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   319
     * </ul>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   320
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   321
     * @implNote In the implementation then observability of modules may depend
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   322
     * on referential integrity checks that ensure different builds of tightly
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   323
     * coupled modules are not combined in the same configuration.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   324
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   325
     * @param  before
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   326
     *         The <em>before</em> module finder to find modules
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   327
     * @param  parents
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   328
     *         The list parent configurations in search order
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   329
     * @param  after
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   330
     *         The <em>after</em> module finder to locate modules when not
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   331
     *         located by the {@code before} module finder or in parent
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   332
     *         configurations
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   333
     * @param  roots
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   334
     *         The possibly-empty collection of module names of the modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   335
     *         to resolve
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   336
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   337
     * @return The configuration that is the result of resolving the given
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   338
     *         root modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   339
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   340
     * @throws FindException
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   341
     *         If resolution fails for an observability-related reason
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   342
     * @throws ResolutionException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   343
     *         If a post-resolution consistency checks fails
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   344
     * @throws IllegalArgumentException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   345
     *         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
   346
     *         parents with modules for different target operating systems,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   347
     *         architectures, or versions
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   348
     *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   349
     * @throws SecurityException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   350
     *         If locating a module is denied by the security manager
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   351
     */
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   352
    public static Configuration resolve(ModuleFinder before,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   353
                                        List<Configuration> parents,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   354
                                        ModuleFinder after,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   355
                                        Collection<String> roots)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   356
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   357
        Objects.requireNonNull(before);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   358
        Objects.requireNonNull(after);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   359
        Objects.requireNonNull(roots);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   360
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   361
        List<Configuration> parentList = new ArrayList<>(parents);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   362
        if (parentList.isEmpty())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   363
            throw new IllegalArgumentException("'parents' is empty");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   364
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   365
        Resolver resolver = new Resolver(before, parentList, after, null);
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   366
        resolver.resolve(roots);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   367
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   368
        return new Configuration(parentList, resolver, true);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   369
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   370
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   371
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   372
     * Resolves a collection of root modules, with service binding, to create
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   373
     * configuration.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   374
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   375
     * <p> This method works exactly as specified by {@link
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   376
     * #resolve(ModuleFinder,List,ModuleFinder,Collection)
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   377
     * resolve} except that the graph of resolved modules is augmented
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   378
     * with modules induced by the service-use dependence relation. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   379
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   380
     * <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
   381
     * {@code resolve}. The resolved modules, and all modules in the
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   382
     * parent configurations, with {@link ModuleDescriptor#uses() service
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   383
     * dependences} are then examined. All modules found by the given module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   384
     * finders that {@link ModuleDescriptor#provides() provide} an
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   385
     * implementation of one or more of the service types are added to the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   386
     * 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
   387
     * resolve} method. Adding modules to the module graph may introduce new
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   388
     * service-use dependences and so the process works iteratively until no
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   389
     * more modules are added. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   390
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   391
     * <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
   392
     * FindException} or {@code ResolutionException} for exactly the same
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   393
     * reasons specified in {@code resolve}. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   394
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   395
     * @param  before
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   396
     *         The <em>before</em> module finder to find modules
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   397
     * @param  parents
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   398
     *         The list parent configurations in search order
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   399
     * @param  after
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   400
     *         The <em>after</em> module finder to locate modules when not
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   401
     *         located by the {@code before} module finder or in parent
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   402
     *         configurations
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   403
     * @param  roots
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   404
     *         The possibly-empty collection of module names of the modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   405
     *         to resolve
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   406
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   407
     * @return The configuration that is the result of resolving, with service
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   408
     *         binding, the given root modules
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   409
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   410
     * @throws FindException
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   411
     *         If resolution fails for any of the observability-related reasons
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   412
     *         specified by the static {@code resolve} method
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   413
     * @throws ResolutionException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   414
     *         If any of the post-resolution consistency checks specified by
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   415
     *         the  static {@code resolve} method fail
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   416
     * @throws IllegalArgumentException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   417
     *         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
   418
     *         parents with modules for different target operating systems,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   419
     *         architectures, or versions
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   420
     * @throws SecurityException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   421
     *         If locating a module is denied by the security manager
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   422
     */
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   423
    public static Configuration resolveAndBind(ModuleFinder before,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   424
                                               List<Configuration> parents,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   425
                                               ModuleFinder after,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   426
                                               Collection<String> roots)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   427
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   428
        Objects.requireNonNull(before);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   429
        Objects.requireNonNull(after);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   430
        Objects.requireNonNull(roots);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   431
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   432
        List<Configuration> parentList = new ArrayList<>(parents);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   433
        if (parentList.isEmpty())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   434
            throw new IllegalArgumentException("'parents' is empty");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   435
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   436
        Resolver resolver = new Resolver(before, parentList, after, null);
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   437
        resolver.resolve(roots).bind();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   438
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   439
        return new Configuration(parentList, resolver, true);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   440
    }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   441
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   442
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   443
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   444
     * 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
   445
     * empty configuration. It has no parents.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   446
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   447
     * @return The empty configuration
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   448
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   449
    public static Configuration empty() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   450
        return EMPTY_CONFIGURATION;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   451
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   452
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   453
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   454
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   455
     * Returns an unmodifiable list of this configuration's parents, in search
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   456
     * order. If this is the {@linkplain #empty empty configuration} then an
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   457
     * empty list is returned.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   458
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   459
     * @return A possibly-empty unmodifiable list of this parent configurations
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   460
     */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   461
    public List<Configuration> parents() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   462
        return parents;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   463
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   464
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   465
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   466
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   467
     * Returns an immutable set of the resolved modules in this configuration.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   468
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   469
     * @return A possibly-empty unmodifiable set of the resolved modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   470
     *         in this configuration
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   471
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   472
    public Set<ResolvedModule> modules() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   473
        return modules;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   474
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   475
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   476
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   477
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   478
     * 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
   479
     * configuration, the {@linkplain #parents parent} configurations.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   480
     * Finding a module in parent configurations is equivalent to invoking
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   481
     * {@code findModule} on each parent, in search order, until the module
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   482
     * 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
   483
     * configurations</em> then this is equivalent to a depth-first search.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   484
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   485
     * @param  name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   486
     *         The module name of the resolved module to find
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   487
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   488
     * @return The resolved module with the given name or an empty {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   489
     *         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
   490
     *         configuration or any parent configurations
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   491
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   492
    public Optional<ResolvedModule> findModule(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   493
        Objects.requireNonNull(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   494
        ResolvedModule m = nameToModule.get(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   495
        if (m != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   496
            return Optional.of(m);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   497
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   498
        if (!parents.isEmpty()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   499
            return configurations()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   500
                    .skip(1)  // skip this configuration
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   501
                    .map(cf -> cf.nameToModule)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   502
                    .filter(map -> map.containsKey(name))
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   503
                    .map(map -> map.get(name))
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   504
                    .findFirst();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   505
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   506
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   507
        return Optional.empty();
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
    Set<ModuleDescriptor> descriptors() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   512
        if (modules.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   513
            return Collections.emptySet();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   514
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   515
            return modules.stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   516
                    .map(ResolvedModule::reference)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   517
                    .map(ModuleReference::descriptor)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   518
                    .collect(Collectors.toSet());
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
    Set<ResolvedModule> reads(ResolvedModule m) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   523
        return Collections.unmodifiableSet(graph.get(m));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   524
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   525
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   526
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   527
     * Returns an ordered stream of configurations. The first element is this
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   528
     * configuration, the remaining elements are the parent configurations
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   529
     * in DFS order.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   530
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   531
     * @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
   532
     * 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
   533
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   534
    Stream<Configuration> configurations() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   535
        List<Configuration> allConfigurations = this.allConfigurations;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   536
        if (allConfigurations == null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   537
            allConfigurations = new ArrayList<>();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   538
            Set<Configuration> visited = new HashSet<>();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   539
            Deque<Configuration> stack = new ArrayDeque<>();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   540
            visited.add(this);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   541
            stack.push(this);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   542
            while (!stack.isEmpty()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   543
                Configuration layer = stack.pop();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   544
                allConfigurations.add(layer);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   545
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   546
                // push in reverse order
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   547
                for (int i = layer.parents.size() - 1; i >= 0; i--) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   548
                    Configuration parent = layer.parents.get(i);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   549
                    if (!visited.contains(parent)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   550
                        visited.add(parent);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   551
                        stack.push(parent);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   552
                    }
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
            this.allConfigurations = Collections.unmodifiableList(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
        return allConfigurations.stream();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   558
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   559
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   560
    private volatile List<Configuration> allConfigurations;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   561
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   562
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   563
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   564
     * Returns a string describing this configuration.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   565
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   566
     * @return A possibly empty string describing this configuration
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   567
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   568
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   569
    public String toString() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   570
        return modules().stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   571
                .map(ResolvedModule::name)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   572
                .collect(Collectors.joining(", "));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   573
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   574
}