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