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