jdk/src/java.base/share/classes/java/lang/module/Configuration.java
author alanb
Sat, 21 May 2016 08:01:03 +0100
changeset 38457 3d019217e322
parent 37779 7c84df693837
child 42338 a60f280f803c
permissions -rw-r--r--
8152650: ModuleFinder.compose should accept varargs Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
package java.lang.module;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    28
import java.io.PrintStream;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.util.Collection;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.util.HashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.util.Objects;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.util.Optional;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.util.Set;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.util.stream.Collectors;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
 * The configuration that is the result of resolution or resolution with
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
 * service binding.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
 * <h2><a name="resolution">Resolution</a></h2>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
 * <p> Resolution is the process of computing the transitive closure of a set
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
 * of root modules over a set of observable modules by resolving the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
 * dependences expressed by {@code requires} clauses.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
 * The <em>dependence graph</em> is augmented with edges that take account of
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
 * implicitly declared dependences ({@code requires public}) to create a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
 * <em>readability graph</em>. A {@code Configuration} encapsulates the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
 * resulting graph of {@link ResolvedModule resolved modules}.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
 * <p> Suppose we have the following observable modules: </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
 * <pre> {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
 *     module m1 { requires m2; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
 *     module m2 { requires public m3; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
 *     module m3 { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
 *     module m4 { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
 * } </pre>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
 * <p> If the module {@code m1} is resolved then the resulting configuration
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
 * contains three modules ({@code m1}, {@code m2}, {@code m3}). The edges in
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
 * its readability graph are: </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
 * <pre> {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
 *     m1 --> m2  (meaning m1 reads m2)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
 *     m1 --> m3
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
 *     m2 --> m3
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
 * } </pre>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
 * <p> Resolution is an additive process. When computing the transitive closure
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
 * then the dependence relation may include dependences on modules in parent
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
 * configurations. The result is a <em>relative configuration</em> that is
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
 * relative to a parent configuration and where the readability graph may have
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
 * edges from modules in the configuration to modules in a parent configuration.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
 * </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
 * <p> Suppose we have the following observable modules: </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
 * <pre> {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
 *     module m1 { requires m2; requires java.xml; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
 *     module m2 { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
 * } </pre>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
 * <p> If module {@code m1} is resolved with the configuration for the {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
 * java.lang.reflect.Layer#boot() boot} layer as the parent then the resulting
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
 * configuration contains two modules ({@code m1}, {@code m2}). The edges in
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
 * its readability graph are:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
 * <pre> {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
 *     m1 --> m2
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
 *     m1 --> java.xml
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
 * } </pre>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
 * where module {@code java.xml} is in the parent configuration. For
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
 * simplicity, this example omits the implicitly declared dependence on the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
 * {@code java.base} module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
 * <a name="automaticmoduleresolution"></a>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
 * <p> {@link ModuleDescriptor#isAutomatic() Automatic} modules receive special
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
 * treatment during resolution. Each automatic module is resolved so that it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
 * reads all other modules in the configuration and all parent configurations.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
 * Each automatic module is also resolved as if it {@code requires public} all
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
 * other automatic modules in the configuration (and all automatic modules in
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
 * parent configurations). </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
 * <h2><a name="servicebinding">Service binding</a></h2>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
 * <p> Service binding is the process of augmenting a graph of resolved modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
 * from the set of observable modules induced by the service-use dependence
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
 * ({@code uses} and {@code provides} clauses). Any module that was not
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
 * previously in the graph requires resolution to compute its transitive
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
 * closure. Service binding is an iterative process in that adding a module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
 * that satisfies some service-use dependence may introduce new service-use
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
 * dependences. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
 * <p> Suppose we have the following observable modules: </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
 * <pre> {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
 *     module m1 { exports p; uses p.S; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
 *     module m2 { requires m1; provides p.S with p2.S2; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
 *     module m3 { requires m1; requires m4; provides p.S with p3.S3; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
 *     module m4 { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
 * } </pre>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
 * <p> If the module {@code m1} is resolved then the resulting graph of modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
 * has one module ({@code m1}). If the graph is augmented with modules induced
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
 * by the service-use dependence relation then the configuration will contain
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
 * four modules ({@code m1}, {@code m2}, {@code m3}, {@code m4}). The edges in
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
 * its readability graph are: </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
 * <pre> {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
 *     m2 --> m1
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
 *     m3 --> m1
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
 *     m3 --> m4
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
 * } </pre>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
 * <p> The edges in the conceptual service-use graph are: </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
 * <pre> {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
 *     m1 --> m2  (meaning m1 uses a service that is provided by m2)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
 *     m1 --> m3
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
 * } </pre>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
 * <p> If this configuration is instantiated as a {@code Layer}, and if code in
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
 * module {@code m1} uses {@link java.util.ServiceLoader ServiceLoader} to
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
 * iterate over implementations of {@code p.S.class}, then it will iterate over
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
 * an instance of {@code p2.S2} and {@code p3.S3}. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
 * <h3> Example </h3>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
 * <p> The following example uses the {@code resolveRequires} method to resolve
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
 * a module named <em>myapp</em> with the configuration for the boot layer as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
 * the parent configuration. It prints the name of each resolved module and
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
 * the names of the modules that each module reads. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
 * <pre>{@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
 *    ModuleFinder finder = ModuleFinder.of(dir1, dir2, dir3);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
 *    Configuration parent = Layer.boot().configuration();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
 *    Configuration cf = parent.resolveRequires(finder,
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37779
diff changeset
   155
 *                                              ModuleFinder.of(),
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
 *                                              Set.of("myapp"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
 *    cf.modules().forEach(m -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
 *        System.out.format("%s -> %s%n",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
 *            m.name(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
 *            m.reads().stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
 *                .map(ResolvedModule::name)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
 *                .collect(Collectors.joining(", ")));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
 *    });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
 * }</pre>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
 * @since 9
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
 * @see java.lang.reflect.Layer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
public final class Configuration {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
    // @see Configuration#empty()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
    private static final Configuration EMPTY_CONFIGURATION = new Configuration();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
    private final Configuration parent;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
    private final Map<ResolvedModule, Set<ResolvedModule>> graph;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
    private final Set<ResolvedModule> modules;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
    private final Map<String, ResolvedModule> nameToModule;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
    private Configuration() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
        this.parent = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
        this.graph = Collections.emptyMap();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
        this.modules = Collections.emptySet();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
        this.nameToModule = Collections.emptyMap();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   187
    private Configuration(Configuration parent,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   188
                          Resolver resolver,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   189
                          boolean check)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   190
    {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   191
        Map<ResolvedModule, Set<ResolvedModule>> g = resolver.finish(this, check);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
        Map<String, ResolvedModule> nameToModule = new HashMap<>();
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   194
        for (ResolvedModule resolvedModule : g.keySet()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
            nameToModule.put(resolvedModule.name(), resolvedModule);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   197
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
        this.parent = parent;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   199
        this.graph = g;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   200
        this.modules = Collections.unmodifiableSet(g.keySet());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
        this.nameToModule = Collections.unmodifiableMap(nameToModule);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
     * Resolves a collection of root modules, with this configuration as its
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
     * parent, to create a new configuration.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
     * <p> Each root module is located using the given {@code before} module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
     * finder. If a module is not found then it is located in the parent
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
     * configuration as if by invoking the {@link #findModule(String)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
     * findModule} method. If not found then the module is located using the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   213
     * given {@code after} module finder. The same search order is used to
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   214
     * locate transitive dependences. Root modules or dependences that are
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   215
     * located in a parent configuration are resolved no further and are not
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   216
     * included in the resulting configuration. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
     * <p> When all modules have been resolved then the resulting dependency
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
     * graph is checked to ensure that it does not contain cycles. A
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
     * readability graph is constructed and then, in conjunction with the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
     * module exports and service use, checked for consistency. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
     * <p> Resolution and the (post-resolution) consistency checks may fail for
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
     * following reasons: </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
     * <ul>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
     *     <li><p> A root module, or a direct or transitive dependency, is not
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
     *     found. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
     *     <li><p> An error occurs when attempting to find a module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
     *     Possible errors include I/O errors, errors detected parsing a module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
     *     descriptor ({@code module-info.class}) or two versions of the same
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
     *     module are found in the same directory. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
     *     <li><p> A cycle is detected, say where module {@code m1} requires
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
     *     module {@code m2} and {@code m2} requires {@code m1}. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
     *     <li><p> Two or more modules in the configuration export the same
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
     *     package to a module that reads both. This includes the case where a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
     *     module {@code M} containing package {@code p} reads another module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
     *     that exports {@code p} to {@code M}. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
     *     <li><p> A module {@code M} declares that it "{@code uses p.S}" or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
     *     "{@code provides p.S with ...}" but package {@code p} is neither in
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
     *     module {@code M} nor exported to {@code M} by any module that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
     *     {@code M} reads. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
     *     <li><p> A module {@code M} declares that it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
     *     "{@code provides ... with q.T}" but package {@code q} is not in
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
     *     module {@code M}. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
     *     <li><p> Two or more modules in the configuration are specific to
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
     *     different {@link ModuleDescriptor#osName() operating systems},
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   254
     *     {@link ModuleDescriptor#osArch() architectures}, or {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   255
     *     ModuleDescriptor#osVersion() versions}. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   256
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   257
     *     <li><p> Other implementation specific checks, for example referential
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   258
     *     integrity checks to ensure that different versions of tighly coupled
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   259
     *     modules cannot be combined in the same configuration. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   260
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   261
     * </ul>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   262
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
     * @param  before
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   264
     *         The <em>before</em> module finder to find modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   265
     * @param  after
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
     *         The <em>after</em> module finder to locate modules when not
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
     *         located by the {@code before} module finder or in parent
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
     *         configurations
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
     * @param  roots
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   270
     *         The possibly-empty collection of module names of the modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   271
     *         to resolve
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   272
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   273
     * @return The configuration that is the result of resolving the given
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   274
     *         root modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   275
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   276
     * @throws ResolutionException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   277
     *         If resolution or the post-resolution checks fail for any of the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   278
     *         reasons listed
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   279
     * @throws SecurityException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   280
     *         If locating a module is denied by the security manager
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   281
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   282
    public Configuration resolveRequires(ModuleFinder before,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   283
                                         ModuleFinder after,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   284
                                         Collection<String> roots)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   285
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   286
        Objects.requireNonNull(before);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   287
        Objects.requireNonNull(after);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   288
        Objects.requireNonNull(roots);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   289
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   290
        Resolver resolver = new Resolver(before, this, after, null);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   291
        resolver.resolveRequires(roots);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   292
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   293
        return new Configuration(this, resolver, true);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   294
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   295
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   296
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   297
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   298
     * Resolves a collection of root modules, with service binding, and with
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   299
     * this configuration as its parent, to create a new configuration.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   300
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   301
     * <p> This method works exactly as specified by {@link #resolveRequires
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   302
     * resolveRequires} except that the graph of resolved modules is augmented
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   303
     * with modules induced by the service-use dependence relation. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   304
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   305
     * <p> More specifically, the root modules are resolved as if by calling
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   306
     * {@code resolveRequires}. The resolved modules, and all modules in the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   307
     * parent configurations, with {@link ModuleDescriptor#uses() service
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   308
     * dependences} are then examined. All modules found by the given module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   309
     * finders that {@link ModuleDescriptor#provides() provide} an
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   310
     * implementation of one or more of the service types are added to the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   311
     * module graph and then resolved as if by calling the {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   312
     * resolveRequires} method. Adding modules to the module graph may
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   313
     * introduce new service-use dependences and so the process works
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   314
     * iteratively until no more modules are added. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   315
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   316
     * <p> As service binding involves resolution then it may fail with {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   317
     * ResolutionException} for exactly the same reasons specified in
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   318
     * {@code resolveRequires}.  </p>
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   322
     * @param  after
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   323
     *         The <em>after</em> module finder to locate modules when not
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   324
     *         located by the {@code before} module finder or in parent
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   325
     *         configurations
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   326
     * @param  roots
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   327
     *         The possibly-empty collection of module names of the modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   328
     *         to resolve
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   329
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   330
     * @return The configuration that is the result of resolving the given
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   331
     *         root modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   332
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   333
     * @throws ResolutionException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   334
     *         If resolution or the post-resolution checks fail for any of the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   335
     *         reasons listed
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   336
     * @throws SecurityException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   337
     *         If locating a module is denied by the security manager
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   338
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   339
    public Configuration resolveRequiresAndUses(ModuleFinder before,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   340
                                                ModuleFinder after,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   341
                                                Collection<String> roots)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   342
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   343
        Objects.requireNonNull(before);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   344
        Objects.requireNonNull(after);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   345
        Objects.requireNonNull(roots);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   346
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   347
        Resolver resolver = new Resolver(before, this, after, null);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   348
        resolver.resolveRequires(roots).resolveUses();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   349
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   350
        return new Configuration(this, resolver, true);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   351
    }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   352
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   353
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   354
    /**
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   355
     * Resolves a collection of root modules, with service binding, and with
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   356
     * the empty configuration as its parent. The post resolution checks
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   357
     * are optionally run.
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   358
     *
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   359
     * This method is used to create the configuration for the boot layer.
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   360
     */
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   361
    static Configuration resolveRequiresAndUses(ModuleFinder finder,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   362
                                                Collection<String> roots,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   363
                                                boolean check,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   364
                                                PrintStream traceOutput)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   365
    {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   366
        Configuration parent = empty();
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   367
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   368
        Resolver resolver
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37779
diff changeset
   369
            = new Resolver(finder, parent, ModuleFinder.of(), traceOutput);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   370
        resolver.resolveRequires(roots).resolveUses();
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   371
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   372
        return new Configuration(parent, resolver, check);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   373
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   374
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   375
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   376
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   377
     * Returns the <em>empty</em> configuration. The empty configuration does
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   378
     * not contain any modules and does not have a parent.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   379
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   380
     * @return The empty configuration
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   381
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   382
    public static Configuration empty() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   383
        return EMPTY_CONFIGURATION;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   384
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   385
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   386
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   387
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   388
     * Returns this configuration's parent unless this is the {@linkplain #empty
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   389
     * empty configuration}, which has no parent.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   390
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   391
     * @return This configuration's parent
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   392
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   393
    public Optional<Configuration> parent() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   394
        return Optional.ofNullable(parent);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   395
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   396
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   397
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   398
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   399
     * Returns an immutable set of the resolved modules in this configuration.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   400
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   401
     * @return A possibly-empty unmodifiable set of the resolved modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   402
     *         in this configuration
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   403
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   404
    public Set<ResolvedModule> modules() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   405
        return modules;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   406
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   407
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   408
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   409
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   410
     * Finds a resolved module in this configuration, or if not in this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   411
     * configuration, the {@linkplain #parent parent} configurations.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   412
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   413
     * @param  name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   414
     *         The module name of the resolved module to find
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   415
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   416
     * @return The resolved module with the given name or an empty {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   417
     *         Optional} if there isn't a module with this name in this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   418
     *         configuration or any parent configuration
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   419
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   420
    public Optional<ResolvedModule> findModule(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   421
        Objects.requireNonNull(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   422
        if (parent == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   423
            return Optional.empty();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   424
        ResolvedModule m = nameToModule.get(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   425
        if (m != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   426
            return Optional.of(m);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   427
        return parent().flatMap(x -> x.findModule(name));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   428
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   429
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   430
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   431
    Set<ModuleDescriptor> descriptors() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   432
        if (modules.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   433
            return Collections.emptySet();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   434
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   435
            return modules.stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   436
                    .map(ResolvedModule::reference)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   437
                    .map(ModuleReference::descriptor)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   438
                    .collect(Collectors.toSet());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   439
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   440
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   441
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   442
    Set<ResolvedModule> reads(ResolvedModule m) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   443
        return Collections.unmodifiableSet(graph.get(m));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   444
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   445
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   446
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   447
     * Returns a string describing this configuration.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   448
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   449
     * @return A string describing this configuration
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   450
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   451
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   452
    public String toString() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   453
        return modules().stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   454
                .map(ResolvedModule::name)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   455
                .collect(Collectors.joining(", "));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   456
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   457
}