36511
|
1 |
/*
|
|
2 |
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
|
|
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 |
|
|
28 |
import java.util.Collection;
|
|
29 |
import java.util.Collections;
|
|
30 |
import java.util.HashMap;
|
|
31 |
import java.util.Map;
|
|
32 |
import java.util.Objects;
|
|
33 |
import java.util.Optional;
|
|
34 |
import java.util.Set;
|
|
35 |
import java.util.stream.Collectors;
|
|
36 |
|
|
37 |
/**
|
|
38 |
* The configuration that is the result of resolution or resolution with
|
|
39 |
* service binding.
|
|
40 |
*
|
|
41 |
* <h2><a name="resolution">Resolution</a></h2>
|
|
42 |
*
|
|
43 |
* <p> Resolution is the process of computing the transitive closure of a set
|
|
44 |
* of root modules over a set of observable modules by resolving the
|
|
45 |
* dependences expressed by {@code requires} clauses.
|
|
46 |
*
|
|
47 |
* The <em>dependence graph</em> is augmented with edges that take account of
|
|
48 |
* implicitly declared dependences ({@code requires public}) to create a
|
|
49 |
* <em>readability graph</em>. A {@code Configuration} encapsulates the
|
|
50 |
* resulting graph of {@link ResolvedModule resolved modules}.
|
|
51 |
*
|
|
52 |
* <p> Suppose we have the following observable modules: </p>
|
|
53 |
* <pre> {@code
|
|
54 |
* module m1 { requires m2; }
|
|
55 |
* module m2 { requires public m3; }
|
|
56 |
* module m3 { }
|
|
57 |
* module m4 { }
|
|
58 |
* } </pre>
|
|
59 |
*
|
|
60 |
* <p> If the module {@code m1} is resolved then the resulting configuration
|
|
61 |
* contains three modules ({@code m1}, {@code m2}, {@code m3}). The edges in
|
|
62 |
* its readability graph are: </p>
|
|
63 |
* <pre> {@code
|
|
64 |
* m1 --> m2 (meaning m1 reads m2)
|
|
65 |
* m1 --> m3
|
|
66 |
* m2 --> m3
|
|
67 |
* } </pre>
|
|
68 |
*
|
|
69 |
* <p> Resolution is an additive process. When computing the transitive closure
|
|
70 |
* then the dependence relation may include dependences on modules in parent
|
|
71 |
* configurations. The result is a <em>relative configuration</em> that is
|
|
72 |
* relative to a parent configuration and where the readability graph may have
|
|
73 |
* edges from modules in the configuration to modules in a parent configuration.
|
|
74 |
* </p>
|
|
75 |
*
|
|
76 |
* <p> Suppose we have the following observable modules: </p>
|
|
77 |
* <pre> {@code
|
|
78 |
* module m1 { requires m2; requires java.xml; }
|
|
79 |
* module m2 { }
|
|
80 |
* } </pre>
|
|
81 |
*
|
|
82 |
* <p> If module {@code m1} is resolved with the configuration for the {@link
|
|
83 |
* java.lang.reflect.Layer#boot() boot} layer as the parent then the resulting
|
|
84 |
* configuration contains two modules ({@code m1}, {@code m2}). The edges in
|
|
85 |
* its readability graph are:
|
|
86 |
* <pre> {@code
|
|
87 |
* m1 --> m2
|
|
88 |
* m1 --> java.xml
|
|
89 |
* } </pre>
|
|
90 |
* where module {@code java.xml} is in the parent configuration. For
|
|
91 |
* simplicity, this example omits the implicitly declared dependence on the
|
|
92 |
* {@code java.base} module.
|
|
93 |
*
|
|
94 |
* <a name="automaticmoduleresolution"></a>
|
|
95 |
* <p> {@link ModuleDescriptor#isAutomatic() Automatic} modules receive special
|
|
96 |
* treatment during resolution. Each automatic module is resolved so that it
|
|
97 |
* reads all other modules in the configuration and all parent configurations.
|
|
98 |
* Each automatic module is also resolved as if it {@code requires public} all
|
|
99 |
* other automatic modules in the configuration (and all automatic modules in
|
|
100 |
* parent configurations). </p>
|
|
101 |
|
|
102 |
* <h2><a name="servicebinding">Service binding</a></h2>
|
|
103 |
*
|
|
104 |
* <p> Service binding is the process of augmenting a graph of resolved modules
|
|
105 |
* from the set of observable modules induced by the service-use dependence
|
|
106 |
* ({@code uses} and {@code provides} clauses). Any module that was not
|
|
107 |
* previously in the graph requires resolution to compute its transitive
|
|
108 |
* closure. Service binding is an iterative process in that adding a module
|
|
109 |
* that satisfies some service-use dependence may introduce new service-use
|
|
110 |
* dependences. </p>
|
|
111 |
*
|
|
112 |
* <p> Suppose we have the following observable modules: </p>
|
|
113 |
* <pre> {@code
|
|
114 |
* module m1 { exports p; uses p.S; }
|
|
115 |
* module m2 { requires m1; provides p.S with p2.S2; }
|
|
116 |
* module m3 { requires m1; requires m4; provides p.S with p3.S3; }
|
|
117 |
* module m4 { }
|
|
118 |
* } </pre>
|
|
119 |
*
|
|
120 |
* <p> If the module {@code m1} is resolved then the resulting graph of modules
|
|
121 |
* has one module ({@code m1}). If the graph is augmented with modules induced
|
|
122 |
* by the service-use dependence relation then the configuration will contain
|
|
123 |
* four modules ({@code m1}, {@code m2}, {@code m3}, {@code m4}). The edges in
|
|
124 |
* its readability graph are: </p>
|
|
125 |
* <pre> {@code
|
|
126 |
* m2 --> m1
|
|
127 |
* m3 --> m1
|
|
128 |
* m3 --> m4
|
|
129 |
* } </pre>
|
|
130 |
* <p> The edges in the conceptual service-use graph are: </p>
|
|
131 |
* <pre> {@code
|
|
132 |
* m1 --> m2 (meaning m1 uses a service that is provided by m2)
|
|
133 |
* m1 --> m3
|
|
134 |
* } </pre>
|
|
135 |
*
|
|
136 |
* <p> If this configuration is instantiated as a {@code Layer}, and if code in
|
|
137 |
* module {@code m1} uses {@link java.util.ServiceLoader ServiceLoader} to
|
|
138 |
* iterate over implementations of {@code p.S.class}, then it will iterate over
|
|
139 |
* an instance of {@code p2.S2} and {@code p3.S3}. </p>
|
|
140 |
*
|
|
141 |
* <h3> Example </h3>
|
|
142 |
*
|
|
143 |
* <p> The following example uses the {@code resolveRequires} method to resolve
|
|
144 |
* a module named <em>myapp</em> with the configuration for the boot layer as
|
|
145 |
* the parent configuration. It prints the name of each resolved module and
|
|
146 |
* the names of the modules that each module reads. </p>
|
|
147 |
*
|
|
148 |
* <pre>{@code
|
|
149 |
* ModuleFinder finder = ModuleFinder.of(dir1, dir2, dir3);
|
|
150 |
*
|
|
151 |
* Configuration parent = Layer.boot().configuration();
|
|
152 |
*
|
|
153 |
* Configuration cf = parent.resolveRequires(finder,
|
|
154 |
* ModuleFinder.empty(),
|
|
155 |
* Set.of("myapp"));
|
|
156 |
* cf.modules().forEach(m -> {
|
|
157 |
* System.out.format("%s -> %s%n",
|
|
158 |
* m.name(),
|
|
159 |
* m.reads().stream()
|
|
160 |
* .map(ResolvedModule::name)
|
|
161 |
* .collect(Collectors.joining(", ")));
|
|
162 |
* });
|
|
163 |
* }</pre>
|
|
164 |
*
|
|
165 |
* @since 9
|
|
166 |
* @see java.lang.reflect.Layer
|
|
167 |
*/
|
|
168 |
public final class Configuration {
|
|
169 |
|
|
170 |
// @see Configuration#empty()
|
|
171 |
private static final Configuration EMPTY_CONFIGURATION = new Configuration();
|
|
172 |
|
|
173 |
private final Configuration parent;
|
|
174 |
|
|
175 |
private final Map<ResolvedModule, Set<ResolvedModule>> graph;
|
|
176 |
private final Set<ResolvedModule> modules;
|
|
177 |
private final Map<String, ResolvedModule> nameToModule;
|
|
178 |
|
|
179 |
private Configuration() {
|
|
180 |
this.parent = null;
|
|
181 |
this.graph = Collections.emptyMap();
|
|
182 |
this.modules = Collections.emptySet();
|
|
183 |
this.nameToModule = Collections.emptyMap();
|
|
184 |
}
|
|
185 |
|
|
186 |
private Configuration(Configuration parent, Resolver resolver) {
|
|
187 |
Map<ResolvedModule, Set<ResolvedModule>> graph = resolver.finish(this);
|
|
188 |
|
|
189 |
Map<String, ResolvedModule> nameToModule = new HashMap<>();
|
|
190 |
for (ResolvedModule resolvedModule : graph.keySet()) {
|
|
191 |
nameToModule.put(resolvedModule.name(), resolvedModule);
|
|
192 |
}
|
|
193 |
|
|
194 |
this.parent = parent;
|
|
195 |
this.graph = graph;
|
|
196 |
this.modules = Collections.unmodifiableSet(graph.keySet());
|
|
197 |
this.nameToModule = Collections.unmodifiableMap(nameToModule);
|
|
198 |
}
|
|
199 |
|
|
200 |
|
|
201 |
/**
|
|
202 |
* Resolves a collection of root modules, with this configuration as its
|
|
203 |
* parent, to create a new configuration.
|
|
204 |
*
|
|
205 |
* <p> Each root module is located using the given {@code before} module
|
|
206 |
* finder. If a module is not found then it is located in the parent
|
|
207 |
* configuration as if by invoking the {@link #findModule(String)
|
|
208 |
* findModule} method. If not found then the module is located using the
|
|
209 |
* given {@code after} module finder. The same search order is used to
|
|
210 |
* locate transitive dependences. Root modules or dependences that are
|
|
211 |
* located in a parent configuration are resolved no further and are not
|
|
212 |
* included in the resulting configuration. </p>
|
|
213 |
*
|
|
214 |
* <p> When all modules have been resolved then the resulting dependency
|
|
215 |
* graph is checked to ensure that it does not contain cycles. A
|
|
216 |
* readability graph is constructed and then, in conjunction with the
|
|
217 |
* module exports and service use, checked for consistency. </p>
|
|
218 |
*
|
|
219 |
* <p> Resolution and the (post-resolution) consistency checks may fail for
|
|
220 |
* following reasons: </p>
|
|
221 |
*
|
|
222 |
* <ul>
|
|
223 |
* <li><p> A root module, or a direct or transitive dependency, is not
|
|
224 |
* found. </p></li>
|
|
225 |
*
|
|
226 |
* <li><p> An error occurs when attempting to find a module.
|
|
227 |
* Possible errors include I/O errors, errors detected parsing a module
|
|
228 |
* descriptor ({@code module-info.class}) or two versions of the same
|
|
229 |
* module are found in the same directory. </p></li>
|
|
230 |
*
|
|
231 |
* <li><p> A cycle is detected, say where module {@code m1} requires
|
|
232 |
* module {@code m2} and {@code m2} requires {@code m1}. </p></li>
|
|
233 |
*
|
|
234 |
* <li><p> Two or more modules in the configuration export the same
|
|
235 |
* package to a module that reads both. This includes the case where a
|
|
236 |
* module {@code M} containing package {@code p} reads another module
|
|
237 |
* that exports {@code p} to {@code M}. </p></li>
|
|
238 |
*
|
|
239 |
* <li><p> A module {@code M} declares that it "{@code uses p.S}" or
|
|
240 |
* "{@code provides p.S with ...}" but package {@code p} is neither in
|
|
241 |
* module {@code M} nor exported to {@code M} by any module that
|
|
242 |
* {@code M} reads. </p></li>
|
|
243 |
*
|
|
244 |
* <li><p> A module {@code M} declares that it
|
|
245 |
* "{@code provides ... with q.T}" but package {@code q} is not in
|
|
246 |
* module {@code M}. </p></li>
|
|
247 |
*
|
|
248 |
* <li><p> Two or more modules in the configuration are specific to
|
|
249 |
* different {@link ModuleDescriptor#osName() operating systems},
|
|
250 |
* {@link ModuleDescriptor#osArch() architectures}, or {@link
|
|
251 |
* ModuleDescriptor#osVersion() versions}. </p></li>
|
|
252 |
*
|
|
253 |
* <li><p> Other implementation specific checks, for example referential
|
|
254 |
* integrity checks to ensure that different versions of tighly coupled
|
|
255 |
* modules cannot be combined in the same configuration. </p></li>
|
|
256 |
*
|
|
257 |
* </ul>
|
|
258 |
*
|
|
259 |
* @param before
|
|
260 |
* The <em>before</em> module finder to find modules
|
|
261 |
* @param after
|
|
262 |
* The <em>after</em> module finder to locate modules when not
|
|
263 |
* located by the {@code before} module finder or in parent
|
|
264 |
* configurations
|
|
265 |
* @param roots
|
|
266 |
* The possibly-empty collection of module names of the modules
|
|
267 |
* to resolve
|
|
268 |
*
|
|
269 |
* @return The configuration that is the result of resolving the given
|
|
270 |
* root modules
|
|
271 |
*
|
|
272 |
* @throws ResolutionException
|
|
273 |
* If resolution or the post-resolution checks fail for any of the
|
|
274 |
* reasons listed
|
|
275 |
* @throws SecurityException
|
|
276 |
* If locating a module is denied by the security manager
|
|
277 |
*/
|
|
278 |
public Configuration resolveRequires(ModuleFinder before,
|
|
279 |
ModuleFinder after,
|
|
280 |
Collection<String> roots)
|
|
281 |
{
|
|
282 |
Objects.requireNonNull(before);
|
|
283 |
Objects.requireNonNull(after);
|
|
284 |
Objects.requireNonNull(roots);
|
|
285 |
|
|
286 |
Resolver resolver = new Resolver(before, this, after);
|
|
287 |
resolver.resolveRequires(roots);
|
|
288 |
|
|
289 |
return new Configuration(this, resolver);
|
|
290 |
}
|
|
291 |
|
|
292 |
|
|
293 |
/**
|
|
294 |
* Resolves a collection of root modules, with service binding, and with
|
|
295 |
* this configuration as its parent, to create a new configuration.
|
|
296 |
*
|
|
297 |
* <p> This method works exactly as specified by {@link #resolveRequires
|
|
298 |
* resolveRequires} except that the graph of resolved modules is augmented
|
|
299 |
* with modules induced by the service-use dependence relation. </p>
|
|
300 |
*
|
|
301 |
* <p> More specifically, the root modules are resolved as if by calling
|
|
302 |
* {@code resolveRequires}. The resolved modules, and all modules in the
|
|
303 |
* parent configurations, with {@link ModuleDescriptor#uses() service
|
|
304 |
* dependences} are then examined. All modules found by the given module
|
|
305 |
* finders that {@link ModuleDescriptor#provides() provide} an
|
|
306 |
* implementation of one or more of the service types are added to the
|
|
307 |
* module graph and then resolved as if by calling the {@code
|
|
308 |
* resolveRequires} method. Adding modules to the module graph may
|
|
309 |
* introduce new service-use dependences and so the process works
|
|
310 |
* iteratively until no more modules are added. </p>
|
|
311 |
*
|
|
312 |
* <p> As service binding involves resolution then it may fail with {@link
|
|
313 |
* ResolutionException} for exactly the same reasons specified in
|
|
314 |
* {@code resolveRequires}. </p>
|
|
315 |
*
|
|
316 |
* @param before
|
|
317 |
* The <em>before</em> module finder to find modules
|
|
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 |
*
|
|
329 |
* @throws ResolutionException
|
|
330 |
* If resolution or the post-resolution checks fail for any of the
|
|
331 |
* reasons listed
|
|
332 |
* @throws SecurityException
|
|
333 |
* If locating a module is denied by the security manager
|
|
334 |
*/
|
|
335 |
public Configuration resolveRequiresAndUses(ModuleFinder before,
|
|
336 |
ModuleFinder after,
|
|
337 |
Collection<String> roots)
|
|
338 |
{
|
|
339 |
Objects.requireNonNull(before);
|
|
340 |
Objects.requireNonNull(after);
|
|
341 |
Objects.requireNonNull(roots);
|
|
342 |
|
|
343 |
Resolver resolver = new Resolver(before, this, after);
|
|
344 |
resolver.resolveRequires(roots).resolveUses();
|
|
345 |
|
|
346 |
return new Configuration(this, resolver);
|
|
347 |
}
|
|
348 |
|
|
349 |
|
|
350 |
/**
|
|
351 |
* Returns the <em>empty</em> configuration. The empty configuration does
|
|
352 |
* not contain any modules and does not have a parent.
|
|
353 |
*
|
|
354 |
* @return The empty configuration
|
|
355 |
*/
|
|
356 |
public static Configuration empty() {
|
|
357 |
return EMPTY_CONFIGURATION;
|
|
358 |
}
|
|
359 |
|
|
360 |
|
|
361 |
/**
|
|
362 |
* Returns this configuration's parent unless this is the {@linkplain #empty
|
|
363 |
* empty configuration}, which has no parent.
|
|
364 |
*
|
|
365 |
* @return This configuration's parent
|
|
366 |
*/
|
|
367 |
public Optional<Configuration> parent() {
|
|
368 |
return Optional.ofNullable(parent);
|
|
369 |
}
|
|
370 |
|
|
371 |
|
|
372 |
/**
|
|
373 |
* Returns an immutable set of the resolved modules in this configuration.
|
|
374 |
*
|
|
375 |
* @return A possibly-empty unmodifiable set of the resolved modules
|
|
376 |
* in this configuration
|
|
377 |
*/
|
|
378 |
public Set<ResolvedModule> modules() {
|
|
379 |
return modules;
|
|
380 |
}
|
|
381 |
|
|
382 |
|
|
383 |
/**
|
|
384 |
* Finds a resolved module in this configuration, or if not in this
|
|
385 |
* configuration, the {@linkplain #parent parent} configurations.
|
|
386 |
*
|
|
387 |
* @param name
|
|
388 |
* The module name of the resolved module to find
|
|
389 |
*
|
|
390 |
* @return The resolved module with the given name or an empty {@code
|
|
391 |
* Optional} if there isn't a module with this name in this
|
|
392 |
* configuration or any parent configuration
|
|
393 |
*/
|
|
394 |
public Optional<ResolvedModule> findModule(String name) {
|
|
395 |
Objects.requireNonNull(name);
|
|
396 |
if (parent == null)
|
|
397 |
return Optional.empty();
|
|
398 |
ResolvedModule m = nameToModule.get(name);
|
|
399 |
if (m != null)
|
|
400 |
return Optional.of(m);
|
|
401 |
return parent().flatMap(x -> x.findModule(name));
|
|
402 |
}
|
|
403 |
|
|
404 |
|
|
405 |
Set<ModuleDescriptor> descriptors() {
|
|
406 |
if (modules.isEmpty()) {
|
|
407 |
return Collections.emptySet();
|
|
408 |
} else {
|
|
409 |
return modules.stream()
|
|
410 |
.map(ResolvedModule::reference)
|
|
411 |
.map(ModuleReference::descriptor)
|
|
412 |
.collect(Collectors.toSet());
|
|
413 |
}
|
|
414 |
}
|
|
415 |
|
|
416 |
Set<ResolvedModule> reads(ResolvedModule m) {
|
|
417 |
return Collections.unmodifiableSet(graph.get(m));
|
|
418 |
}
|
|
419 |
|
|
420 |
/**
|
|
421 |
* Returns a string describing this configuration.
|
|
422 |
*
|
|
423 |
* @return A string describing this configuration
|
|
424 |
*/
|
|
425 |
@Override
|
|
426 |
public String toString() {
|
|
427 |
return modules().stream()
|
|
428 |
.map(ResolvedModule::name)
|
|
429 |
.collect(Collectors.joining(", "));
|
|
430 |
}
|
|
431 |
}
|