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