forked from kangjianwei/LearningJDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModuleDescriptor.java
2631 lines (2368 loc) · 101 KB
/
ModuleDescriptor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2009, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.lang.module;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.UncheckedIOException;
import java.nio.ByteBuffer;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import jdk.internal.misc.JavaLangModuleAccess;
import jdk.internal.misc.SharedSecrets;
import jdk.internal.module.Checks;
import jdk.internal.module.ModuleInfo;
import static java.util.Objects.requireNonNull;
import static jdk.internal.module.Checks.requireModuleName;
import static jdk.internal.module.Checks.requirePackageName;
import static jdk.internal.module.Checks.requireQualifiedClassName;
import static jdk.internal.module.Checks.requireServiceTypeName;
/**
* A module descriptor.
*
* <p> A module descriptor describes a named module and defines methods to
* obtain each of its components. The module descriptor for a named module
* in the Java virtual machine is obtained by invoking the {@link
* java.lang.Module Module}'s {@link java.lang.Module#getDescriptor
* getDescriptor} method. Module descriptors can also be created using the
* {@link ModuleDescriptor.Builder} class or by reading the binary form of a
* module declaration ({@code module-info.class}) using the {@link
* #read(InputStream,Supplier) read} methods defined here. </p>
*
* <p> A module descriptor describes a <em>normal</em>, open, or automatic
* module. <em>Normal</em> modules and open modules describe their {@link
* #requires() dependences}, {@link #exports() exported-packages}, the services
* that they {@link #uses() use} or {@link #provides() provide}, and other
* components. <em>Normal</em> modules may {@link #opens() open} specific
* packages. The module descriptor for an open modules does not declare any
* open packages (its {@code opens} method returns an empty set) but when
* instantiated in the Java virtual machine then it is treated as if all
* packages are open. The module descriptor for an automatic module does not
* declare any dependences (except for the mandatory dependency on {@code
* java.base}), and does not declare any exported or open packages. Automatic
* module receive special treatment during resolution so that they read all
* other modules in the configuration. When an automatic module is instantiated
* in the Java virtual machine then it reads every unnamed module and is
* treated as if all packages are exported and open. </p>
*
* <p> {@code ModuleDescriptor} objects are immutable and safe for use by
* multiple concurrent threads.</p>
*
* @see java.lang.Module
* @since 9
* @spec JPMS
*/
// 模块描述符,对应module-info.class
public class ModuleDescriptor implements Comparable<ModuleDescriptor> {
private final String name; // 模块名称
private final Version version; // 模块版本
private final String rawVersionString;
private final Set<Modifier> modifiers; // 模块描述符
/**
* 当前模块是否为open模块
*
* open模块(开放)opens所有包到所有模块
*/
private final boolean open; // true if modifiers contains OPEN
/**
* 当前模块是否为自动模块
*
* 自动模块是放在模块路径上的普通jar,
* 自动模块依赖所有模块,且导出(exports)/(开放)opens所有包到所有模块
*/
private final boolean automatic; // true if modifiers contains AUTOMATIC
private final Set<Requires> requires; // 当前模块依赖(requires)的元素
private final Set<Exports> exports; // 当前模块导出(exports)的元素
private final Set<Opens> opens; // 当前模块打开(opens)的元素
private final Set<String> uses; // 当前模块uses的服务接口
private final Set<Provides> provides; // 当前模块provides的服务
private final Set<String> packages; // 当前模块下辖的包
private final String mainClass; // 当前模块的主类
private transient int hash; // cached hash code
// 留下一些后门方法
static {
/*
* Setup the shared secret to allow code in other packages access
* private package methods in java.lang.module.
*/
SharedSecrets.setJavaLangModuleAccess(new JavaLangModuleAccess() {
@Override
public ModuleDescriptor newModuleDescriptor(String name, Version version, Set<ModuleDescriptor.Modifier> modifiers, Set<Requires> requires, Set<Exports> exports, Set<Opens> opens, Set<String> uses, Set<Provides> provides, Set<String> packages, String mainClass, int hashCode) {
return new ModuleDescriptor(name, version, modifiers, requires, exports, opens, uses, provides, packages, mainClass, hashCode, false);
}
@Override
public Builder newModuleBuilder(String mn, boolean strict, Set<ModuleDescriptor.Modifier> modifiers) {
return new Builder(mn, strict, modifiers);
}
@Override
public Set<String> packages(ModuleDescriptor.Builder builder) {
return builder.packages();
}
@Override
public void requires(ModuleDescriptor.Builder builder, Set<Requires.Modifier> ms, String mn, String rawCompiledVersion) {
builder.requires(ms, mn, rawCompiledVersion);
}
@Override
public Requires newRequires(Set<Requires.Modifier> ms, String mn, Version v) {
return new Requires(ms, mn, v, true);
}
@Override
public Exports newExports(Set<Exports.Modifier> ms, String source) {
return new Exports(ms, source, Collections.emptySet(), true);
}
@Override
public Exports newExports(Set<Exports.Modifier> ms, String source, Set<String> targets) {
return new Exports(ms, source, targets, true);
}
@Override
public Opens newOpens(Set<Opens.Modifier> ms, String source, Set<String> targets) {
return new Opens(ms, source, targets, true);
}
@Override
public Opens newOpens(Set<Opens.Modifier> ms, String source) {
return new Opens(ms, source, Collections.emptySet(), true);
}
@Override
public Provides newProvides(String service, List<String> providers) {
return new Provides(service, providers, true);
}
@Override
public Configuration resolveAndBind(ModuleFinder finder, Collection<String> roots, PrintStream traceOutput) {
return Configuration.resolveAndBind(finder, roots, traceOutput);
}
@Override
public Configuration newConfiguration(ModuleFinder finder, Map<String, Set<String>> graph) {
return new Configuration(finder, graph);
}
});
}
/*▼ 构造器 ████████████████████████████████████████████████████████████████████████████████┓ */
private ModuleDescriptor(String name, Version version, String rawVersionString, Set<Modifier> modifiers, Set<Requires> requires, Set<Exports> exports, Set<Opens> opens, Set<String> uses, Set<Provides> provides, Set<String> packages, String mainClass) {
assert version == null || rawVersionString == null;
this.name = name;
this.version = version;
this.rawVersionString = rawVersionString;
this.modifiers = emptyOrUnmodifiableSet(modifiers);
this.open = modifiers.contains(Modifier.OPEN);
this.automatic = modifiers.contains(Modifier.AUTOMATIC);
assert (requires.stream().map(Requires::name).distinct().count() == requires.size());
this.requires = emptyOrUnmodifiableSet(requires);
this.exports = emptyOrUnmodifiableSet(exports);
this.opens = emptyOrUnmodifiableSet(opens);
this.uses = emptyOrUnmodifiableSet(uses);
this.provides = emptyOrUnmodifiableSet(provides);
this.packages = emptyOrUnmodifiableSet(packages);
this.mainClass = mainClass;
}
/**
* Creates a module descriptor from its components.
* The arguments are pre-validated and sets are unmodifiable sets.
*/
ModuleDescriptor(String name, Version version, Set<Modifier> modifiers, Set<Requires> requires, Set<Exports> exports, Set<Opens> opens, Set<String> uses, Set<Provides> provides, Set<String> packages, String mainClass, int hashCode, boolean unused) {
this.name = name;
this.version = version;
this.rawVersionString = null;
this.modifiers = modifiers;
this.open = modifiers.contains(Modifier.OPEN);
this.automatic = modifiers.contains(Modifier.AUTOMATIC);
this.requires = requires;
this.exports = exports;
this.opens = opens;
this.uses = uses;
this.provides = provides;
this.packages = packages;
this.mainClass = mainClass;
this.hash = hashCode;
}
/*▲ 构造器 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 关联信息 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* <p> Returns {@code true} if this is an open module. </p>
*
* <p> This method is equivalent to testing if the set of {@link #modifiers()
* modifiers} contains the {@link Modifier#OPEN OPEN} modifier. </p>
*
* @return {@code true} if this is an open module
*/
// 是否为open模块
public boolean isOpen() {
return open;
}
/**
* <p> Returns {@code true} if this is an automatic module. </p>
*
* <p> This method is equivalent to testing if the set of {@link #modifiers()
* modifiers} contains the {@link Modifier#AUTOMATIC AUTOMATIC} modifier. </p>
*
* @return {@code true} if this is an automatic module
*/
// 是否为自动模块
public boolean isAutomatic() {
return automatic;
}
/**
* <p> Returns the module name. </p>
*
* @return The module name
*/
// 获取模块名称
public String name() {
return name;
}
/**
* <p> Returns the set of module modifiers. </p>
*
* @return A possibly-empty unmodifiable set of modifiers
*/
// 获取模块修饰符
public Set<Modifier> modifiers() {
return modifiers;
}
/**
* <p> Returns the module main class. </p>
*
* @return The fully qualified class name of the module's main class
*/
// 返回当前模块中的主类
public Optional<String> mainClass() {
return Optional.ofNullable(mainClass);
}
/**
* Returns the set of packages in the module.
*
* <p> The set of packages includes all exported and open packages, as well
* as the packages of any service providers, and the package for the main
* class. </p>
*
* @return A possibly-empty unmodifiable set of the packages in the module
*/
// 返回当前模块下辖的包
public Set<String> packages() {
return packages;
}
/**
* <p> Returns the set of {@code Requires} objects representing the module
* dependences. </p>
*
* <p> The set includes a dependency on "{@code java.base}" when this
* module is not named "{@code java.base}". If this module is an automatic
* module then it does not have a dependency on any module other than
* "{@code java.base}". </p>
*
* @return A possibly-empty unmodifiable set of {@link Requires} objects
*/
// 返回当前模块依赖(requires)的元素
public Set<Requires> requires() {
return requires;
}
/**
* <p> Returns the set of {@code Exports} objects representing the exported
* packages. </p>
*
* <p> If this module is an automatic module then the set of exports
* is empty. </p>
*
* @return A possibly-empty unmodifiable set of exported packages
*/
// 返回当前模块导出(exports)的元素
public Set<Exports> exports() {
return exports;
}
/**
* <p> Returns the set of {@code Opens} objects representing the open
* packages. </p>
*
* <p> If this module is an open module or an automatic module then the
* set of open packages is empty. </p>
*
* @return A possibly-empty unmodifiable set of open packages
*/
// 返回当前模块打开(opens)的元素
public Set<Opens> opens() {
return opens;
}
/**
* <p> Returns the set of service dependences. </p>
*
* <p> If this module is an automatic module then the set of service
* dependences is empty. </p>
*
* @return A possibly-empty unmodifiable set of the fully qualified class
* names of the service types used
*/
// 返回当前模块uses的服务接口
public Set<String> uses() {
return uses;
}
/**
* <p> Returns the set of {@code Provides} objects representing the
* services that the module provides. </p>
*
* @return The possibly-empty unmodifiable set of the services that this
* module provides
*/
// 返回当前模块provides的服务
public Set<Provides> provides() {
return provides;
}
/**
* <p> Returns the module version. </p>
*
* @return This module's version, or an empty {@code Optional} if the
* module does not have a version or the version is
* {@linkplain Version#parse(String) unparseable}
*/
// 返回当前模块的版本
public Optional<Version> version() {
return Optional.ofNullable(version);
}
/**
* <p> Returns the string with the possibly-unparseable version of the
* module </p>
*
* @return The string containing the version of the module or an empty
* {@code Optional} if the module does not have a version
*
* @see #version()
*/
// 返回当前模块的原始版本(无法解析的版本名称)
public Optional<String> rawVersion() {
if(version != null) {
return Optional.of(version.toString());
} else {
return Optional.ofNullable(rawVersionString);
}
}
/*▲ 关联信息 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 模块描述符工厂 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Instantiates a builder to build a module descriptor.
*
* @param name The module name
* @param ms The set of module modifiers
*
* @return A new builder
*
* @throws IllegalArgumentException If the module name is {@code null} or is not a legal module name,
* or the set of modifiers contains {@link Modifier#AUTOMATIC AUTOMATIC} with other modifiers
*/
// 使用指定的模块标记ms来构造一个模块描述符
public static Builder newModule(String name, Set<Modifier> ms) {
Set<Modifier> mods = new HashSet<>(ms);
if(mods.contains(Modifier.AUTOMATIC) && mods.size()>1) {
throw new IllegalArgumentException("AUTOMATIC cannot be used with other modifiers");
}
return new Builder(name, true, mods);
}
/**
* Instantiates a builder to build a module descriptor for a <em>normal</em> module.
* This method is equivalent to invoking {@link #newModule(String, Set) newModule}
* with an empty set of {@link ModuleDescriptor.Modifier modifiers}.
*
* @param name The module name
*
* @return A new builder
*
* @throws IllegalArgumentException If the module name is {@code null} or is not a legal module
* name
*/
// 构造一个普通模块描述符
public static Builder newModule(String name) {
return new Builder(name, true, Set.of());
}
/**
* Instantiates a builder to build a module descriptor for an open module.
* This method is equivalent to invoking {@link #newModule(String, Set) newModule}
* with the {@link ModuleDescriptor.Modifier#OPEN OPEN} modifier.
*
* <p> The builder for an open module cannot be used to declare any open
* packages. </p>
*
* @param name The module name
*
* @return A new builder that builds an open module
*
* @throws IllegalArgumentException If the module name is {@code null} or is not a legal module
* name
*/
// 构造一个open模块描述符
public static Builder newOpenModule(String name) {
return new Builder(name, true, Set.of(Modifier.OPEN));
}
/**
* Instantiates a builder to build a module descriptor for an automatic module.
* This method is equivalent to invoking {@link #newModule(String, Set) newModule}
* with the {@link ModuleDescriptor.Modifier#AUTOMATIC AUTOMATIC} modifier.
*
* The builder for an automatic module cannot be used to declare module or service dependences.
* It also cannot be used to declare any exported or open packages.
*
* @param name The module name
*
* @return A new builder that builds an automatic module
*
* @throws IllegalArgumentException If the module name is {@code null} or is not a legal module
* name
* @see ModuleFinder#of(Path[])
*/
// 构造一个自动模块描述符
public static Builder newAutomaticModule(String name) {
return new Builder(name, true, Set.of(Modifier.AUTOMATIC));
}
/*▲ 模块描述符工厂 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 从module-info.class构建ModuleDescriptor ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Reads the binary form of a module declaration from an input stream
* as a module descriptor.
*
* <p> If the descriptor encoded in the input stream does not indicate a
* set of packages in the module then the {@code packageFinder} will be
* invoked. The set of packages that the {@code packageFinder} returns
* must include all the packages that the module exports, opens, as well
* as the packages of the service implementations that the module provides,
* and the package of the main class (if the module has a main class). If
* the {@code packageFinder} throws an {@link UncheckedIOException} then
* {@link IOException} cause will be re-thrown. </p>
*
* <p> If there are bytes following the module descriptor then it is
* implementation specific as to whether those bytes are read, ignored,
* or reported as an {@code InvalidModuleDescriptorException}. If this
* method fails with an {@code InvalidModuleDescriptorException} or {@code
* IOException} then it may do so after some, but not all, bytes have
* been read from the input stream. It is strongly recommended that the
* stream be promptly closed and discarded if an exception occurs. </p>
*
* @param in The input stream
* @param packageFinder A supplier that can produce the set of packages
*
* @return The module descriptor
*
* @throws InvalidModuleDescriptorException If an invalid module descriptor is detected or the set of
* packages returned by the {@code packageFinder} does not include
* all of the packages obtained from the module descriptor
* @throws IOException If an I/O error occurs reading from the input stream or {@code
* UncheckedIOException} is thrown by the package finder
* @apiNote The {@code packageFinder} parameter is for use when reading
* module descriptors from legacy module-artifact formats that do not
* record the set of packages in the descriptor itself.
*/
public static ModuleDescriptor read(InputStream in, Supplier<Set<String>> packageFinder) throws IOException {
return ModuleInfo.read(in, requireNonNull(packageFinder)).descriptor();
}
/**
* Reads the binary form of a module declaration from an input stream as a
* module descriptor. This method works exactly as specified by the 2-arg
* {@link #read(InputStream, Supplier) read} method with the exception that
* a packager finder is not used to find additional packages when the
* module descriptor read from the stream does not indicate the set of
* packages.
*
* @param in The input stream
*
* @return The module descriptor
*
* @throws InvalidModuleDescriptorException If an invalid module descriptor is detected
* @throws IOException If an I/O error occurs reading from the input stream
*/
public static ModuleDescriptor read(InputStream in) throws IOException {
return ModuleInfo.read(in, null).descriptor();
}
/**
* Reads the binary form of a module declaration from a byte buffer
* as a module descriptor.
*
* <p> If the descriptor encoded in the byte buffer does not indicate a
* set of packages in the module then the {@code packageFinder} will be
* invoked. The set of packages that the {@code packageFinder} returns
* must include all the packages that the module exports, opens, as well
* as the packages of the service implementations that the module provides,
* and the package of the main class (if the module has a main class). If
* the {@code packageFinder} throws an {@link UncheckedIOException} then
* {@link IOException} cause will be re-thrown. </p>
*
* <p> The module descriptor is read from the buffer starting at index
* {@code p}, where {@code p} is the buffer's {@link ByteBuffer#position()
* position} when this method is invoked. Upon return the buffer's position
* will be equal to {@code p + n} where {@code n} is the number of bytes
* read from the buffer. </p>
*
* <p> If there are bytes following the module descriptor then it is
* implementation specific as to whether those bytes are read, ignored,
* or reported as an {@code InvalidModuleDescriptorException}. If this
* method fails with an {@code InvalidModuleDescriptorException} then it
* may do so after some, but not all, bytes have been read. </p>
*
* @param bb The byte buffer
* @param packageFinder A supplier that can produce the set of packages
*
* @return The module descriptor
*
* @throws InvalidModuleDescriptorException If an invalid module descriptor is detected or the set of
* packages returned by the {@code packageFinder} does not include
* all of the packages obtained from the module descriptor
* @apiNote The {@code packageFinder} parameter is for use when reading
* module descriptors from legacy module-artifact formats that do not
* record the set of packages in the descriptor itself.
*/
public static ModuleDescriptor read(ByteBuffer bb, Supplier<Set<String>> packageFinder) {
return ModuleInfo.read(bb, requireNonNull(packageFinder)).descriptor();
}
/**
* Reads the binary form of a module declaration from a byte buffer as a
* module descriptor. This method works exactly as specified by the 2-arg
* {@link #read(ByteBuffer, Supplier) read} method with the exception that a
* packager finder is not used to find additional packages when the module
* descriptor encoded in the buffer does not indicate the set of packages.
*
* @param bb The byte buffer
*
* @return The module descriptor
*
* @throws InvalidModuleDescriptorException If an invalid module descriptor is detected
*/
public static ModuleDescriptor read(ByteBuffer bb) {
return ModuleInfo.read(bb, null).descriptor();
}
/*▲ 从module-info.class构建ModuleDescriptor ████████████████████████████████████████████████████████████████████████████████┛ */
/**
* <p> Returns a string containing the module name and, if present, its
* version. </p>
*
* @return A string containing the module name and, if present, its
* version
*/
public String toNameAndVersion() {
if(version != null) {
return name() + "@" + version;
} else {
return name();
}
}
/**
* Compares this module descriptor to another.
*
* <p> Two {@code ModuleDescriptor} objects are compared by comparing their
* module names lexicographically. Where the module names are equal then the
* module versions are compared. When comparing the module versions then a
* module descriptor with a version is considered to succeed a module
* descriptor that does not have a version. If both versions are {@linkplain
* Version#parse(String) unparseable} then the {@linkplain #rawVersion()
* raw version strings} are compared lexicographically. Where the module names
* are equal and the versions are equal (or not present in both), then the
* set of modifiers are compared. Sets of modifiers are compared by comparing
* a <em>binary value</em> computed for each set. If a modifier is present
* in the set then the bit at the position of its ordinal is {@code 1}
* in the binary value, otherwise {@code 0}. If the two set of modifiers
* are also equal then the other components of the module descriptors are
* compared in a manner that is consistent with {@code equals}. </p>
*
* @param that The module descriptor to compare
*
* @return A negative integer, zero, or a positive integer if this module
* descriptor is less than, equal to, or greater than the given
* module descriptor
*/
@Override
public int compareTo(ModuleDescriptor that) {
if(this == that)
return 0;
int c = this.name().compareTo(that.name());
if(c != 0)
return c;
c = compare(this.version, that.version);
if(c != 0)
return c;
c = compare(this.rawVersionString, that.rawVersionString);
if(c != 0)
return c;
long v1 = modsValue(this.modifiers());
long v2 = modsValue(that.modifiers());
c = Long.compare(v1, v2);
if(c != 0)
return c;
c = compare(this.requires, that.requires);
if(c != 0)
return c;
c = compare(this.packages, that.packages);
if(c != 0)
return c;
c = compare(this.exports, that.exports);
if(c != 0)
return c;
c = compare(this.opens, that.opens);
if(c != 0)
return c;
c = compare(this.uses, that.uses);
if(c != 0)
return c;
c = compare(this.provides, that.provides);
if(c != 0)
return c;
c = compare(this.mainClass, that.mainClass);
if(c != 0)
return c;
return 0;
}
/**
* <p> Returns a string describing the module. </p>
*
* @return A string describing the module
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
if(isOpen())
sb.append("open ");
sb.append("module { name: ").append(toNameAndVersion());
if(!requires.isEmpty())
sb.append(", ").append(requires);
if(!uses.isEmpty())
sb.append(", uses: ").append(uses);
if(!exports.isEmpty())
sb.append(", exports: ").append(exports);
if(!opens.isEmpty())
sb.append(", opens: ").append(opens);
if(!provides.isEmpty()) {
sb.append(", provides: ").append(provides);
}
sb.append(" }");
return sb.toString();
}
/**
* Tests this module descriptor for equality with the given object.
*
* <p> If the given object is not a {@code ModuleDescriptor} then this
* method returns {@code false}. Two module descriptors are equal if each
* of their corresponding components is equal. </p>
*
* <p> This method satisfies the general contract of the {@link
* java.lang.Object#equals(Object) Object.equals} method. </p>
*
* @param ob the object to which this object is to be compared
*
* @return {@code true} if, and only if, the given object is a module
* descriptor that is equal to this module descriptor
*/
@Override
public boolean equals(Object ob) {
if(ob == this)
return true;
if(!(ob instanceof ModuleDescriptor))
return false;
ModuleDescriptor that = (ModuleDescriptor) ob;
return (name.equals(that.name) && modifiers.equals(that.modifiers) && requires.equals(that.requires) && Objects.equals(packages, that.packages) && exports.equals(that.exports) && opens.equals(that.opens) && uses.equals(that.uses) && provides.equals(that.provides) && Objects.equals(version, that.version) && Objects.equals(rawVersionString, that.rawVersionString) && Objects.equals(mainClass, that.mainClass));
}
/**
* Computes a hash code for this module descriptor.
*
* <p> The hash code is based upon the components of the module descriptor,
* and satisfies the general contract of the {@link Object#hashCode
* Object.hashCode} method. </p>
*
* @return The hash-code value for this module descriptor
*/
@Override
public int hashCode() {
int hc = hash;
if(hc == 0) {
hc = name.hashCode();
hc = hc * 43 + Objects.hashCode(modifiers);
hc = hc * 43 + requires.hashCode();
hc = hc * 43 + Objects.hashCode(packages);
hc = hc * 43 + exports.hashCode();
hc = hc * 43 + opens.hashCode();
hc = hc * 43 + uses.hashCode();
hc = hc * 43 + provides.hashCode();
hc = hc * 43 + Objects.hashCode(version);
hc = hc * 43 + Objects.hashCode(rawVersionString);
hc = hc * 43 + Objects.hashCode(mainClass);
if(hc == 0)
hc = -1;
hash = hc;
}
return hc;
}
// 从给定的map构建一个只读map
private static <K, V> Map<K, V> emptyOrUnmodifiableMap(Map<K, V> map) {
if(map.isEmpty()) {
return Collections.emptyMap();
} else if(map.size() == 1) {
Map.Entry<K, V> entry = map.entrySet().iterator().next();
return Collections.singletonMap(entry.getKey(), entry.getValue());
} else {
return Collections.unmodifiableMap(map);
}
}
// 从给定的set构建一个只读set
private static <T> Set<T> emptyOrUnmodifiableSet(Set<T> set) {
if(set.isEmpty()) {
return Collections.emptySet();
} else if(set.size() == 1) {
return Collections.singleton(set.iterator().next());
} else {
return Collections.unmodifiableSet(set);
}
}
// 从全限定类名中解析出包名
private static String packageName(String cn) {
int index = cn.lastIndexOf('.');
return (index == -1) ? "" : cn.substring(0, index);
}
private static <E extends Enum<E>> long modsValue(Set<E> set) {
long value = 0;
for(Enum<E> e : set) {
value += 1 << e.ordinal();
}
return value;
}
/**
* Returns a string containing the given set of modifiers and label.
*/
private static <M> String toString(Set<M> mods, String what) {
return (Stream.concat(mods.stream().map(e -> e.toString().toLowerCase(Locale.ROOT)), Stream.of(what))).collect(Collectors.joining(" "));
}
private static <T extends Object & Comparable<? super T>> int compare(T obj1, T obj2) {
if(obj1 != null) {
return (obj2 != null) ? obj1.compareTo(obj2) : 1;
} else {
return (obj2 == null) ? 0 : -1;
}
}
/**
* Compares two sets of {@code Comparable} objects.
*/
@SuppressWarnings("unchecked")
private static <T extends Object & Comparable<? super T>> int compare(Set<T> s1, Set<T> s2) {
T[] a1 = (T[]) s1.toArray();
T[] a2 = (T[]) s2.toArray();
Arrays.sort(a1);
Arrays.sort(a2);
return Arrays.compare(a1, a2);
}
/**
* A modifier on a module.
*
* @spec JPMS
* @see ModuleDescriptor#modifiers()
* @since 9
*/
// 模块描述符的修饰符
public enum Modifier {
/**
* An open module. An open module does not declare any open packages
* but the resulting module is treated as if all packages are open.
*/
OPEN, // open模块
/**
* An automatic module. An automatic module is treated as if it exports
* and opens all packages.
*
* @apiNote This modifier does not correspond to a module flag in the
* binary form of a module declaration ({@code module-info.class}).
*/
AUTOMATIC, // 自动模块
/**
* The module was not explicitly or implicitly declared.
*/
SYNTHETIC, // 非显式或隐式声明的模块,如JDK动态代理中构造的模块
/**
* The module was implicitly declared.
*/
MANDATED // 隐式声明的模块,如asm字节码操作中构造的模块
}
/**
* <p> A dependence upon a module </p>
*
* @spec JPMS
* @see ModuleDescriptor#requires()
* @since 9
*/
// 标记为requires的模块
public final static class Requires implements Comparable<Requires> {
private final String name; // 标记为requires的模块的名称
private final Set<Modifier> mods; // 标记为requires的模块的修饰符,例如requires static
private final Version compiledVersion;
private final String rawCompiledVersion;
private Requires(Set<Modifier> ms, String mn, Version v, String vs) {
assert v == null || vs == null;
if(ms.isEmpty()) {
ms = Collections.emptySet();
} else {
ms = Collections.unmodifiableSet(EnumSet.copyOf(ms));
}
this.mods = ms;
this.name = mn;
this.compiledVersion = v;
this.rawCompiledVersion = vs;
}
private Requires(Set<Modifier> ms, String mn, Version v, boolean unused) {
this.mods = ms;
this.name = mn;
this.compiledVersion = v;
this.rawCompiledVersion = null;
}
/**
* Returns the set of modifiers.
*
* @return A possibly-empty unmodifiable set of modifiers
*/
public Set<Modifier> modifiers() {
return mods;
}
/**
* Return the module name.
*
* @return The module name
*/
public String name() {
return name;
}
/**
* Returns the version of the module if recorded at compile-time.
*
* @return The version of the module if recorded at compile-time,
* or an empty {@code Optional} if no version was recorded or
* the version string recorded is {@linkplain Version#parse(String)
* unparseable}
*/
public Optional<Version> compiledVersion() {
return Optional.ofNullable(compiledVersion);
}
/**
* Returns the string with the possibly-unparseable version of the module
* if recorded at compile-time.
*
* @return The string containing the version of the module if recorded
* at compile-time, or an empty {@code Optional} if no version
* was recorded
*
* @see #compiledVersion()
*/
public Optional<String> rawCompiledVersion() {
if(compiledVersion != null) {
return Optional.of(compiledVersion.toString());
} else {
return Optional.ofNullable(rawCompiledVersion);
}
}
/**
* Compares this module dependence to another.
*
* <p> Two {@code Requires} objects are compared by comparing their
* module names lexicographically. Where the module names are equal
* then the sets of modifiers are compared in the same way that
* module modifiers are compared (see {@link ModuleDescriptor#compareTo
* ModuleDescriptor.compareTo}). Where the module names are equal and
* the set of modifiers are equal then the version of the modules