author | sdrach |
Tue, 19 Jul 2016 10:05:22 -0700 | |
changeset 39758 | 28c2c63fc09f |
parent 39050 | 9de41b79ec7e |
child 41817 | b90ad1de93ea |
permissions | -rw-r--r-- |
36511 | 1 |
/* |
2 |
* Copyright (c) 2009, 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.io.InputStream; |
|
29 |
import java.io.IOException; |
|
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
30 |
import java.io.PrintStream; |
36511 | 31 |
import java.io.UncheckedIOException; |
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
32 |
import java.net.URI; |
36511 | 33 |
import java.nio.ByteBuffer; |
34 |
import java.util.ArrayList; |
|
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
35 |
import java.util.Collection; |
36511 | 36 |
import java.util.Collections; |
37 |
import java.util.EnumSet; |
|
38 |
import java.util.HashMap; |
|
39 |
import java.util.HashSet; |
|
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
40 |
import java.util.LinkedHashSet; |
36511 | 41 |
import java.util.List; |
42 |
import java.util.Map; |
|
43 |
import java.util.Objects; |
|
44 |
import java.util.Optional; |
|
45 |
import java.util.Set; |
|
46 |
import java.util.function.Supplier; |
|
47 |
||
48 |
import static jdk.internal.module.Checks.*; |
|
49 |
import static java.util.Objects.*; |
|
50 |
||
51 |
import jdk.internal.module.Checks; |
|
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
52 |
import jdk.internal.module.ModuleHashes; |
36511 | 53 |
|
54 |
||
55 |
/** |
|
56 |
* A module descriptor. |
|
57 |
* |
|
58 |
* <p> A {@code ModuleDescriptor} is typically created from the binary form |
|
59 |
* of a module declaration. The associated {@link ModuleDescriptor.Builder} |
|
60 |
* class can also be used to create a {@code ModuleDescriptor} from its |
|
61 |
* components. </p> |
|
62 |
* |
|
63 |
* <p> {@code ModuleDescriptor} objects are immutable and safe for use by |
|
64 |
* multiple concurrent threads.</p> |
|
65 |
* |
|
66 |
* @since 9 |
|
67 |
* @see java.lang.reflect.Module |
|
68 |
*/ |
|
69 |
||
70 |
public class ModuleDescriptor |
|
71 |
implements Comparable<ModuleDescriptor> |
|
72 |
{ |
|
73 |
||
74 |
/** |
|
75 |
* <p> A dependence upon a module </p> |
|
76 |
* |
|
77 |
* @see ModuleDescriptor#requires() |
|
78 |
* @since 9 |
|
79 |
*/ |
|
80 |
||
81 |
public final static class Requires |
|
82 |
implements Comparable<Requires> |
|
83 |
{ |
|
84 |
||
85 |
/** |
|
86 |
* A modifier on a module dependence. |
|
87 |
* |
|
88 |
* @since 9 |
|
89 |
*/ |
|
90 |
public static enum Modifier { |
|
91 |
||
92 |
/** |
|
93 |
* The dependence causes any module which depends on the <i>current |
|
94 |
* module</i> to have an implicitly declared dependence on the module |
|
95 |
* named by the {@code Requires}. |
|
96 |
*/ |
|
97 |
PUBLIC, |
|
98 |
||
99 |
/** |
|
100 |
* The dependence was not explicitly or implicitly declared in the |
|
101 |
* source of the module declaration. |
|
102 |
*/ |
|
103 |
SYNTHETIC, |
|
104 |
||
105 |
/** |
|
106 |
* The dependence was implicitly declared in the source of the module |
|
107 |
* declaration. |
|
108 |
*/ |
|
109 |
MANDATED; |
|
110 |
||
111 |
} |
|
112 |
||
113 |
private final Set<Modifier> mods; |
|
114 |
private final String name; |
|
115 |
||
116 |
private Requires(Set<Modifier> ms, String mn) { |
|
117 |
this(ms, mn, true); |
|
118 |
} |
|
119 |
private Requires(Set<Modifier> ms, String mn, boolean check) { |
|
120 |
if (ms == null || ms.isEmpty()) { |
|
121 |
mods = Collections.emptySet(); |
|
122 |
} else { |
|
123 |
mods = check ? Collections.unmodifiableSet(EnumSet.copyOf(ms)) |
|
124 |
: ms; |
|
125 |
} |
|
126 |
this.name = check ? requireModuleName(mn) : mn; |
|
127 |
} |
|
128 |
||
129 |
/** |
|
130 |
* Returns the set of modifiers. |
|
131 |
* |
|
132 |
* @return A possibly-empty unmodifiable set of modifiers |
|
133 |
*/ |
|
134 |
public Set<Modifier> modifiers() { |
|
135 |
return mods; |
|
136 |
} |
|
137 |
||
138 |
/** |
|
139 |
* Return the module name. |
|
140 |
* |
|
141 |
* @return The module name |
|
142 |
*/ |
|
143 |
public String name() { |
|
144 |
return name; |
|
145 |
} |
|
146 |
||
147 |
/** |
|
148 |
* Compares this module dependence to another. |
|
149 |
* |
|
150 |
* <p> Two {@code Requires} objects are compared by comparing their |
|
151 |
* module name lexicographically. Where the module names are equal then |
|
152 |
* the sets of modifiers are compared based on a value computed from the |
|
153 |
* ordinal of each modifier. </p> |
|
154 |
* |
|
155 |
* @return A negative integer, zero, or a positive integer if this module |
|
156 |
* dependence is less than, equal to, or greater than the given |
|
157 |
* module dependence |
|
158 |
*/ |
|
159 |
@Override |
|
160 |
public int compareTo(Requires that) { |
|
161 |
int c = this.name().compareTo(that.name()); |
|
162 |
if (c != 0) |
|
163 |
return c; |
|
164 |
// same name, compare by modifiers |
|
165 |
return Long.compare(this.modsValue(), that.modsValue()); |
|
166 |
} |
|
167 |
||
168 |
/** |
|
169 |
* Return a value for the modifiers to allow sets of modifiers to be |
|
170 |
* compared. |
|
171 |
*/ |
|
172 |
private long modsValue() { |
|
173 |
long value = 0; |
|
174 |
for (Modifier m : mods) { |
|
175 |
value += 1 << m.ordinal(); |
|
176 |
} |
|
177 |
return value; |
|
178 |
} |
|
179 |
||
180 |
/** |
|
181 |
* Tests this module dependence for equality with the given object. |
|
182 |
* |
|
183 |
* <p> If the given object is not a {@code Requires} then this method |
|
184 |
* returns {@code false}. Two module dependence objects are equal if |
|
185 |
* the module names are equal and set of modifiers are equal. </p> |
|
186 |
* |
|
187 |
* <p> This method satisfies the general contract of the {@link |
|
188 |
* java.lang.Object#equals(Object) Object.equals} method. </p> |
|
189 |
* |
|
190 |
* @param ob |
|
191 |
* the object to which this object is to be compared |
|
192 |
* |
|
193 |
* @return {@code true} if, and only if, the given object is a module |
|
194 |
* dependence that is equal to this module dependence |
|
195 |
*/ |
|
196 |
@Override |
|
197 |
public boolean equals(Object ob) { |
|
198 |
if (!(ob instanceof Requires)) |
|
199 |
return false; |
|
200 |
Requires that = (Requires)ob; |
|
201 |
return (name.equals(that.name) && mods.equals(that.mods)); |
|
202 |
} |
|
203 |
||
204 |
/** |
|
205 |
* Computes a hash code for this module dependence. |
|
206 |
* |
|
207 |
* <p> The hash code is based upon the module name and modifiers. It |
|
208 |
* satisfies the general contract of the {@link Object#hashCode |
|
209 |
* Object.hashCode} method. </p> |
|
210 |
* |
|
211 |
* @return The hash-code value for this module dependence |
|
212 |
*/ |
|
213 |
@Override |
|
214 |
public int hashCode() { |
|
215 |
return name.hashCode() * 43 + mods.hashCode(); |
|
216 |
} |
|
217 |
||
218 |
/** |
|
219 |
* Returns a string describing module dependence. |
|
220 |
* |
|
221 |
* @return A string describing module dependence |
|
222 |
*/ |
|
223 |
@Override |
|
224 |
public String toString() { |
|
225 |
return Dependence.toString(mods, name); |
|
226 |
} |
|
227 |
||
228 |
} |
|
229 |
||
230 |
||
231 |
||
232 |
/** |
|
233 |
* <p> A module export, may be qualified or unqualified. </p> |
|
234 |
* |
|
235 |
* @see ModuleDescriptor#exports() |
|
236 |
* @since 9 |
|
237 |
*/ |
|
238 |
||
239 |
public final static class Exports { |
|
240 |
||
241 |
private final String source; |
|
242 |
private final Set<String> targets; // empty if unqualified export |
|
243 |
||
244 |
/** |
|
245 |
* Constructs a qualified export. |
|
246 |
*/ |
|
247 |
private Exports(String source, Set<String> targets) { |
|
248 |
this(source, targets, true); |
|
249 |
} |
|
250 |
||
251 |
private Exports(String source, Set<String> targets, boolean check) { |
|
252 |
this.source = check ? requirePackageName(source) : source; |
|
253 |
targets = check ? Collections.unmodifiableSet(new HashSet<>(targets)) |
|
254 |
: Collections.unmodifiableSet(targets); |
|
255 |
if (targets.isEmpty()) |
|
256 |
throw new IllegalArgumentException("Empty target set"); |
|
257 |
if (check) |
|
258 |
targets.stream().forEach(Checks::requireModuleName); |
|
259 |
this.targets = targets; |
|
260 |
} |
|
261 |
||
262 |
/** |
|
263 |
* Constructs an unqualified export. |
|
264 |
*/ |
|
265 |
private Exports(String source) { |
|
266 |
this(source, true); |
|
267 |
} |
|
268 |
private Exports(String source, boolean check) { |
|
269 |
this.source = check ? requirePackageName(source) : source; |
|
270 |
this.targets = Collections.emptySet(); |
|
271 |
} |
|
272 |
||
273 |
/** |
|
274 |
* Returns {@code true} if this is a qualified export. |
|
275 |
* |
|
276 |
* @return {@code true} if this is a qualified export |
|
277 |
*/ |
|
278 |
public boolean isQualified() { |
|
279 |
return !targets.isEmpty(); |
|
280 |
} |
|
281 |
||
282 |
/** |
|
283 |
* Returns the package name. |
|
284 |
* |
|
285 |
* @return The package name |
|
286 |
*/ |
|
287 |
public String source() { |
|
288 |
return source; |
|
289 |
} |
|
290 |
||
291 |
/** |
|
292 |
* For a qualified export, returns the non-empty and immutable set |
|
293 |
* of the module names to which the package is exported. For an |
|
294 |
* unqualified export, returns an empty set. |
|
295 |
* |
|
296 |
* @return The set of target module names or for an unqualified |
|
297 |
* export, an empty set |
|
298 |
*/ |
|
299 |
public Set<String> targets() { |
|
300 |
return targets; |
|
301 |
} |
|
302 |
||
303 |
/** |
|
304 |
* Computes a hash code for this module export. |
|
305 |
* |
|
306 |
* <p> The hash code is based upon the package name, and for a |
|
307 |
* qualified export, the set of modules names to which the package |
|
308 |
* is exported. It satisfies the general contract of the {@link |
|
309 |
* Object#hashCode Object.hashCode} method. |
|
310 |
* |
|
311 |
* @return The hash-code value for this module export |
|
312 |
*/ |
|
313 |
@Override |
|
314 |
public int hashCode() { |
|
315 |
return hash(source, targets); |
|
316 |
} |
|
317 |
||
318 |
/** |
|
319 |
* Tests this module export for equality with the given object. |
|
320 |
* |
|
37880 | 321 |
* <p> If the given object is not an {@code Exports} then this method |
36511 | 322 |
* returns {@code false}. Two module exports objects are equal if the |
323 |
* package names are equal and the set of target module names is equal. |
|
324 |
* </p> |
|
325 |
* |
|
326 |
* <p> This method satisfies the general contract of the {@link |
|
327 |
* java.lang.Object#equals(Object) Object.equals} method. </p> |
|
328 |
* |
|
329 |
* @param ob |
|
330 |
* the object to which this object is to be compared |
|
331 |
* |
|
332 |
* @return {@code true} if, and only if, the given object is a module |
|
333 |
* dependence that is equal to this module dependence |
|
334 |
*/ |
|
335 |
@Override |
|
336 |
public boolean equals(Object ob) { |
|
337 |
if (!(ob instanceof Exports)) |
|
338 |
return false; |
|
339 |
Exports other = (Exports)ob; |
|
340 |
return Objects.equals(this.source, other.source) && |
|
341 |
Objects.equals(this.targets, other.targets); |
|
342 |
} |
|
343 |
||
344 |
/** |
|
345 |
* Returns a string describing module export. |
|
346 |
* |
|
347 |
* @return A string describing module export |
|
348 |
*/ |
|
349 |
@Override |
|
350 |
public String toString() { |
|
351 |
if (targets.isEmpty()) |
|
352 |
return source; |
|
353 |
else |
|
354 |
return source + " to " + targets; |
|
355 |
} |
|
356 |
||
357 |
} |
|
358 |
||
359 |
||
360 |
||
361 |
/** |
|
362 |
* <p> A service that a module provides one or more implementations of. </p> |
|
363 |
* |
|
364 |
* @see ModuleDescriptor#provides() |
|
365 |
* @since 9 |
|
366 |
*/ |
|
367 |
||
368 |
public final static class Provides { |
|
369 |
||
370 |
private final String service; |
|
371 |
private final Set<String> providers; |
|
372 |
||
373 |
private Provides(String service, Set<String> providers) { |
|
374 |
this(service, providers, true); |
|
375 |
} |
|
376 |
||
377 |
private Provides(String service, Set<String> providers, boolean check) { |
|
378 |
this.service = check ? requireServiceTypeName(service) : service; |
|
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
379 |
providers = check |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
380 |
? Collections.unmodifiableSet(new LinkedHashSet<>(providers)) |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
381 |
: Collections.unmodifiableSet(providers); |
36511 | 382 |
if (providers.isEmpty()) |
383 |
throw new IllegalArgumentException("Empty providers set"); |
|
384 |
if (check) |
|
385 |
providers.forEach(Checks::requireServiceProviderName); |
|
386 |
this.providers = providers; |
|
387 |
} |
|
388 |
||
389 |
/** |
|
390 |
* Returns the fully qualified class name of the service type. |
|
391 |
* |
|
392 |
* @return The fully qualified class name of the service type. |
|
393 |
*/ |
|
394 |
public String service() { return service; } |
|
395 |
||
396 |
/** |
|
397 |
* Returns the set of the fully qualified class names of the providers. |
|
398 |
* |
|
399 |
* @return A non-empty and unmodifiable set of the fully qualified class |
|
400 |
* names of the providers. |
|
401 |
*/ |
|
402 |
public Set<String> providers() { return providers; } |
|
403 |
||
404 |
/** |
|
405 |
* Computes a hash code for this provides. |
|
406 |
* |
|
407 |
* <p> The hash code is based upon the service type and the set of |
|
408 |
* providers. It satisfies the general contract of the {@link |
|
409 |
* Object#hashCode Object.hashCode} method. </p> |
|
410 |
* |
|
411 |
* @return The hash-code value for this module provides |
|
412 |
*/ |
|
413 |
@Override |
|
414 |
public int hashCode() { |
|
415 |
return hash(service, providers); |
|
416 |
} |
|
417 |
||
418 |
/** |
|
419 |
* Tests this provides for equality with the given object. |
|
420 |
* |
|
421 |
* <p> If the given object is not a {@code Provides} then this method |
|
422 |
* returns {@code false}. Two {@code Provides} objects are equal if the |
|
423 |
* service type is equal and the set of providers is equal. </p> |
|
424 |
* |
|
425 |
* <p> This method satisfies the general contract of the {@link |
|
426 |
* java.lang.Object#equals(Object) Object.equals} method. </p> |
|
427 |
* |
|
428 |
* @param ob |
|
429 |
* the object to which this object is to be compared |
|
430 |
* |
|
431 |
* @return {@code true} if, and only if, the given object is a |
|
432 |
* {@code Provides} that is equal to this {@code Provides} |
|
433 |
*/ |
|
434 |
@Override |
|
435 |
public boolean equals(Object ob) { |
|
436 |
if (!(ob instanceof Provides)) |
|
437 |
return false; |
|
438 |
Provides other = (Provides)ob; |
|
439 |
return Objects.equals(this.service, other.service) && |
|
440 |
Objects.equals(this.providers, other.providers); |
|
441 |
} |
|
442 |
||
443 |
/** |
|
444 |
* Returns a string describing this provides. |
|
445 |
* |
|
446 |
* @return A string describing this provides |
|
447 |
*/ |
|
448 |
@Override |
|
449 |
public String toString() { |
|
450 |
return service + " with " + providers; |
|
451 |
} |
|
452 |
||
453 |
} |
|
454 |
||
455 |
||
456 |
||
457 |
/** |
|
458 |
* A module's version string. |
|
459 |
* |
|
460 |
* <p> A version string has three components: The version number itself, an |
|
461 |
* optional pre-release version, and an optional build version. Each |
|
462 |
* component is sequence of tokens; each token is either a non-negative |
|
463 |
* integer or a string. Tokens are separated by the punctuation characters |
|
464 |
* {@code '.'}, {@code '-'}, or {@code '+'}, or by transitions from a |
|
465 |
* sequence of digits to a sequence of characters that are neither digits |
|
466 |
* nor punctuation characters, or vice versa. |
|
467 |
* |
|
468 |
* <ul> |
|
469 |
* |
|
470 |
* <li> The <i>version number</i> is a sequence of tokens separated by |
|
471 |
* {@code '.'} characters, terminated by the first {@code '-'} or {@code |
|
472 |
* '+'} character. </li> |
|
473 |
* |
|
474 |
* <li> The <i>pre-release version</i> is a sequence of tokens separated |
|
475 |
* by {@code '.'} or {@code '-'} characters, terminated by the first |
|
476 |
* {@code '+'} character. </li> |
|
477 |
* |
|
478 |
* <li> The <i>build version</i> is a sequence of tokens separated by |
|
479 |
* {@code '.'}, {@code '-'}, or {@code '+'} characters. |
|
480 |
* |
|
481 |
* </ul> |
|
482 |
* |
|
483 |
* <p> When comparing two version strings, the elements of their |
|
484 |
* corresponding components are compared in pointwise fashion. If one |
|
485 |
* component is longer than the other, but otherwise equal to it, then the |
|
486 |
* first component is considered the greater of the two; otherwise, if two |
|
487 |
* corresponding elements are integers then they are compared as such; |
|
488 |
* otherwise, at least one of the elements is a string, so the other is |
|
489 |
* converted into a string if it is an integer and the two are compared |
|
490 |
* lexicographically. Trailing integer elements with the value zero are |
|
491 |
* ignored. |
|
492 |
* |
|
493 |
* <p> Given two version strings, if their version numbers differ then the |
|
494 |
* result of comparing them is the result of comparing their version |
|
495 |
* numbers; otherwise, if one of them has a pre-release version but the |
|
496 |
* other does not then the first is considered to precede the second, |
|
497 |
* otherwise the result of comparing them is the result of comparing their |
|
498 |
* pre-release versions; otherwise, the result of comparing them is the |
|
499 |
* result of comparing their build versions. |
|
500 |
* |
|
501 |
* @see ModuleDescriptor#version() |
|
502 |
* @since 9 |
|
503 |
*/ |
|
504 |
||
505 |
public final static class Version |
|
506 |
implements Comparable<Version> |
|
507 |
{ |
|
508 |
||
509 |
private final String version; |
|
510 |
||
511 |
// If Java had disjunctive types then we'd write List<Integer|String> here |
|
512 |
// |
|
513 |
private final List<Object> sequence; |
|
514 |
private final List<Object> pre; |
|
515 |
private final List<Object> build; |
|
516 |
||
517 |
// Take a numeric token starting at position i |
|
518 |
// Append it to the given list |
|
519 |
// Return the index of the first character not taken |
|
520 |
// Requires: s.charAt(i) is (decimal) numeric |
|
521 |
// |
|
522 |
private static int takeNumber(String s, int i, List<Object> acc) { |
|
523 |
char c = s.charAt(i); |
|
524 |
int d = (c - '0'); |
|
525 |
int n = s.length(); |
|
526 |
while (++i < n) { |
|
527 |
c = s.charAt(i); |
|
528 |
if (c >= '0' && c <= '9') { |
|
529 |
d = d * 10 + (c - '0'); |
|
530 |
continue; |
|
531 |
} |
|
532 |
break; |
|
533 |
} |
|
534 |
acc.add(d); |
|
535 |
return i; |
|
536 |
} |
|
537 |
||
538 |
// Take a string token starting at position i |
|
539 |
// Append it to the given list |
|
540 |
// Return the index of the first character not taken |
|
541 |
// Requires: s.charAt(i) is not '.' |
|
542 |
// |
|
543 |
private static int takeString(String s, int i, List<Object> acc) { |
|
544 |
int b = i; |
|
545 |
int n = s.length(); |
|
546 |
while (++i < n) { |
|
547 |
char c = s.charAt(i); |
|
548 |
if (c != '.' && c != '-' && c != '+' && !(c >= '0' && c <= '9')) |
|
549 |
continue; |
|
550 |
break; |
|
551 |
} |
|
552 |
acc.add(s.substring(b, i)); |
|
553 |
return i; |
|
554 |
} |
|
555 |
||
556 |
// Syntax: tok+ ( '-' tok+)? ( '+' tok+)? |
|
557 |
// First token string is sequence, second is pre, third is build |
|
558 |
// Tokens are separated by '.' or '-', or by changes between alpha & numeric |
|
559 |
// Numeric tokens are compared as decimal integers |
|
560 |
// Non-numeric tokens are compared lexicographically |
|
561 |
// A version with a non-empty pre is less than a version with same seq but no pre |
|
562 |
// Tokens in build may contain '-' and '+' |
|
563 |
// |
|
564 |
private Version(String v) { |
|
565 |
||
566 |
if (v == null) |
|
567 |
throw new IllegalArgumentException("Null version string"); |
|
568 |
int n = v.length(); |
|
569 |
if (n == 0) |
|
570 |
throw new IllegalArgumentException("Empty version string"); |
|
571 |
||
572 |
int i = 0; |
|
573 |
char c = v.charAt(i); |
|
574 |
if (!(c >= '0' && c <= '9')) |
|
575 |
throw new IllegalArgumentException(v |
|
576 |
+ ": Version string does not start" |
|
577 |
+ " with a number"); |
|
578 |
||
579 |
List<Object> sequence = new ArrayList<>(4); |
|
580 |
List<Object> pre = new ArrayList<>(2); |
|
581 |
List<Object> build = new ArrayList<>(2); |
|
582 |
||
583 |
i = takeNumber(v, i, sequence); |
|
584 |
||
585 |
while (i < n) { |
|
586 |
c = v.charAt(i); |
|
587 |
if (c == '.') { |
|
588 |
i++; |
|
589 |
continue; |
|
590 |
} |
|
591 |
if (c == '-' || c == '+') { |
|
592 |
i++; |
|
593 |
break; |
|
594 |
} |
|
595 |
if (c >= '0' && c <= '9') |
|
596 |
i = takeNumber(v, i, sequence); |
|
597 |
else |
|
598 |
i = takeString(v, i, sequence); |
|
599 |
} |
|
600 |
||
601 |
if (c == '-' && i >= n) |
|
602 |
throw new IllegalArgumentException(v + ": Empty pre-release"); |
|
603 |
||
604 |
while (i < n) { |
|
605 |
c = v.charAt(i); |
|
606 |
if (c >= '0' && c <= '9') |
|
607 |
i = takeNumber(v, i, pre); |
|
608 |
else |
|
609 |
i = takeString(v, i, pre); |
|
610 |
if (i >= n) |
|
611 |
break; |
|
612 |
c = v.charAt(i); |
|
613 |
if (c == '.' || c == '-') { |
|
614 |
i++; |
|
615 |
continue; |
|
616 |
} |
|
617 |
if (c == '+') { |
|
618 |
i++; |
|
619 |
break; |
|
620 |
} |
|
621 |
} |
|
622 |
||
623 |
if (c == '+' && i >= n) |
|
624 |
throw new IllegalArgumentException(v + ": Empty pre-release"); |
|
625 |
||
626 |
while (i < n) { |
|
627 |
c = v.charAt(i); |
|
628 |
if (c >= '0' && c <= '9') |
|
629 |
i = takeNumber(v, i, build); |
|
630 |
else |
|
631 |
i = takeString(v, i, build); |
|
632 |
if (i >= n) |
|
633 |
break; |
|
634 |
c = v.charAt(i); |
|
635 |
if (c == '.' || c == '-' || c == '+') { |
|
636 |
i++; |
|
637 |
continue; |
|
638 |
} |
|
639 |
} |
|
640 |
||
641 |
this.version = v; |
|
642 |
this.sequence = sequence; |
|
643 |
this.pre = pre; |
|
644 |
this.build = build; |
|
645 |
} |
|
646 |
||
647 |
/** |
|
648 |
* Parses the given string as a version string. |
|
649 |
* |
|
650 |
* @param v |
|
651 |
* The string to parse |
|
652 |
* |
|
653 |
* @return The resulting {@code Version} |
|
654 |
* |
|
655 |
* @throws IllegalArgumentException |
|
656 |
* If {@code v} is {@code null}, an empty string, or cannot be |
|
657 |
* parsed as a version string |
|
658 |
*/ |
|
659 |
public static Version parse(String v) { |
|
660 |
return new Version(v); |
|
661 |
} |
|
662 |
||
663 |
@SuppressWarnings("unchecked") |
|
664 |
private int cmp(Object o1, Object o2) { |
|
665 |
return ((Comparable)o1).compareTo(o2); |
|
666 |
} |
|
667 |
||
668 |
private int compareTokens(List<Object> ts1, List<Object> ts2) { |
|
669 |
int n = Math.min(ts1.size(), ts2.size()); |
|
670 |
for (int i = 0; i < n; i++) { |
|
671 |
Object o1 = ts1.get(i); |
|
672 |
Object o2 = ts2.get(i); |
|
673 |
if ((o1 instanceof Integer && o2 instanceof Integer) |
|
674 |
|| (o1 instanceof String && o2 instanceof String)) |
|
675 |
{ |
|
676 |
int c = cmp(o1, o2); |
|
677 |
if (c == 0) |
|
678 |
continue; |
|
679 |
return c; |
|
680 |
} |
|
681 |
// Types differ, so convert number to string form |
|
682 |
int c = o1.toString().compareTo(o2.toString()); |
|
683 |
if (c == 0) |
|
684 |
continue; |
|
685 |
return c; |
|
686 |
} |
|
687 |
List<Object> rest = ts1.size() > ts2.size() ? ts1 : ts2; |
|
688 |
int e = rest.size(); |
|
689 |
for (int i = n; i < e; i++) { |
|
690 |
Object o = rest.get(i); |
|
691 |
if (o instanceof Integer && ((Integer)o) == 0) |
|
692 |
continue; |
|
693 |
return ts1.size() - ts2.size(); |
|
694 |
} |
|
695 |
return 0; |
|
696 |
} |
|
697 |
||
698 |
/** |
|
699 |
* Compares this module version to another module version. Module |
|
700 |
* versions are compared as described in the class description. |
|
701 |
* |
|
702 |
* @param that |
|
703 |
* The module version to compare |
|
704 |
* |
|
705 |
* @return A negative integer, zero, or a positive integer as this |
|
706 |
* module version is less than, equal to, or greater than the |
|
707 |
* given module version |
|
708 |
*/ |
|
709 |
@Override |
|
710 |
public int compareTo(Version that) { |
|
711 |
int c = compareTokens(this.sequence, that.sequence); |
|
712 |
if (c != 0) return c; |
|
713 |
if (this.pre.isEmpty()) { |
|
714 |
if (!that.pre.isEmpty()) return +1; |
|
715 |
} else { |
|
716 |
if (that.pre.isEmpty()) return -1; |
|
717 |
} |
|
718 |
c = compareTokens(this.pre, that.pre); |
|
719 |
if (c != 0) return c; |
|
720 |
return compareTokens(this.build, that.build); |
|
721 |
} |
|
722 |
||
723 |
/** |
|
724 |
* Tests this module version for equality with the given object. |
|
725 |
* |
|
726 |
* <p> If the given object is not a {@code Version} then this method |
|
727 |
* returns {@code false}. Two module version are equal if their |
|
728 |
* corresponding components are equal. </p> |
|
729 |
* |
|
730 |
* <p> This method satisfies the general contract of the {@link |
|
731 |
* java.lang.Object#equals(Object) Object.equals} method. </p> |
|
732 |
* |
|
733 |
* @param ob |
|
734 |
* the object to which this object is to be compared |
|
735 |
* |
|
736 |
* @return {@code true} if, and only if, the given object is a module |
|
737 |
* reference that is equal to this module reference |
|
738 |
*/ |
|
739 |
@Override |
|
740 |
public boolean equals(Object ob) { |
|
741 |
if (!(ob instanceof Version)) |
|
742 |
return false; |
|
743 |
return compareTo((Version)ob) == 0; |
|
744 |
} |
|
745 |
||
746 |
/** |
|
747 |
* Computes a hash code for this module version. |
|
748 |
* |
|
749 |
* <p> The hash code is based upon the components of the version and |
|
750 |
* satisfies the general contract of the {@link Object#hashCode |
|
751 |
* Object.hashCode} method. </p> |
|
752 |
* |
|
753 |
* @return The hash-code value for this module version |
|
754 |
*/ |
|
755 |
@Override |
|
756 |
public int hashCode() { |
|
757 |
return version.hashCode(); |
|
758 |
} |
|
759 |
||
760 |
/** |
|
761 |
* Returns the string from which this version was parsed. |
|
762 |
* |
|
763 |
* @return The string from which this version was parsed. |
|
764 |
*/ |
|
765 |
@Override |
|
766 |
public String toString() { |
|
767 |
return version; |
|
768 |
} |
|
769 |
||
770 |
} |
|
771 |
||
772 |
||
773 |
||
774 |
// From module declarations |
|
775 |
private final String name; |
|
776 |
private final Set<Requires> requires; |
|
777 |
private final Set<Exports> exports; |
|
778 |
private final Set<String> uses; |
|
779 |
private final Map<String, Provides> provides; |
|
780 |
||
781 |
// Indicates if synthesised for a JAR file found on the module path |
|
782 |
private final boolean automatic; |
|
783 |
||
784 |
// Not generated from a module-info.java |
|
785 |
private final boolean synthetic; |
|
786 |
||
787 |
// "Extended" information, added post-compilation by tools |
|
788 |
private final Version version; |
|
789 |
private final String mainClass; |
|
790 |
private final String osName; |
|
791 |
private final String osArch; |
|
792 |
private final String osVersion; |
|
793 |
private final Set<String> packages; |
|
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
794 |
private final ModuleHashes hashes; |
36511 | 795 |
|
38888
dd584fbea6a2
8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents:
37880
diff
changeset
|
796 |
|
36511 | 797 |
private ModuleDescriptor(String name, |
798 |
boolean automatic, |
|
799 |
boolean synthetic, |
|
800 |
Map<String, Requires> requires, |
|
801 |
Set<String> uses, |
|
802 |
Map<String, Exports> exports, |
|
803 |
Map<String, Provides> provides, |
|
804 |
Version version, |
|
805 |
String mainClass, |
|
806 |
String osName, |
|
807 |
String osArch, |
|
808 |
String osVersion, |
|
38888
dd584fbea6a2
8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents:
37880
diff
changeset
|
809 |
Set<String> packages, |
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
810 |
ModuleHashes hashes) |
36511 | 811 |
{ |
812 |
||
813 |
this.name = name; |
|
814 |
this.automatic = automatic; |
|
815 |
this.synthetic = synthetic; |
|
816 |
||
817 |
Set<Requires> rqs = new HashSet<>(requires.values()); |
|
818 |
assert (rqs.stream().map(Requires::name).sorted().distinct().count() |
|
819 |
== rqs.size()) |
|
820 |
: "Module " + name + " has duplicate requires"; |
|
821 |
this.requires = emptyOrUnmodifiableSet(rqs); |
|
822 |
||
823 |
Set<Exports> exs = new HashSet<>(exports.values()); |
|
824 |
assert (exs.stream().map(Exports::source).sorted().distinct().count() |
|
825 |
== exs.size()) |
|
826 |
: "Module " + name + " has duplicate exports"; |
|
827 |
this.exports = emptyOrUnmodifiableSet(exs); |
|
828 |
||
829 |
this.uses = emptyOrUnmodifiableSet(uses); |
|
830 |
this.provides = emptyOrUnmodifiableMap(provides); |
|
831 |
||
832 |
this.version = version; |
|
833 |
this.mainClass = mainClass; |
|
834 |
this.osName = osName; |
|
835 |
this.osArch = osArch; |
|
836 |
this.osVersion = osVersion; |
|
837 |
this.hashes = hashes; |
|
838 |
||
38888
dd584fbea6a2
8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents:
37880
diff
changeset
|
839 |
this.packages = emptyOrUnmodifiableSet(packages); |
36511 | 840 |
} |
841 |
||
842 |
/** |
|
843 |
* Clones the given module descriptor with an augmented set of packages |
|
844 |
*/ |
|
845 |
ModuleDescriptor(ModuleDescriptor md, Set<String> pkgs) { |
|
846 |
this.name = md.name; |
|
847 |
this.automatic = md.automatic; |
|
848 |
this.synthetic = md.synthetic; |
|
849 |
||
850 |
this.requires = md.requires; |
|
851 |
this.exports = md.exports; |
|
852 |
this.uses = md.uses; |
|
853 |
this.provides = md.provides; |
|
854 |
||
855 |
this.version = md.version; |
|
856 |
this.mainClass = md.mainClass; |
|
857 |
this.osName = md.osName; |
|
858 |
this.osArch = md.osArch; |
|
859 |
this.osVersion = md.osVersion; |
|
860 |
this.hashes = null; // need to ignore |
|
861 |
||
38888
dd584fbea6a2
8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents:
37880
diff
changeset
|
862 |
Set<String> packages = new HashSet<>(md.packages); |
dd584fbea6a2
8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents:
37880
diff
changeset
|
863 |
packages.addAll(pkgs); |
dd584fbea6a2
8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents:
37880
diff
changeset
|
864 |
this.packages = emptyOrUnmodifiableSet(packages); |
36511 | 865 |
} |
866 |
||
867 |
/** |
|
868 |
* Creates a module descriptor from its components. This method is intended |
|
869 |
* for use by the jlink plugin. |
|
870 |
*/ |
|
871 |
ModuleDescriptor(String name, |
|
872 |
boolean automatic, |
|
873 |
boolean synthetic, |
|
874 |
Set<Requires> requires, |
|
875 |
Set<String> uses, |
|
876 |
Set<Exports> exports, |
|
877 |
Map<String, Provides> provides, |
|
878 |
Version version, |
|
879 |
String mainClass, |
|
880 |
String osName, |
|
881 |
String osArch, |
|
882 |
String osVersion, |
|
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
883 |
Set<String> packages, |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
884 |
ModuleHashes hashes) { |
36511 | 885 |
this.name = name; |
886 |
this.automatic = automatic; |
|
887 |
this.synthetic = synthetic; |
|
888 |
this.requires = Collections.unmodifiableSet(requires); |
|
889 |
this.exports = Collections.unmodifiableSet(exports); |
|
890 |
this.uses = Collections.unmodifiableSet(uses); |
|
891 |
this.provides = Collections.unmodifiableMap(provides); |
|
892 |
this.packages = Collections.unmodifiableSet(packages); |
|
893 |
||
894 |
this.version = version; |
|
895 |
this.mainClass = mainClass; |
|
896 |
this.osName = osName; |
|
897 |
this.osArch = osArch; |
|
898 |
this.osVersion = osVersion; |
|
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
899 |
this.hashes = hashes; |
36511 | 900 |
} |
901 |
||
902 |
/** |
|
903 |
* <p> The module name. </p> |
|
904 |
* |
|
905 |
* @return The module name |
|
906 |
*/ |
|
907 |
public String name() { |
|
908 |
return name; |
|
909 |
} |
|
910 |
||
911 |
/** |
|
912 |
* <p> Returns {@code true} if this is an automatic module. </p> |
|
913 |
* |
|
914 |
* <p> An automatic module is defined implicitly rather than explicitly |
|
915 |
* and therefore does not have a module declaration. JAR files located on |
|
916 |
* the application module path, or by the {@link ModuleFinder} returned by |
|
917 |
* {@link ModuleFinder#of(java.nio.file.Path[]) ModuleFinder.of}, are |
|
918 |
* treated as automatic modules if they do have not have a module |
|
919 |
* declaration. </p> |
|
920 |
* |
|
921 |
* @return {@code true} if this is an automatic module |
|
922 |
*/ |
|
923 |
public boolean isAutomatic() { |
|
924 |
return automatic; |
|
925 |
} |
|
926 |
||
927 |
/** |
|
928 |
* <p> Returns {@code true} if this module descriptor was not generated |
|
929 |
* from an explicit module declaration ({@code module-info.java}) |
|
930 |
* or an implicit module declaration (an {@link #isAutomatic() automatic} |
|
931 |
* module). </p> |
|
932 |
* |
|
933 |
* @return {@code true} if this module descriptor was not generated by |
|
934 |
* an explicit or implicit module declaration |
|
935 |
* |
|
936 |
* @jvms 4.7.8 The {@code Synthetic} Attribute |
|
937 |
*/ |
|
938 |
public boolean isSynthetic() { |
|
939 |
return synthetic; |
|
940 |
} |
|
941 |
||
942 |
/** |
|
943 |
* <p> The dependences of this module. </p> |
|
944 |
* |
|
945 |
* @return A possibly-empty unmodifiable set of {@link Requires} objects |
|
946 |
*/ |
|
947 |
public Set<Requires> requires() { |
|
948 |
return requires; |
|
949 |
} |
|
950 |
||
951 |
/** |
|
952 |
* <p> The service dependences of this module. </p> |
|
953 |
* |
|
954 |
* @return A possibly-empty unmodifiable set of the fully qualified class |
|
955 |
* names of the service types used |
|
956 |
*/ |
|
957 |
public Set<String> uses() { |
|
958 |
return uses; |
|
959 |
} |
|
960 |
||
961 |
/** |
|
962 |
* <p> The services that this module provides. </p> |
|
963 |
* |
|
964 |
* @return The possibly-empty unmodifiable map of the services that this |
|
965 |
* module provides. The map key is fully qualified class name of |
|
966 |
* the service type. |
|
967 |
*/ |
|
968 |
public Map<String, Provides> provides() { |
|
969 |
return provides; |
|
970 |
} |
|
971 |
||
972 |
/** |
|
973 |
* <p> The module exports. </p> |
|
974 |
* |
|
975 |
* @return A possibly-empty unmodifiable set of exported packages |
|
976 |
*/ |
|
977 |
public Set<Exports> exports() { |
|
978 |
return exports; |
|
979 |
} |
|
980 |
||
981 |
/** |
|
982 |
* Returns this module's version. |
|
983 |
* |
|
984 |
* @return This module's version |
|
985 |
*/ |
|
986 |
public Optional<Version> version() { |
|
987 |
return Optional.ofNullable(version); |
|
988 |
} |
|
989 |
||
990 |
/** |
|
991 |
* Returns a string containing this module's name and, if present, its |
|
992 |
* version. |
|
993 |
* |
|
994 |
* @return A string containing this module's name and, if present, its |
|
995 |
* version. |
|
996 |
*/ |
|
997 |
public String toNameAndVersion() { |
|
998 |
if (version != null) { |
|
999 |
return name() + "@" + version; |
|
1000 |
} else { |
|
1001 |
return name(); |
|
1002 |
} |
|
1003 |
} |
|
1004 |
||
1005 |
/** |
|
1006 |
* Returns the module's main class. |
|
1007 |
* |
|
1008 |
* @return The fully qualified class name of this module's main class |
|
1009 |
*/ |
|
1010 |
public Optional<String> mainClass() { |
|
1011 |
return Optional.ofNullable(mainClass); |
|
1012 |
} |
|
1013 |
||
1014 |
/** |
|
1015 |
* Returns the operating system name if this module is operating system |
|
1016 |
* specific. |
|
1017 |
* |
|
1018 |
* @return The operating system name or an empty {@code Optional} |
|
1019 |
* if this module is not operating system specific |
|
1020 |
*/ |
|
1021 |
public Optional<String> osName() { |
|
1022 |
return Optional.ofNullable(osName); |
|
1023 |
} |
|
1024 |
||
1025 |
/** |
|
1026 |
* Returns the operating system architecture if this module is operating |
|
1027 |
* system architecture specific. |
|
1028 |
* |
|
1029 |
* @return The operating system architecture or an empty {@code Optional} |
|
1030 |
* if this module is not operating system architecture specific |
|
1031 |
*/ |
|
1032 |
public Optional<String> osArch() { |
|
1033 |
return Optional.ofNullable(osArch); |
|
1034 |
} |
|
1035 |
||
1036 |
/** |
|
1037 |
* Returns the operating system version if this module is operating |
|
1038 |
* system version specific. |
|
1039 |
* |
|
1040 |
* @return The operating system version or an empty {@code Optional} |
|
1041 |
* if this module is not operating system version specific |
|
1042 |
*/ |
|
1043 |
public Optional<String> osVersion() { |
|
1044 |
return Optional.ofNullable(osVersion); |
|
1045 |
} |
|
1046 |
||
1047 |
/** |
|
1048 |
* Returns the names of the packages defined in, but not exported by, this |
|
1049 |
* module. |
|
1050 |
* |
|
1051 |
* @return A possibly-empty unmodifiable set of the concealed packages |
|
1052 |
*/ |
|
1053 |
public Set<String> conceals() { |
|
38888
dd584fbea6a2
8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents:
37880
diff
changeset
|
1054 |
Set<String> conceals = new HashSet<>(packages); |
dd584fbea6a2
8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents:
37880
diff
changeset
|
1055 |
exports.stream().map(Exports::source).forEach(conceals::remove); |
dd584fbea6a2
8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents:
37880
diff
changeset
|
1056 |
return emptyOrUnmodifiableSet(conceals); |
36511 | 1057 |
} |
1058 |
||
1059 |
/** |
|
1060 |
* Returns the names of all the packages defined in this module, whether |
|
1061 |
* exported or concealed. |
|
1062 |
* |
|
1063 |
* @return A possibly-empty unmodifiable set of the all packages |
|
1064 |
*/ |
|
1065 |
public Set<String> packages() { |
|
1066 |
return packages; |
|
1067 |
} |
|
1068 |
||
1069 |
/** |
|
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1070 |
* Returns the object with the hashes of other modules |
36511 | 1071 |
*/ |
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1072 |
Optional<ModuleHashes> hashes() { |
36511 | 1073 |
return Optional.ofNullable(hashes); |
1074 |
} |
|
1075 |
||
1076 |
||
1077 |
/** |
|
1078 |
* A builder used for building {@link ModuleDescriptor} objects. |
|
1079 |
* |
|
1080 |
* <p> Example usage: </p> |
|
1081 |
* |
|
1082 |
* <pre>{@code |
|
1083 |
* ModuleDescriptor descriptor = new ModuleDescriptor.Builder("m1") |
|
1084 |
* .requires("m2") |
|
1085 |
* .exports("p") |
|
1086 |
* .build(); |
|
1087 |
* }</pre> |
|
1088 |
* |
|
1089 |
* @apiNote A {@code Builder} cannot be used to create an {@link |
|
1090 |
* ModuleDescriptor#isAutomatic() automatic} or a {@link |
|
1091 |
* ModuleDescriptor#isSynthetic() synthetic} module. |
|
1092 |
* |
|
1093 |
* @since 9 |
|
1094 |
*/ |
|
1095 |
public static final class Builder { |
|
1096 |
||
1097 |
final String name; |
|
39050
9de41b79ec7e
8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents:
38888
diff
changeset
|
1098 |
boolean automatic; |
36511 | 1099 |
boolean synthetic; |
1100 |
final Map<String, Requires> requires = new HashMap<>(); |
|
1101 |
final Set<String> uses = new HashSet<>(); |
|
1102 |
final Map<String, Exports> exports = new HashMap<>(); |
|
1103 |
final Map<String, Provides> provides = new HashMap<>(); |
|
1104 |
Set<String> conceals = Collections.emptySet(); |
|
1105 |
Version version; |
|
1106 |
String osName; |
|
1107 |
String osArch; |
|
1108 |
String osVersion; |
|
1109 |
String mainClass; |
|
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1110 |
ModuleHashes hashes; |
36511 | 1111 |
|
1112 |
/** |
|
1113 |
* Initializes a new builder with the given module name. |
|
1114 |
* |
|
1115 |
* @param name |
|
1116 |
* The module name |
|
1117 |
* |
|
1118 |
* @throws IllegalArgumentException |
|
1119 |
* If the module name is {@code null} or is not a legal Java |
|
1120 |
* identifier |
|
1121 |
*/ |
|
1122 |
public Builder(String name) { |
|
39050
9de41b79ec7e
8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents:
38888
diff
changeset
|
1123 |
this.name = requireModuleName(name); |
36511 | 1124 |
} |
1125 |
||
39050
9de41b79ec7e
8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents:
38888
diff
changeset
|
1126 |
/** |
9de41b79ec7e
8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents:
38888
diff
changeset
|
1127 |
* Updates the builder so that it builds an automatic module. |
9de41b79ec7e
8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents:
38888
diff
changeset
|
1128 |
* |
9de41b79ec7e
8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents:
38888
diff
changeset
|
1129 |
* @return This builder |
9de41b79ec7e
8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents:
38888
diff
changeset
|
1130 |
* |
9de41b79ec7e
8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents:
38888
diff
changeset
|
1131 |
* @see ModuleDescriptor#isAutomatic() |
9de41b79ec7e
8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents:
38888
diff
changeset
|
1132 |
*/ |
9de41b79ec7e
8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents:
38888
diff
changeset
|
1133 |
/* package */ Builder automatic() { |
9de41b79ec7e
8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents:
38888
diff
changeset
|
1134 |
this.automatic = true; |
9de41b79ec7e
8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents:
38888
diff
changeset
|
1135 |
return this; |
36511 | 1136 |
} |
1137 |
||
1138 |
/** |
|
1139 |
* Adds a dependence on a module. |
|
1140 |
* |
|
1141 |
* @param req |
|
1142 |
* The dependence |
|
1143 |
* |
|
1144 |
* @return This builder |
|
1145 |
* |
|
1146 |
* @throws IllegalArgumentException |
|
1147 |
* If the dependence is on the module that this builder was |
|
1148 |
* initialized to build |
|
1149 |
* @throws IllegalStateException |
|
1150 |
* If the dependence on the module has already been declared |
|
1151 |
*/ |
|
1152 |
public Builder requires(Requires req) { |
|
1153 |
String mn = req.name(); |
|
1154 |
if (name.equals(mn)) |
|
1155 |
throw new IllegalArgumentException("Dependence on self"); |
|
1156 |
if (requires.containsKey(mn)) |
|
1157 |
throw new IllegalStateException("Dependence upon " + mn |
|
1158 |
+ " already declared"); |
|
1159 |
requires.put(mn, req); |
|
1160 |
return this; |
|
1161 |
} |
|
1162 |
||
1163 |
/** |
|
1164 |
* Adds a dependence on a module with the given (and possibly empty) |
|
1165 |
* set of modifiers. |
|
1166 |
* |
|
1167 |
* @param mods |
|
1168 |
* The set of modifiers |
|
1169 |
* @param mn |
|
1170 |
* The module name |
|
1171 |
* |
|
1172 |
* @return This builder |
|
1173 |
* |
|
1174 |
* @throws IllegalArgumentException |
|
1175 |
* If the module name is {@code null}, is not a legal Java |
|
1176 |
* identifier, or is equal to the module name that this builder |
|
1177 |
* was initialized to build |
|
1178 |
* @throws IllegalStateException |
|
1179 |
* If the dependence on the module has already been declared |
|
1180 |
*/ |
|
1181 |
public Builder requires(Set<Requires.Modifier> mods, String mn) { |
|
1182 |
if (name.equals(mn)) |
|
1183 |
throw new IllegalArgumentException("Dependence on self"); |
|
1184 |
if (requires.containsKey(mn)) |
|
1185 |
throw new IllegalStateException("Dependence upon " + mn |
|
1186 |
+ " already declared"); |
|
1187 |
requires.put(mn, new Requires(mods, mn)); // checks mn |
|
1188 |
return this; |
|
1189 |
} |
|
1190 |
||
1191 |
/** |
|
1192 |
* Adds a dependence on a module with an empty set of modifiers. |
|
1193 |
* |
|
1194 |
* @param mn |
|
1195 |
* The module name |
|
1196 |
* |
|
1197 |
* @return This builder |
|
1198 |
* |
|
1199 |
* @throws IllegalArgumentException |
|
1200 |
* If the module name is {@code null}, is not a legal Java |
|
1201 |
* identifier, or is equal to the module name that this builder |
|
1202 |
* was initialized to build |
|
1203 |
* @throws IllegalStateException |
|
1204 |
* If the dependence on the module has already been declared |
|
1205 |
*/ |
|
1206 |
public Builder requires(String mn) { |
|
1207 |
return requires(EnumSet.noneOf(Requires.Modifier.class), mn); |
|
1208 |
} |
|
1209 |
||
1210 |
/** |
|
1211 |
* Adds a dependence on a module with the given modifier. |
|
1212 |
* |
|
1213 |
* @param mod |
|
1214 |
* The modifier |
|
1215 |
* @param mn |
|
1216 |
* The module name |
|
1217 |
* |
|
1218 |
* @return This builder |
|
1219 |
* |
|
1220 |
* @throws IllegalArgumentException |
|
1221 |
* If the module name is {@code null}, is not a legal Java |
|
1222 |
* identifier, or is equal to the module name that this builder |
|
1223 |
* was initialized to build |
|
1224 |
* @throws IllegalStateException |
|
1225 |
* If the dependence on the module has already been declared |
|
1226 |
*/ |
|
1227 |
public Builder requires(Requires.Modifier mod, String mn) { |
|
1228 |
return requires(EnumSet.of(mod), mn); |
|
1229 |
} |
|
1230 |
||
1231 |
/** |
|
1232 |
* Adds a service dependence. |
|
1233 |
* |
|
1234 |
* @param st |
|
1235 |
* The service type |
|
1236 |
* |
|
1237 |
* @return This builder |
|
1238 |
* |
|
1239 |
* @throws IllegalArgumentException |
|
1240 |
* If the service type is {@code null} or is not a legal Java |
|
1241 |
* identifier |
|
1242 |
* @throws IllegalStateException |
|
1243 |
* If a dependency on the service type has already been declared |
|
1244 |
*/ |
|
1245 |
public Builder uses(String st) { |
|
1246 |
if (uses.contains(requireServiceTypeName(st))) |
|
1247 |
throw new IllegalStateException("Dependence upon service " |
|
1248 |
+ st + " already declared"); |
|
1249 |
uses.add(st); |
|
1250 |
return this; |
|
1251 |
} |
|
1252 |
||
1253 |
/** |
|
1254 |
* Ensures that the given package name has not been declared as an |
|
1255 |
* exported or concealed package. |
|
1256 |
*/ |
|
1257 |
private void ensureNotExportedOrConcealed(String pn) { |
|
1258 |
if (exports.containsKey(pn)) |
|
1259 |
throw new IllegalStateException("Export of package " |
|
1260 |
+ pn + " already declared"); |
|
1261 |
if (conceals.contains(pn)) |
|
1262 |
throw new IllegalStateException("Concealed package " |
|
1263 |
+ pn + " already declared"); |
|
1264 |
} |
|
1265 |
||
1266 |
/** |
|
1267 |
* Adds an export. |
|
1268 |
* |
|
1269 |
* @param e |
|
1270 |
* The export |
|
1271 |
* |
|
1272 |
* @return This builder |
|
1273 |
* |
|
1274 |
* @throws IllegalStateException |
|
1275 |
* If the package is already declared as an exported or |
|
1276 |
* concealed package |
|
1277 |
*/ |
|
1278 |
public Builder exports(Exports e) { |
|
1279 |
String pn = e.source(); |
|
1280 |
ensureNotExportedOrConcealed(pn); |
|
1281 |
exports.put(pn, e); |
|
1282 |
return this; |
|
1283 |
} |
|
1284 |
||
1285 |
/** |
|
1286 |
* Adds an export to a set of target modules. |
|
1287 |
* |
|
1288 |
* @param pn |
|
1289 |
* The package name |
|
1290 |
* @param targets |
|
1291 |
* The set of target modules names |
|
1292 |
* |
|
1293 |
* @return This builder |
|
1294 |
* |
|
1295 |
* @throws IllegalArgumentException |
|
1296 |
* If the package name or any of the target modules is {@code |
|
1297 |
* null} or is not a legal Java identifier, or the set of |
|
1298 |
* targets is empty |
|
1299 |
* @throws IllegalStateException |
|
1300 |
* If the package is already declared as an exported or |
|
1301 |
* concealed package |
|
1302 |
*/ |
|
1303 |
public Builder exports(String pn, Set<String> targets) { |
|
1304 |
ensureNotExportedOrConcealed(pn); |
|
1305 |
exports.put(pn, new Exports(pn, targets)); // checks pn and targets |
|
1306 |
return this; |
|
1307 |
} |
|
1308 |
||
1309 |
/** |
|
1310 |
* Adds an export to a target module. |
|
1311 |
* |
|
1312 |
* @param pn |
|
1313 |
* The package name |
|
1314 |
* @param target |
|
1315 |
* The target module name |
|
1316 |
* |
|
1317 |
* @return This builder |
|
1318 |
* |
|
1319 |
* @throws IllegalArgumentException |
|
1320 |
* If the package name or target module is {@code null} or is |
|
1321 |
* not a legal Java identifier |
|
1322 |
* @throws IllegalStateException |
|
1323 |
* If the package is already declared as an exported or |
|
1324 |
* concealed package |
|
1325 |
*/ |
|
1326 |
public Builder exports(String pn, String target) { |
|
1327 |
return exports(pn, Collections.singleton(target)); |
|
1328 |
} |
|
1329 |
||
1330 |
/** |
|
1331 |
* Adds an export. |
|
1332 |
* |
|
1333 |
* @param pn |
|
1334 |
* The package name |
|
1335 |
* |
|
1336 |
* @return This builder |
|
1337 |
* |
|
1338 |
* @throws IllegalArgumentException |
|
1339 |
* If the package name is {@code null} or is not a legal Java |
|
1340 |
* identifier |
|
1341 |
* @throws IllegalStateException |
|
1342 |
* If the package is already declared as an exported or |
|
1343 |
* concealed package |
|
1344 |
*/ |
|
1345 |
public Builder exports(String pn) { |
|
1346 |
ensureNotExportedOrConcealed(pn); |
|
1347 |
exports.put(pn, new Exports(pn)); // checks pn |
|
1348 |
return this; |
|
1349 |
} |
|
1350 |
||
1351 |
// Used by ModuleInfo, after a packageFinder is invoked |
|
1352 |
/* package */ Set<String> exportedPackages() { |
|
1353 |
return exports.keySet(); |
|
1354 |
} |
|
1355 |
||
1356 |
/** |
|
1357 |
* Provides a service with one or more implementations. |
|
1358 |
* |
|
1359 |
* @param p |
|
1360 |
* The provides |
|
1361 |
* |
|
1362 |
* @return This builder |
|
1363 |
* |
|
1364 |
* @throws IllegalStateException |
|
1365 |
* If the providers for the service type have already been |
|
1366 |
* declared |
|
1367 |
*/ |
|
1368 |
public Builder provides(Provides p) { |
|
1369 |
String st = p.service(); |
|
1370 |
if (provides.containsKey(st)) |
|
1371 |
throw new IllegalStateException("Providers of service " |
|
1372 |
+ st + " already declared"); |
|
1373 |
provides.put(st, p); |
|
1374 |
return this; |
|
1375 |
} |
|
1376 |
||
1377 |
/** |
|
1378 |
* Provides service {@code st} with implementations {@code pcs}. |
|
1379 |
* |
|
1380 |
* @param st |
|
1381 |
* The service type |
|
1382 |
* @param pcs |
|
1383 |
* The set of provider class names |
|
1384 |
* |
|
1385 |
* @return This builder |
|
1386 |
* |
|
1387 |
* @throws IllegalArgumentException |
|
1388 |
* If the service type or any of the provider class names is |
|
1389 |
* {@code null} or is not a legal Java identifier, or the set |
|
1390 |
* of provider class names is empty |
|
1391 |
* @throws IllegalStateException |
|
1392 |
* If the providers for the service type have already been |
|
1393 |
* declared |
|
1394 |
*/ |
|
1395 |
public Builder provides(String st, Set<String> pcs) { |
|
1396 |
if (provides.containsKey(st)) |
|
1397 |
throw new IllegalStateException("Providers of service " |
|
1398 |
+ st + " already declared"); |
|
1399 |
provides.put(st, new Provides(st, pcs)); // checks st and pcs |
|
1400 |
return this; |
|
1401 |
} |
|
1402 |
||
1403 |
/** |
|
1404 |
* Provides service {@code st} with implementation {@code pc}. |
|
1405 |
* |
|
1406 |
* @param st |
|
1407 |
* The service type |
|
1408 |
* @param pc |
|
1409 |
* The provider class name |
|
1410 |
* |
|
1411 |
* @return This builder |
|
1412 |
* |
|
1413 |
* @throws IllegalArgumentException |
|
1414 |
* If the service type or the provider class name is {@code |
|
1415 |
* null} or is not a legal Java identifier |
|
1416 |
* @throws IllegalStateException |
|
1417 |
* If the providers for the service type have already been |
|
1418 |
* declared |
|
1419 |
*/ |
|
1420 |
public Builder provides(String st, String pc) { |
|
1421 |
return provides(st, Collections.singleton(pc)); |
|
1422 |
} |
|
1423 |
||
1424 |
/** |
|
1425 |
* Adds a set of (possible empty) concealed packages. |
|
1426 |
* |
|
1427 |
* @param pns |
|
1428 |
* The set of package names of the concealed packages |
|
1429 |
* |
|
1430 |
* @return This builder |
|
1431 |
* |
|
1432 |
* @throws IllegalArgumentException |
|
1433 |
* If any of the package names is {@code null} or is not a |
|
1434 |
* legal Java identifier |
|
1435 |
* @throws IllegalStateException |
|
1436 |
* If any of packages are already declared as a concealed or |
|
1437 |
* exported package |
|
1438 |
*/ |
|
1439 |
public Builder conceals(Set<String> pns) { |
|
1440 |
pns.forEach(this::conceals); |
|
1441 |
return this; |
|
1442 |
} |
|
1443 |
||
1444 |
/** |
|
1445 |
* Adds a concealed package. |
|
1446 |
* |
|
1447 |
* @param pn |
|
1448 |
* The package name |
|
1449 |
* |
|
1450 |
* @return This builder |
|
1451 |
* |
|
1452 |
* @throws IllegalArgumentException |
|
1453 |
* If the package name is {@code null}, or is not a legal Java |
|
1454 |
* identifier |
|
1455 |
* @throws IllegalStateException |
|
1456 |
* If the package is already declared as a concealed or exported |
|
1457 |
* package |
|
1458 |
*/ |
|
1459 |
public Builder conceals(String pn) { |
|
1460 |
Checks.requirePackageName(pn); |
|
1461 |
ensureNotExportedOrConcealed(pn); |
|
1462 |
if (conceals.isEmpty()) |
|
1463 |
conceals = new HashSet<>(); |
|
1464 |
conceals.add(pn); |
|
1465 |
return this; |
|
1466 |
} |
|
1467 |
||
1468 |
/** |
|
1469 |
* Sets the module version. |
|
1470 |
* |
|
1471 |
* @param v |
|
1472 |
* The version |
|
1473 |
* |
|
1474 |
* @return This builder |
|
1475 |
* |
|
1476 |
* @throws IllegalStateException |
|
1477 |
* If the module version is already set |
|
1478 |
*/ |
|
1479 |
public Builder version(Version v) { |
|
1480 |
if (version != null) |
|
1481 |
throw new IllegalStateException("module version already set"); |
|
1482 |
version = requireNonNull(v); |
|
1483 |
return this; |
|
1484 |
} |
|
1485 |
||
1486 |
/** |
|
1487 |
* Sets the module version. |
|
1488 |
* |
|
1489 |
* @param v |
|
1490 |
* The version string to parse |
|
1491 |
* |
|
1492 |
* @return This builder |
|
1493 |
* |
|
1494 |
* @throws IllegalArgumentException |
|
1495 |
* If {@code v} is null or cannot be parsed as a version string |
|
1496 |
* @throws IllegalStateException |
|
1497 |
* If the module version is already set |
|
1498 |
* |
|
1499 |
* @see Version#parse(String) |
|
1500 |
*/ |
|
1501 |
public Builder version(String v) { |
|
1502 |
if (version != null) |
|
1503 |
throw new IllegalStateException("module version already set"); |
|
1504 |
version = Version.parse(v); |
|
1505 |
return this; |
|
1506 |
} |
|
1507 |
||
1508 |
/** |
|
1509 |
* Sets the module main class. |
|
1510 |
* |
|
1511 |
* @param mc |
|
1512 |
* The module main class |
|
1513 |
* |
|
1514 |
* @return This builder |
|
1515 |
* |
|
1516 |
* @throws IllegalArgumentException |
|
1517 |
* If {@code mainClass} is null or is not a legal Java identifier |
|
1518 |
* @throws IllegalStateException |
|
1519 |
* If the module main class is already set |
|
1520 |
*/ |
|
1521 |
public Builder mainClass(String mc) { |
|
1522 |
if (mainClass != null) |
|
1523 |
throw new IllegalStateException("main class already set"); |
|
1524 |
mainClass = requireJavaIdentifier("main class name", mc); |
|
1525 |
return this; |
|
1526 |
} |
|
1527 |
||
1528 |
/** |
|
1529 |
* Sets the operating system name. |
|
1530 |
* |
|
1531 |
* @param name |
|
1532 |
* The operating system name |
|
1533 |
* |
|
1534 |
* @return This builder |
|
1535 |
* |
|
1536 |
* @throws IllegalArgumentException |
|
1537 |
* If {@code name} is null or the empty String |
|
1538 |
* @throws IllegalStateException |
|
1539 |
* If the operating system name is already set |
|
1540 |
*/ |
|
1541 |
public Builder osName(String name) { |
|
1542 |
if (osName != null) |
|
1543 |
throw new IllegalStateException("OS name already set"); |
|
1544 |
if (name == null || name.isEmpty()) |
|
1545 |
throw new IllegalArgumentException("OS name is null or empty"); |
|
1546 |
osName = name; |
|
1547 |
return this; |
|
1548 |
} |
|
1549 |
||
1550 |
/** |
|
1551 |
* Sets the operating system architecture. |
|
1552 |
* |
|
1553 |
* @param arch |
|
1554 |
* The operating system architecture |
|
1555 |
* |
|
1556 |
* @return This builder |
|
1557 |
* |
|
1558 |
* @throws IllegalArgumentException |
|
1559 |
* If {@code name} is null or the empty String |
|
1560 |
* @throws IllegalStateException |
|
1561 |
* If the operating system architecture is already set |
|
1562 |
*/ |
|
1563 |
public Builder osArch(String arch) { |
|
1564 |
if (osArch != null) |
|
1565 |
throw new IllegalStateException("OS arch already set"); |
|
1566 |
if (arch == null || arch.isEmpty()) |
|
1567 |
throw new IllegalArgumentException("OS arch is null or empty"); |
|
1568 |
osArch = arch; |
|
1569 |
return this; |
|
1570 |
} |
|
1571 |
||
1572 |
/** |
|
1573 |
* Sets the operating system version. |
|
1574 |
* |
|
1575 |
* @param version |
|
1576 |
* The operating system version |
|
1577 |
* |
|
1578 |
* @return This builder |
|
1579 |
* |
|
1580 |
* @throws IllegalArgumentException |
|
1581 |
* If {@code name} is null or the empty String |
|
1582 |
* @throws IllegalStateException |
|
1583 |
* If the operating system version is already set |
|
1584 |
*/ |
|
1585 |
public Builder osVersion(String version) { |
|
1586 |
if (osVersion != null) |
|
1587 |
throw new IllegalStateException("OS version already set"); |
|
1588 |
if (version == null || version.isEmpty()) |
|
1589 |
throw new IllegalArgumentException("OS version is null or empty"); |
|
1590 |
osVersion = version; |
|
1591 |
return this; |
|
1592 |
} |
|
1593 |
||
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1594 |
/* package */ Builder hashes(ModuleHashes hashes) { |
36511 | 1595 |
this.hashes = hashes; |
1596 |
return this; |
|
1597 |
} |
|
1598 |
||
1599 |
||
1600 |
/* package */ Builder synthetic(boolean v) { |
|
1601 |
this.synthetic = v; |
|
1602 |
return this; |
|
1603 |
} |
|
1604 |
||
1605 |
/** |
|
1606 |
* Builds and returns a {@code ModuleDescriptor} from its components. |
|
1607 |
* |
|
1608 |
* @return The module descriptor |
|
1609 |
*/ |
|
1610 |
public ModuleDescriptor build() { |
|
1611 |
assert name != null; |
|
1612 |
||
38888
dd584fbea6a2
8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents:
37880
diff
changeset
|
1613 |
Set<String> packages = new HashSet<>(conceals); |
dd584fbea6a2
8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents:
37880
diff
changeset
|
1614 |
packages.addAll(exportedPackages()); |
36511 | 1615 |
return new ModuleDescriptor(name, |
1616 |
automatic, |
|
1617 |
synthetic, |
|
1618 |
requires, |
|
1619 |
uses, |
|
1620 |
exports, |
|
1621 |
provides, |
|
1622 |
version, |
|
1623 |
mainClass, |
|
1624 |
osName, |
|
1625 |
osArch, |
|
1626 |
osVersion, |
|
38888
dd584fbea6a2
8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents:
37880
diff
changeset
|
1627 |
packages, |
36511 | 1628 |
hashes); |
1629 |
} |
|
1630 |
||
1631 |
} |
|
1632 |
||
1633 |
||
1634 |
/** |
|
1635 |
* Compares this module descriptor to another. |
|
1636 |
* |
|
1637 |
* <p> Two {@code ModuleDescriptor} objects are compared by comparing their |
|
1638 |
* module name lexicographically. Where the module names are equal then |
|
1639 |
* the versions, if present, are compared. </p> |
|
1640 |
* |
|
1641 |
* @apiNote For now, the natural ordering is not consistent with equals. |
|
1642 |
* If two module descriptors have equal module names, equal versions if |
|
1643 |
* present, but their corresponding components are not equal, then they |
|
1644 |
* will be considered equal by this method. |
|
1645 |
* |
|
1646 |
* @param that |
|
1647 |
* The object to which this module descriptor is to be compared |
|
1648 |
* |
|
1649 |
* @return A negative integer, zero, or a positive integer if this module |
|
1650 |
* descriptor is less than, equal to, or greater than the given |
|
1651 |
* module descriptor |
|
1652 |
*/ |
|
1653 |
@Override |
|
1654 |
public int compareTo(ModuleDescriptor that) { |
|
1655 |
int c = this.name().compareTo(that.name()); |
|
1656 |
if (c != 0) return c; |
|
1657 |
if (version == null) { |
|
1658 |
if (that.version == null) |
|
1659 |
return 0; |
|
1660 |
return -1; |
|
1661 |
} |
|
1662 |
if (that.version == null) |
|
1663 |
return +1; |
|
1664 |
return version.compareTo(that.version); |
|
1665 |
} |
|
1666 |
||
1667 |
/** |
|
1668 |
* Tests this module descriptor for equality with the given object. |
|
1669 |
* |
|
1670 |
* <p> If the given object is not a {@code ModuleDescriptor} then this |
|
1671 |
* method returns {@code false}. Two module descriptors are equal if each |
|
1672 |
* of their corresponding components is equal. </p> |
|
1673 |
* |
|
1674 |
* <p> This method satisfies the general contract of the {@link |
|
1675 |
* java.lang.Object#equals(Object) Object.equals} method. </p> |
|
1676 |
* |
|
1677 |
* @param ob |
|
1678 |
* the object to which this object is to be compared |
|
1679 |
* |
|
1680 |
* @return {@code true} if, and only if, the given object is a module |
|
1681 |
* descriptor that is equal to this module descriptor |
|
1682 |
*/ |
|
1683 |
@Override |
|
1684 |
public boolean equals(Object ob) { |
|
1685 |
if (ob == this) |
|
1686 |
return true; |
|
1687 |
if (!(ob instanceof ModuleDescriptor)) |
|
1688 |
return false; |
|
1689 |
ModuleDescriptor that = (ModuleDescriptor)ob; |
|
1690 |
return (name.equals(that.name) |
|
1691 |
&& automatic == that.automatic |
|
1692 |
&& synthetic == that.synthetic |
|
1693 |
&& requires.equals(that.requires) |
|
1694 |
&& uses.equals(that.uses) |
|
1695 |
&& exports.equals(that.exports) |
|
1696 |
&& provides.equals(that.provides) |
|
1697 |
&& Objects.equals(version, that.version) |
|
1698 |
&& Objects.equals(mainClass, that.mainClass) |
|
1699 |
&& Objects.equals(osName, that.osName) |
|
1700 |
&& Objects.equals(osArch, that.osArch) |
|
1701 |
&& Objects.equals(osVersion, that.osVersion) |
|
38888
dd584fbea6a2
8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents:
37880
diff
changeset
|
1702 |
&& Objects.equals(packages, that.packages) |
36511 | 1703 |
&& Objects.equals(hashes, that.hashes)); |
1704 |
} |
|
1705 |
||
1706 |
private transient int hash; // cached hash code |
|
1707 |
||
1708 |
/** |
|
1709 |
* Computes a hash code for this module descriptor. |
|
1710 |
* |
|
1711 |
* <p> The hash code is based upon the components of the module descriptor, |
|
1712 |
* and satisfies the general contract of the {@link Object#hashCode |
|
1713 |
* Object.hashCode} method. </p> |
|
1714 |
* |
|
1715 |
* @return The hash-code value for this module descriptor |
|
1716 |
*/ |
|
1717 |
@Override |
|
1718 |
public int hashCode() { |
|
1719 |
int hc = hash; |
|
1720 |
if (hc == 0) { |
|
1721 |
hc = name.hashCode(); |
|
1722 |
hc = hc * 43 + Boolean.hashCode(automatic); |
|
1723 |
hc = hc * 43 + Boolean.hashCode(synthetic); |
|
1724 |
hc = hc * 43 + requires.hashCode(); |
|
1725 |
hc = hc * 43 + uses.hashCode(); |
|
1726 |
hc = hc * 43 + exports.hashCode(); |
|
1727 |
hc = hc * 43 + provides.hashCode(); |
|
1728 |
hc = hc * 43 + Objects.hashCode(version); |
|
1729 |
hc = hc * 43 + Objects.hashCode(mainClass); |
|
1730 |
hc = hc * 43 + Objects.hashCode(osName); |
|
1731 |
hc = hc * 43 + Objects.hashCode(osArch); |
|
1732 |
hc = hc * 43 + Objects.hashCode(osVersion); |
|
38888
dd584fbea6a2
8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents:
37880
diff
changeset
|
1733 |
hc = hc * 43 + Objects.hashCode(packages); |
36511 | 1734 |
hc = hc * 43 + Objects.hashCode(hashes); |
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1735 |
if (hc == 0) |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1736 |
hc = -1; |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1737 |
hash = hc; |
36511 | 1738 |
} |
1739 |
return hc; |
|
1740 |
} |
|
1741 |
||
1742 |
/** |
|
1743 |
* Returns a string describing this descriptor. |
|
1744 |
* |
|
1745 |
* @return A string describing this descriptor |
|
1746 |
*/ |
|
1747 |
@Override |
|
1748 |
public String toString() { |
|
1749 |
StringBuilder sb = new StringBuilder(); |
|
1750 |
sb.append("Module { name: ").append(toNameAndVersion()); |
|
1751 |
if (!requires.isEmpty()) |
|
1752 |
sb.append(", ").append(requires); |
|
1753 |
if (!uses.isEmpty()) |
|
1754 |
sb.append(", ").append(uses); |
|
1755 |
if (!exports.isEmpty()) |
|
1756 |
sb.append(", exports: ").append(exports); |
|
1757 |
if (!provides.isEmpty()) { |
|
1758 |
sb.append(", provides: ["); |
|
1759 |
for (Map.Entry<String, Provides> entry : provides.entrySet()) { |
|
1760 |
sb.append(entry.getKey()) |
|
1761 |
.append(" with ") |
|
1762 |
.append(entry.getValue()); |
|
1763 |
} |
|
1764 |
sb.append("]"); |
|
1765 |
} |
|
1766 |
sb.append(" }"); |
|
1767 |
return sb.toString(); |
|
1768 |
} |
|
1769 |
||
1770 |
/** |
|
1771 |
* Reads the binary form of a module declaration from an input stream |
|
1772 |
* as a module descriptor. |
|
1773 |
* |
|
1774 |
* <p> If the descriptor encoded in the input stream does not indicate a |
|
1775 |
* set of concealed packages then the {@code packageFinder} will be |
|
1776 |
* invoked. The packages it returns, except for those indicated as |
|
1777 |
* exported in the encoded descriptor, will be considered to be concealed. |
|
1778 |
* If the {@code packageFinder} throws an {@link UncheckedIOException} then |
|
1779 |
* {@link IOException} cause will be re-thrown. </p> |
|
1780 |
* |
|
1781 |
* <p> If there are bytes following the module descriptor then it is |
|
1782 |
* implementation specific as to whether those bytes are read, ignored, |
|
1783 |
* or reported as an {@code InvalidModuleDescriptorException}. If this |
|
1784 |
* method fails with an {@code InvalidModuleDescriptorException} or {@code |
|
1785 |
* IOException} then it may do so after some, but not all, bytes have |
|
1786 |
* been read from the input stream. It is strongly recommended that the |
|
1787 |
* stream be promptly closed and discarded if an exception occurs. </p> |
|
1788 |
* |
|
1789 |
* @apiNote The {@code packageFinder} parameter is for use when reading |
|
1790 |
* module descriptors from legacy module-artifact formats that do not |
|
1791 |
* record the set of concealed packages in the descriptor itself. |
|
1792 |
* |
|
1793 |
* @param in |
|
1794 |
* The input stream |
|
1795 |
* @param packageFinder |
|
1796 |
* A supplier that can produce a set of package names |
|
1797 |
* |
|
1798 |
* @return The module descriptor |
|
1799 |
* |
|
1800 |
* @throws InvalidModuleDescriptorException |
|
1801 |
* If an invalid module descriptor is detected |
|
1802 |
* @throws IOException |
|
1803 |
* If an I/O error occurs reading from the input stream or {@code |
|
1804 |
* UncheckedIOException} is thrown by the package finder |
|
1805 |
*/ |
|
1806 |
public static ModuleDescriptor read(InputStream in, |
|
1807 |
Supplier<Set<String>> packageFinder) |
|
1808 |
throws IOException |
|
1809 |
{ |
|
1810 |
return ModuleInfo.read(in, requireNonNull(packageFinder)); |
|
1811 |
} |
|
1812 |
||
1813 |
/** |
|
1814 |
* Reads the binary form of a module declaration from an input stream |
|
1815 |
* as a module descriptor. |
|
1816 |
* |
|
1817 |
* @param in |
|
1818 |
* The input stream |
|
1819 |
* |
|
1820 |
* @return The module descriptor |
|
1821 |
* |
|
1822 |
* @throws InvalidModuleDescriptorException |
|
1823 |
* If an invalid module descriptor is detected |
|
1824 |
* @throws IOException |
|
1825 |
* If an I/O error occurs reading from the input stream |
|
1826 |
*/ |
|
1827 |
public static ModuleDescriptor read(InputStream in) throws IOException { |
|
1828 |
return ModuleInfo.read(in, null); |
|
1829 |
} |
|
1830 |
||
1831 |
/** |
|
1832 |
* Reads the binary form of a module declaration from a byte buffer |
|
1833 |
* as a module descriptor. |
|
1834 |
* |
|
1835 |
* <p> If the descriptor encoded in the byte buffer does not indicate a |
|
1836 |
* set of concealed packages then the {@code packageFinder} will be |
|
1837 |
* invoked. The packages it returns, except for those indicated as |
|
1838 |
* exported in the encoded descriptor, will be considered to be |
|
1839 |
* concealed. </p> |
|
1840 |
* |
|
1841 |
* <p> The module descriptor is read from the buffer stating at index |
|
1842 |
* {@code p}, where {@code p} is the buffer's {@link ByteBuffer#position() |
|
1843 |
* position} when this method is invoked. Upon return the buffer's position |
|
1844 |
* will be equal to {@code p + n} where {@code n} is the number of bytes |
|
1845 |
* read from the buffer. </p> |
|
1846 |
* |
|
1847 |
* <p> If there are bytes following the module descriptor then it is |
|
1848 |
* implementation specific as to whether those bytes are read, ignored, |
|
1849 |
* or reported as an {@code InvalidModuleDescriptorException}. If this |
|
1850 |
* method fails with an {@code InvalidModuleDescriptorException} then it |
|
1851 |
* may do so after some, but not all, bytes have been read. </p> |
|
1852 |
* |
|
1853 |
* @apiNote The {@code packageFinder} parameter is for use when reading |
|
1854 |
* module descriptors from legacy module-artifact formats that do not |
|
1855 |
* record the set of concealed packages in the descriptor itself. |
|
1856 |
* |
|
1857 |
* @param bb |
|
1858 |
* The byte buffer |
|
1859 |
* @param packageFinder |
|
1860 |
* A supplier that can produce a set of package names |
|
1861 |
* |
|
1862 |
* @return The module descriptor |
|
1863 |
* |
|
1864 |
* @throws InvalidModuleDescriptorException |
|
1865 |
* If an invalid module descriptor is detected |
|
1866 |
*/ |
|
1867 |
public static ModuleDescriptor read(ByteBuffer bb, |
|
1868 |
Supplier<Set<String>> packageFinder) |
|
1869 |
{ |
|
1870 |
return ModuleInfo.read(bb, requireNonNull(packageFinder)); |
|
1871 |
} |
|
1872 |
||
1873 |
/** |
|
1874 |
* Reads the binary form of a module declaration from a byte buffer |
|
1875 |
* as a module descriptor. |
|
1876 |
* |
|
1877 |
* @param bb |
|
1878 |
* The byte buffer |
|
1879 |
* |
|
1880 |
* @return The module descriptor |
|
1881 |
* |
|
1882 |
* @throws InvalidModuleDescriptorException |
|
1883 |
* If an invalid module descriptor is detected |
|
1884 |
*/ |
|
1885 |
public static ModuleDescriptor read(ByteBuffer bb) { |
|
1886 |
return ModuleInfo.read(bb, null); |
|
1887 |
} |
|
1888 |
||
1889 |
private static <K,V> Map<K,V> emptyOrUnmodifiableMap(Map<K,V> map) { |
|
1890 |
if (map.isEmpty()) { |
|
1891 |
return Collections.emptyMap(); |
|
1892 |
} else if (map.size() == 1) { |
|
1893 |
Map.Entry<K, V> entry = map.entrySet().iterator().next(); |
|
1894 |
return Collections.singletonMap(entry.getKey(), entry.getValue()); |
|
1895 |
} else { |
|
1896 |
return Collections.unmodifiableMap(map); |
|
1897 |
} |
|
1898 |
} |
|
1899 |
||
1900 |
private static <T> Set<T> emptyOrUnmodifiableSet(Set<T> set) { |
|
1901 |
if (set.isEmpty()) { |
|
1902 |
return Collections.emptySet(); |
|
1903 |
} else if (set.size() == 1) { |
|
1904 |
return Collections.singleton(set.iterator().next()); |
|
1905 |
} else { |
|
1906 |
return Collections.unmodifiableSet(set); |
|
1907 |
} |
|
1908 |
} |
|
1909 |
||
1910 |
static { |
|
1911 |
/** |
|
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1912 |
* Setup the shared secret to allow code in other packages access |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1913 |
* private package methods in java.lang.module. |
36511 | 1914 |
*/ |
1915 |
jdk.internal.misc.SharedSecrets |
|
1916 |
.setJavaLangModuleAccess(new jdk.internal.misc.JavaLangModuleAccess() { |
|
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1917 |
|
36511 | 1918 |
@Override |
1919 |
public Requires newRequires(Set<Requires.Modifier> ms, String mn) { |
|
1920 |
return new Requires(ms, mn, false); |
|
1921 |
} |
|
1922 |
||
1923 |
@Override |
|
1924 |
public Exports newExports(String source, Set<String> targets) { |
|
1925 |
return new Exports(source, targets, false); |
|
1926 |
} |
|
1927 |
||
1928 |
@Override |
|
1929 |
public Exports newExports(String source) { |
|
1930 |
return new Exports(source, false); |
|
1931 |
} |
|
1932 |
||
1933 |
@Override |
|
1934 |
public Provides newProvides(String service, Set<String> providers) { |
|
1935 |
return new Provides(service, providers, false); |
|
1936 |
} |
|
1937 |
||
1938 |
@Override |
|
1939 |
public Version newVersion(String v) { |
|
1940 |
return new Version(v); |
|
1941 |
} |
|
1942 |
||
1943 |
@Override |
|
1944 |
public ModuleDescriptor newModuleDescriptor(ModuleDescriptor md, |
|
1945 |
Set<String> pkgs) { |
|
1946 |
return new ModuleDescriptor(md, pkgs); |
|
1947 |
} |
|
1948 |
||
1949 |
@Override |
|
1950 |
public ModuleDescriptor newModuleDescriptor(String name, |
|
1951 |
boolean automatic, |
|
1952 |
boolean synthetic, |
|
1953 |
Set<Requires> requires, |
|
38888
dd584fbea6a2
8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents:
37880
diff
changeset
|
1954 |
Set<String> uses, |
dd584fbea6a2
8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents:
37880
diff
changeset
|
1955 |
Set<Exports> exports, |
36511 | 1956 |
Map<String, Provides> provides, |
1957 |
Version version, |
|
1958 |
String mainClass, |
|
1959 |
String osName, |
|
1960 |
String osArch, |
|
1961 |
String osVersion, |
|
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1962 |
Set<String> packages, |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1963 |
ModuleHashes hashes) { |
36511 | 1964 |
return new ModuleDescriptor(name, |
1965 |
automatic, |
|
1966 |
synthetic, |
|
1967 |
requires, |
|
1968 |
uses, |
|
1969 |
exports, |
|
1970 |
provides, |
|
1971 |
version, |
|
1972 |
mainClass, |
|
1973 |
osName, |
|
1974 |
osArch, |
|
1975 |
osVersion, |
|
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1976 |
packages, |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1977 |
hashes); |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1978 |
} |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1979 |
|
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1980 |
@Override |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1981 |
public Configuration resolveRequiresAndUses(ModuleFinder finder, |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1982 |
Collection<String> roots, |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1983 |
boolean check, |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1984 |
PrintStream traceOutput) |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1985 |
{ |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1986 |
return Configuration.resolveRequiresAndUses(finder, roots, check, traceOutput); |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1987 |
} |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1988 |
|
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1989 |
@Override |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1990 |
public ModuleReference newPatchedModule(ModuleDescriptor descriptor, |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1991 |
URI location, |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1992 |
Supplier<ModuleReader> s) { |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1993 |
return new ModuleReference(descriptor, location, s, true, null); |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1994 |
} |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1995 |
|
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1996 |
@Override |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1997 |
public Optional<ModuleHashes> hashes(ModuleDescriptor descriptor) { |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
1998 |
return descriptor.hashes(); |
36511 | 1999 |
} |
2000 |
}); |
|
2001 |
} |
|
2002 |
||
2003 |
} |