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