开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
项目仓库所选许可证以仓库主分支所使用许可证为准
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
Module.java 56.19 KB
一键复制 编辑 原始数据 按行查看 历史
gwdcode 提交于 2020年10月30日 22:13 +08:00 . 常用普通java包(jdk11.0.2)
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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
/*
* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package java.lang;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.module.Configuration;
import java.lang.module.ModuleReference;
import java.lang.module.ModuleDescriptor;
import java.lang.module.ModuleDescriptor.Exports;
import java.lang.module.ModuleDescriptor.Opens;
import java.lang.module.ModuleDescriptor.Version;
import java.lang.module.ResolvedModule;
import java.lang.reflect.AnnotatedElement;
import java.net.URI;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import jdk.internal.loader.BuiltinClassLoader;
import jdk.internal.loader.BootLoader;
import jdk.internal.loader.ClassLoaders;
import jdk.internal.module.IllegalAccessLogger;
import jdk.internal.module.ModuleLoaderMap;
import jdk.internal.module.ServicesCatalog;
import jdk.internal.module.Resources;
import jdk.internal.org.objectweb.asm.AnnotationVisitor;
import jdk.internal.org.objectweb.asm.Attribute;
import jdk.internal.org.objectweb.asm.ClassReader;
import jdk.internal.org.objectweb.asm.ClassVisitor;
import jdk.internal.org.objectweb.asm.ClassWriter;
import jdk.internal.org.objectweb.asm.ModuleVisitor;
import jdk.internal.org.objectweb.asm.Opcodes;
import jdk.internal.reflect.CallerSensitive;
import jdk.internal.reflect.Reflection;
import sun.security.util.SecurityConstants;
/**
* Represents a run-time module, either {@link #isNamed() named} or unnamed.
*
* <p> Named modules have a {@link #getName() name} and are constructed by the
* Java Virtual Machine when a graph of modules is defined to the Java virtual
* machine to create a {@linkplain ModuleLayer module layer}. </p>
*
* <p> An unnamed module does not have a name. There is an unnamed module for
* each {@link ClassLoader ClassLoader}, obtained by invoking its {@link
* ClassLoader#getUnnamedModule() getUnnamedModule} method. All types that are
* not in a named module are members of their defining class loader's unnamed
* module. </p>
*
* <p> The package names that are parameters or returned by methods defined in
* this class are the fully-qualified names of the packages as defined in
* section 6.5.3 of <cite>The Java&trade; Language Specification</cite>, for
* example, {@code "java.lang"}. </p>
*
* <p> Unless otherwise specified, passing a {@code null} argument to a method
* in this class causes a {@link NullPointerException NullPointerException} to
* be thrown. </p>
*
* @since 9
* @spec JPMS
* @see Class#getModule()
*/
public final class Module implements AnnotatedElement {
// the layer that contains this module, can be null
private final ModuleLayer layer;
// module name and loader, these fields are read by VM
private final String name;
private final ClassLoader loader;
// the module descriptor
private final ModuleDescriptor descriptor;
/**
* Creates a new named Module. The resulting Module will be defined to the
* VM but will not read any other modules, will not have any exports setup
* and will not be registered in the service catalog.
*/
Module(ModuleLayer layer,
ClassLoader loader,
ModuleDescriptor descriptor,
URI uri)
{
this.layer = layer;
this.name = descriptor.name();
this.loader = loader;
this.descriptor = descriptor;
// define module to VM
boolean isOpen = descriptor.isOpen() || descriptor.isAutomatic();
Version version = descriptor.version().orElse(null);
String vs = Objects.toString(version, null);
String loc = Objects.toString(uri, null);
String[] packages = descriptor.packages().toArray(new String[0]);
defineModule0(this, isOpen, vs, loc, packages);
}
/**
* Create the unnamed Module for the given ClassLoader.
*
* @see ClassLoader#getUnnamedModule
*/
Module(ClassLoader loader) {
this.layer = null;
this.name = null;
this.loader = loader;
this.descriptor = null;
}
/**
* Creates a named module but without defining the module to the VM.
*
* @apiNote This constructor is for VM white-box testing.
*/
Module(ClassLoader loader, ModuleDescriptor descriptor) {
this.layer = null;
this.name = descriptor.name();
this.loader = loader;
this.descriptor = descriptor;
}
/**
* Returns {@code true} if this module is a named module.
*
* @return {@code true} if this is a named module
*
* @see ClassLoader#getUnnamedModule()
*/
public boolean isNamed() {
return name != null;
}
/**
* Returns the module name or {@code null} if this module is an unnamed
* module.
*
* @return The module name
*/
public String getName() {
return name;
}
/**
* Returns the {@code ClassLoader} for this module.
*
* <p> If there is a security manager then its {@code checkPermission}
* method if first called with a {@code RuntimePermission("getClassLoader")}
* permission to check that the caller is allowed to get access to the
* class loader. </p>
*
* @return The class loader for this module
*
* @throws SecurityException
* If denied by the security manager
*/
public ClassLoader getClassLoader() {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
}
return loader;
}
/**
* Returns the module descriptor for this module or {@code null} if this
* module is an unnamed module.
*
* @return The module descriptor for this module
*/
public ModuleDescriptor getDescriptor() {
return descriptor;
}
/**
* Returns the module layer that contains this module or {@code null} if
* this module is not in a module layer.
*
* A module layer contains named modules and therefore this method always
* returns {@code null} when invoked on an unnamed module.
*
* <p> <a href="reflect/Proxy.html#dynamicmodule">Dynamic modules</a> are
* named modules that are generated at runtime. A dynamic module may or may
* not be in a module layer. </p>
*
* @return The module layer that contains this module
*
* @see java.lang.reflect.Proxy
*/
public ModuleLayer getLayer() {
if (isNamed()) {
ModuleLayer layer = this.layer;
if (layer != null)
return layer;
// special-case java.base as it is created before the boot layer
if (loader == null && name.equals("java.base")) {
return ModuleLayer.boot();
}
}
return null;
}
// --
// special Module to mean "all unnamed modules"
private static final Module ALL_UNNAMED_MODULE = new Module(null);
private static final Set<Module> ALL_UNNAMED_MODULE_SET = Set.of(ALL_UNNAMED_MODULE);
// special Module to mean "everyone"
private static final Module EVERYONE_MODULE = new Module(null);
private static final Set<Module> EVERYONE_SET = Set.of(EVERYONE_MODULE);
/**
* The holder of data structures to support readability, exports, and
* service use added at runtime with the reflective APIs.
*/
private static class ReflectionData {
/**
* A module (1st key) reads another module (2nd key)
*/
static final WeakPairMap<Module, Module, Boolean> reads =
new WeakPairMap<>();
/**
* A module (1st key) exports or opens a package to another module
* (2nd key). The map value is a map of package name to a boolean
* that indicates if the package is opened.
*/
static final WeakPairMap<Module, Module, Map<String, Boolean>> exports =
new WeakPairMap<>();
/**
* A module (1st key) uses a service (2nd key)
*/
static final WeakPairMap<Module, Class<?>, Boolean> uses =
new WeakPairMap<>();
}
// -- readability --
// the modules that this module reads
private volatile Set<Module> reads;
/**
* Indicates if this module reads the given module. This method returns
* {@code true} if invoked to test if this module reads itself. It also
* returns {@code true} if invoked on an unnamed module (as unnamed
* modules read all modules).
*
* @param other
* The other module
*
* @return {@code true} if this module reads {@code other}
*
* @see #addReads(Module)
*/
public boolean canRead(Module other) {
Objects.requireNonNull(other);
// an unnamed module reads all modules
if (!this.isNamed())
return true;
// all modules read themselves
if (other == this)
return true;
// check if this module reads other
if (other.isNamed()) {
Set<Module> reads = this.reads; // volatile read
if (reads != null && reads.contains(other))
return true;
}
// check if this module reads the other module reflectively
if (ReflectionData.reads.containsKeyPair(this, other))
return true;
// if other is an unnamed module then check if this module reads
// all unnamed modules
if (!other.isNamed()
&& ReflectionData.reads.containsKeyPair(this, ALL_UNNAMED_MODULE))
return true;
return false;
}
/**
* If the caller's module is this module then update this module to read
* the given module.
*
* This method is a no-op if {@code other} is this module (all modules read
* themselves), this module is an unnamed module (as unnamed modules read
* all modules), or this module already reads {@code other}.
*
* @implNote <em>Read edges</em> added by this method are <em>weak</em> and
* do not prevent {@code other} from being GC'ed when this module is
* strongly reachable.
*
* @param other
* The other module
*
* @return this module
*
* @throws IllegalCallerException
* If this is a named module and the caller's module is not this
* module
*
* @see #canRead
*/
@CallerSensitive
public Module addReads(Module other) {
Objects.requireNonNull(other);
if (this.isNamed()) {
Module caller = getCallerModule(Reflection.getCallerClass());
if (caller != this) {
throw new IllegalCallerException(caller + " != " + this);
}
implAddReads(other, true);
}
return this;
}
/**
* Updates this module to read another module.
*
* @apiNote Used by the --add-reads command line option.
*/
void implAddReads(Module other) {
implAddReads(other, true);
}
/**
* Updates this module to read all unnamed modules.
*
* @apiNote Used by the --add-reads command line option.
*/
void implAddReadsAllUnnamed() {
implAddReads(Module.ALL_UNNAMED_MODULE, true);
}
/**
* Updates this module to read another module without notifying the VM.
*
* @apiNote This method is for VM white-box testing.
*/
void implAddReadsNoSync(Module other) {
implAddReads(other, false);
}
/**
* Makes the given {@code Module} readable to this module.
*
* If {@code syncVM} is {@code true} then the VM is notified.
*/
private void implAddReads(Module other, boolean syncVM) {
Objects.requireNonNull(other);
if (!canRead(other)) {
// update VM first, just in case it fails
if (syncVM) {
if (other == ALL_UNNAMED_MODULE) {
addReads0(this, null);
} else {
addReads0(this, other);
}
}
// add reflective read
ReflectionData.reads.putIfAbsent(this, other, Boolean.TRUE);
}
}
// -- exported and open packages --
// the packages are open to other modules, can be null
// if the value contains EVERYONE_MODULE then the package is open to all
private volatile Map<String, Set<Module>> openPackages;
// the packages that are exported, can be null
// if the value contains EVERYONE_MODULE then the package is exported to all
private volatile Map<String, Set<Module>> exportedPackages;
/**
* Returns {@code true} if this module exports the given package to at
* least the given module.
*
* <p> This method returns {@code true} if invoked to test if a package in
* this module is exported to itself. It always returns {@code true} when
* invoked on an unnamed module. A package that is {@link #isOpen open} to
* the given module is considered exported to that module at run-time and
* so this method returns {@code true} if the package is open to the given
* module. </p>
*
* <p> This method does not check if the given module reads this module. </p>
*
* @param pn
* The package name
* @param other
* The other module
*
* @return {@code true} if this module exports the package to at least the
* given module
*
* @see ModuleDescriptor#exports()
* @see #addExports(String,Module)
*/
public boolean isExported(String pn, Module other) {
Objects.requireNonNull(pn);
Objects.requireNonNull(other);
return implIsExportedOrOpen(pn, other, /*open*/false);
}
/**
* Returns {@code true} if this module has <em>opened</em> a package to at
* least the given module.
*
* <p> This method returns {@code true} if invoked to test if a package in
* this module is open to itself. It returns {@code true} when invoked on an
* {@link ModuleDescriptor#isOpen open} module with a package in the module.
* It always returns {@code true} when invoked on an unnamed module. </p>
*
* <p> This method does not check if the given module reads this module. </p>
*
* @param pn
* The package name
* @param other
* The other module
*
* @return {@code true} if this module has <em>opened</em> the package
* to at least the given module
*
* @see ModuleDescriptor#opens()
* @see #addOpens(String,Module)
* @see AccessibleObject#setAccessible(boolean)
* @see java.lang.invoke.MethodHandles#privateLookupIn
*/
public boolean isOpen(String pn, Module other) {
Objects.requireNonNull(pn);
Objects.requireNonNull(other);
return implIsExportedOrOpen(pn, other, /*open*/true);
}
/**
* Returns {@code true} if this module exports the given package
* unconditionally.
*
* <p> This method always returns {@code true} when invoked on an unnamed
* module. A package that is {@link #isOpen(String) opened} unconditionally
* is considered exported unconditionally at run-time and so this method
* returns {@code true} if the package is opened unconditionally. </p>
*
* <p> This method does not check if the given module reads this module. </p>
*
* @param pn
* The package name
*
* @return {@code true} if this module exports the package unconditionally
*
* @see ModuleDescriptor#exports()
*/
public boolean isExported(String pn) {
Objects.requireNonNull(pn);
return implIsExportedOrOpen(pn, EVERYONE_MODULE, /*open*/false);
}
/**
* Returns {@code true} if this module has <em>opened</em> a package
* unconditionally.
*
* <p> This method always returns {@code true} when invoked on an unnamed
* module. Additionally, it always returns {@code true} when invoked on an
* {@link ModuleDescriptor#isOpen open} module with a package in the
* module. </p>
*
* <p> This method does not check if the given module reads this module. </p>
*
* @param pn
* The package name
*
* @return {@code true} if this module has <em>opened</em> the package
* unconditionally
*
* @see ModuleDescriptor#opens()
*/
public boolean isOpen(String pn) {
Objects.requireNonNull(pn);
return implIsExportedOrOpen(pn, EVERYONE_MODULE, /*open*/true);
}
/**
* Returns {@code true} if this module exports or opens the given package
* to the given module. If the other module is {@code EVERYONE_MODULE} then
* this method tests if the package is exported or opened unconditionally.
*/
private boolean implIsExportedOrOpen(String pn, Module other, boolean open) {
// all packages in unnamed modules are open
if (!isNamed())
return true;
// all packages are exported/open to self
if (other == this && descriptor.packages().contains(pn))
return true;
// all packages in open and automatic modules are open
if (descriptor.isOpen() || descriptor.isAutomatic())
return descriptor.packages().contains(pn);
// exported/opened via module declaration/descriptor
if (isStaticallyExportedOrOpen(pn, other, open))
return true;
// exported via addExports/addOpens
if (isReflectivelyExportedOrOpen(pn, other, open))
return true;
// not exported or open to other
return false;
}
/**
* Returns {@code true} if this module exports or opens a package to
* the given module via its module declaration or CLI options.
*/
private boolean isStaticallyExportedOrOpen(String pn, Module other, boolean open) {
// test if package is open to everyone or <other>
Map<String, Set<Module>> openPackages = this.openPackages;
if (openPackages != null && allows(openPackages.get(pn), other)) {
return true;
}
if (!open) {
// test package is exported to everyone or <other>
Map<String, Set<Module>> exportedPackages = this.exportedPackages;
if (exportedPackages != null && allows(exportedPackages.get(pn), other)) {
return true;
}
}
return false;
}
/**
* Returns {@code true} if targets is non-null and contains EVERYONE_MODULE
* or the given module. Also returns true if the given module is an unnamed
* module and targets contains ALL_UNNAMED_MODULE.
*/
private boolean allows(Set<Module> targets, Module module) {
if (targets != null) {
if (targets.contains(EVERYONE_MODULE))
return true;
if (module != EVERYONE_MODULE) {
if (targets.contains(module))
return true;
if (!module.isNamed() && targets.contains(ALL_UNNAMED_MODULE))
return true;
}
}
return false;
}
/**
* Returns {@code true} if this module reflectively exports or opens the
* given package to the given module.
*/
private boolean isReflectivelyExportedOrOpen(String pn, Module other, boolean open) {
// exported or open to all modules
Map<String, Boolean> exports = ReflectionData.exports.get(this, EVERYONE_MODULE);
if (exports != null) {
Boolean b = exports.get(pn);
if (b != null) {
boolean isOpen = b.booleanValue();
if (!open || isOpen) return true;
}
}
if (other != EVERYONE_MODULE) {
// exported or open to other
exports = ReflectionData.exports.get(this, other);
if (exports != null) {
Boolean b = exports.get(pn);
if (b != null) {
boolean isOpen = b.booleanValue();
if (!open || isOpen) return true;
}
}
// other is an unnamed module && exported or open to all unnamed
if (!other.isNamed()) {
exports = ReflectionData.exports.get(this, ALL_UNNAMED_MODULE);
if (exports != null) {
Boolean b = exports.get(pn);
if (b != null) {
boolean isOpen = b.booleanValue();
if (!open || isOpen) return true;
}
}
}
}
return false;
}
/**
* Returns {@code true} if this module reflectively exports the
* given package to the given module.
*/
boolean isReflectivelyExported(String pn, Module other) {
return isReflectivelyExportedOrOpen(pn, other, false);
}
/**
* Returns {@code true} if this module reflectively opens the
* given package to the given module.
*/
boolean isReflectivelyOpened(String pn, Module other) {
return isReflectivelyExportedOrOpen(pn, other, true);
}
/**
* If the caller's module is this module then update this module to export
* the given package to the given module.
*
* <p> This method has no effect if the package is already exported (or
* <em>open</em>) to the given module. </p>
*
* @apiNote As specified in section 5.4.3 of the <cite>The Java&trade;
* Virtual Machine Specification </cite>, if an attempt to resolve a
* symbolic reference fails because of a linkage error, then subsequent
* attempts to resolve the reference always fail with the same error that
* was thrown as a result of the initial resolution attempt.
*
* @param pn
* The package name
* @param other
* The module
*
* @return this module
*
* @throws IllegalArgumentException
* If {@code pn} is {@code null}, or this is a named module and the
* package {@code pn} is not a package in this module
* @throws IllegalCallerException
* If this is a named module and the caller's module is not this
* module
*
* @jvms 5.4.3 Resolution
* @see #isExported(String,Module)
*/
@CallerSensitive
public Module addExports(String pn, Module other) {
if (pn == null)
throw new IllegalArgumentException("package is null");
Objects.requireNonNull(other);
if (isNamed()) {
Module caller = getCallerModule(Reflection.getCallerClass());
if (caller != this) {
throw new IllegalCallerException(caller + " != " + this);
}
implAddExportsOrOpens(pn, other, /*open*/false, /*syncVM*/true);
}
return this;
}
/**
* If this module has <em>opened</em> a package to at least the caller
* module then update this module to open the package to the given module.
* Opening a package with this method allows all types in the package,
* and all their members, not just public types and their public members,
* to be reflected on by the given module when using APIs that support
* private access or a way to bypass or suppress default Java language
* access control checks.
*
* <p> This method has no effect if the package is already <em>open</em>
* to the given module. </p>
*
* @apiNote This method can be used for cases where a <em>consumer
* module</em> uses a qualified opens to open a package to an <em>API
* module</em> but where the reflective access to the members of classes in
* the consumer module is delegated to code in another module. Code in the
* API module can use this method to open the package in the consumer module
* to the other module.
*
* @param pn
* The package name
* @param other
* The module
*
* @return this module
*
* @throws IllegalArgumentException
* If {@code pn} is {@code null}, or this is a named module and the
* package {@code pn} is not a package in this module
* @throws IllegalCallerException
* If this is a named module and this module has not opened the
* package to at least the caller's module
*
* @see #isOpen(String,Module)
* @see AccessibleObject#setAccessible(boolean)
* @see java.lang.invoke.MethodHandles#privateLookupIn
*/
@CallerSensitive
public Module addOpens(String pn, Module other) {
if (pn == null)
throw new IllegalArgumentException("package is null");
Objects.requireNonNull(other);
if (isNamed()) {
Module caller = getCallerModule(Reflection.getCallerClass());
if (caller != this && (caller == null || !isOpen(pn, caller)))
throw new IllegalCallerException(pn + " is not open to " + caller);
implAddExportsOrOpens(pn, other, /*open*/true, /*syncVM*/true);
}
return this;
}
/**
* Updates this module to export a package unconditionally.
*
* @apiNote This method is for JDK tests only.
*/
void implAddExports(String pn) {
implAddExportsOrOpens(pn, Module.EVERYONE_MODULE, false, true);
}
/**
* Updates this module to export a package to another module.
*
* @apiNote Used by Instrumentation::redefineModule and --add-exports
*/
void implAddExports(String pn, Module other) {
implAddExportsOrOpens(pn, other, false, true);
}
/**
* Updates this module to export a package to all unnamed modules.
*
* @apiNote Used by the --add-exports command line option.
*/
void implAddExportsToAllUnnamed(String pn) {
implAddExportsOrOpens(pn, Module.ALL_UNNAMED_MODULE, false, true);
}
/**
* Updates this export to export a package unconditionally without
* notifying the VM.
*
* @apiNote This method is for VM white-box testing.
*/
void implAddExportsNoSync(String pn) {
implAddExportsOrOpens(pn.replace('/', '.'), Module.EVERYONE_MODULE, false, false);
}
/**
* Updates a module to export a package to another module without
* notifying the VM.
*
* @apiNote This method is for VM white-box testing.
*/
void implAddExportsNoSync(String pn, Module other) {
implAddExportsOrOpens(pn.replace('/', '.'), other, false, false);
}
/**
* Updates this module to open a package unconditionally.
*
* @apiNote This method is for JDK tests only.
*/
void implAddOpens(String pn) {
implAddExportsOrOpens(pn, Module.EVERYONE_MODULE, true, true);
}
/**
* Updates this module to open a package to another module.
*
* @apiNote Used by Instrumentation::redefineModule and --add-opens
*/
void implAddOpens(String pn, Module other) {
implAddExportsOrOpens(pn, other, true, true);
}
/**
* Updates this module to open a package to all unnamed modules.
*
* @apiNote Used by the --add-opens command line option.
*/
void implAddOpensToAllUnnamed(String pn) {
implAddExportsOrOpens(pn, Module.ALL_UNNAMED_MODULE, true, true);
}
/**
* Updates a module to export or open a module to another module.
*
* If {@code syncVM} is {@code true} then the VM is notified.
*/
private void implAddExportsOrOpens(String pn,
Module other,
boolean open,
boolean syncVM) {
Objects.requireNonNull(other);
Objects.requireNonNull(pn);
// all packages are open in unnamed, open, and automatic modules
if (!isNamed() || descriptor.isOpen() || descriptor.isAutomatic())
return;
// check if the package is already exported/open to other
if (implIsExportedOrOpen(pn, other, open)) {
// if the package is exported/open for illegal access then we need
// to record that it has also been exported/opened reflectively so
// that the IllegalAccessLogger doesn't emit a warning.
boolean needToAdd = false;
if (!other.isNamed()) {
IllegalAccessLogger l = IllegalAccessLogger.illegalAccessLogger();
if (l != null) {
if (open) {
needToAdd = l.isOpenForIllegalAccess(this, pn);
} else {
needToAdd = l.isExportedForIllegalAccess(this, pn);
}
}
}
if (!needToAdd) {
// nothing to do
return;
}
}
// can only export a package in the module
if (!descriptor.packages().contains(pn)) {
throw new IllegalArgumentException("package " + pn
+ " not in contents");
}
// update VM first, just in case it fails
if (syncVM) {
if (other == EVERYONE_MODULE) {
addExportsToAll0(this, pn);
} else if (other == ALL_UNNAMED_MODULE) {
addExportsToAllUnnamed0(this, pn);
} else {
addExports0(this, pn, other);
}
}
// add package name to exports if absent
Map<String, Boolean> map = ReflectionData.exports
.computeIfAbsent(this, other,
(m1, m2) -> new ConcurrentHashMap<>());
if (open) {
map.put(pn, Boolean.TRUE); // may need to promote from FALSE to TRUE
} else {
map.putIfAbsent(pn, Boolean.FALSE);
}
}
/**
* Updates a module to open all packages returned by the given iterator to
* all unnamed modules.
*
* @apiNote Used during startup to open packages for illegal access.
*/
void implAddOpensToAllUnnamed(Iterator<String> iterator) {
if (jdk.internal.misc.VM.isModuleSystemInited()) {
throw new IllegalStateException("Module system already initialized");
}
// replace this module's openPackages map with a new map that opens
// the packages to all unnamed modules.
Map<String, Set<Module>> openPackages = this.openPackages;
if (openPackages == null) {
openPackages = new HashMap<>();
} else {
openPackages = new HashMap<>(openPackages);
}
while (iterator.hasNext()) {
String pn = iterator.next();
Set<Module> prev = openPackages.putIfAbsent(pn, ALL_UNNAMED_MODULE_SET);
if (prev != null) {
prev.add(ALL_UNNAMED_MODULE);
}
// update VM to export the package
addExportsToAllUnnamed0(this, pn);
}
this.openPackages = openPackages;
}
// -- services --
/**
* If the caller's module is this module then update this module to add a
* service dependence on the given service type. This method is intended
* for use by frameworks that invoke {@link java.util.ServiceLoader
* ServiceLoader} on behalf of other modules or where the framework is
* passed a reference to the service type by other code. This method is
* a no-op when invoked on an unnamed module or an automatic module.
*
* <p> This method does not cause {@link Configuration#resolveAndBind
* resolveAndBind} to be re-run. </p>
*
* @param service
* The service type
*
* @return this module
*
* @throws IllegalCallerException
* If this is a named module and the caller's module is not this
* module
*
* @see #canUse(Class)
* @see ModuleDescriptor#uses()
*/
@CallerSensitive
public Module addUses(Class<?> service) {
Objects.requireNonNull(service);
if (isNamed() && !descriptor.isAutomatic()) {
Module caller = getCallerModule(Reflection.getCallerClass());
if (caller != this) {
throw new IllegalCallerException(caller + " != " + this);
}
implAddUses(service);
}
return this;
}
/**
* Update this module to add a service dependence on the given service
* type.
*/
void implAddUses(Class<?> service) {
if (!canUse(service)) {
ReflectionData.uses.putIfAbsent(this, service, Boolean.TRUE);
}
}
/**
* Indicates if this module has a service dependence on the given service
* type. This method always returns {@code true} when invoked on an unnamed
* module or an automatic module.
*
* @param service
* The service type
*
* @return {@code true} if this module uses service type {@code st}
*
* @see #addUses(Class)
*/
public boolean canUse(Class<?> service) {
Objects.requireNonNull(service);
if (!isNamed())
return true;
if (descriptor.isAutomatic())
return true;
// uses was declared
if (descriptor.uses().contains(service.getName()))
return true;
// uses added via addUses
return ReflectionData.uses.containsKeyPair(this, service);
}
// -- packages --
/**
* Returns the set of package names for the packages in this module.
*
* <p> For named modules, the returned set contains an element for each
* package in the module. </p>
*
* <p> For unnamed modules, this method is the equivalent to invoking the
* {@link ClassLoader#getDefinedPackages() getDefinedPackages} method of
* this module's class loader and returning the set of package names. </p>
*
* @return the set of the package names of the packages in this module
*/
public Set<String> getPackages() {
if (isNamed()) {
return descriptor.packages();
} else {
// unnamed module
Stream<Package> packages;
if (loader == null) {
packages = BootLoader.packages();
} else {
packages = loader.packages();
}
return packages.map(Package::getName).collect(Collectors.toSet());
}
}
// -- creating Module objects --
/**
* Defines all module in a configuration to the runtime.
*
* @return a map of module name to runtime {@code Module}
*
* @throws IllegalArgumentException
* If defining any of the modules to the VM fails
*/
static Map<String, Module> defineModules(Configuration cf,
Function<String, ClassLoader> clf,
ModuleLayer layer)
{
boolean isBootLayer = (ModuleLayer.boot() == null);
int cap = (int)(cf.modules().size() / 0.75f + 1.0f);
Map<String, Module> nameToModule = new HashMap<>(cap);
Map<String, ClassLoader> nameToLoader = new HashMap<>(cap);
Set<ClassLoader> loaders = new HashSet<>();
boolean hasPlatformModules = false;
// map each module to a class loader
for (ResolvedModule resolvedModule : cf.modules()) {
String name = resolvedModule.name();
ClassLoader loader = clf.apply(name);
nameToLoader.put(name, loader);
if (loader == null || loader == ClassLoaders.platformClassLoader()) {
if (!(clf instanceof ModuleLoaderMap.Mapper)) {
throw new IllegalArgumentException("loader can't be 'null'"
+ " or the platform class loader");
}
hasPlatformModules = true;
} else {
loaders.add(loader);
}
}
// define each module in the configuration to the VM
for (ResolvedModule resolvedModule : cf.modules()) {
ModuleReference mref = resolvedModule.reference();
ModuleDescriptor descriptor = mref.descriptor();
String name = descriptor.name();
ClassLoader loader = nameToLoader.get(name);
Module m;
if (loader == null && name.equals("java.base")) {
// java.base is already defined to the VM
m = Object.class.getModule();
} else {
URI uri = mref.location().orElse(null);
m = new Module(layer, loader, descriptor, uri);
}
nameToModule.put(name, m);
}
// setup readability and exports/opens
for (ResolvedModule resolvedModule : cf.modules()) {
ModuleReference mref = resolvedModule.reference();
ModuleDescriptor descriptor = mref.descriptor();
String mn = descriptor.name();
Module m = nameToModule.get(mn);
assert m != null;
// reads
Set<Module> reads = new HashSet<>();
// name -> source Module when in parent layer
Map<String, Module> nameToSource = Collections.emptyMap();
for (ResolvedModule other : resolvedModule.reads()) {
Module m2 = null;
if (other.configuration() == cf) {
// this configuration
m2 = nameToModule.get(other.name());
assert m2 != null;
} else {
// parent layer
for (ModuleLayer parent: layer.parents()) {
m2 = findModule(parent, other);
if (m2 != null)
break;
}
assert m2 != null;
if (nameToSource.isEmpty())
nameToSource = new HashMap<>();
nameToSource.put(other.name(), m2);
}
reads.add(m2);
// update VM view
addReads0(m, m2);
}
m.reads = reads;
// automatic modules read all unnamed modules
if (descriptor.isAutomatic()) {
m.implAddReads(ALL_UNNAMED_MODULE, true);
}
// exports and opens, skipped for open and automatic
if (!descriptor.isOpen() && !descriptor.isAutomatic()) {
if (isBootLayer && descriptor.opens().isEmpty()) {
// no open packages, no qualified exports to modules in parent layers
initExports(m, nameToModule);
} else {
initExportsAndOpens(m, nameToSource, nameToModule, layer.parents());
}
}
}
// if there are modules defined to the boot or platform class loaders
// then register the modules in the class loader's services catalog
if (hasPlatformModules) {
ClassLoader pcl = ClassLoaders.platformClassLoader();
ServicesCatalog bootCatalog = BootLoader.getServicesCatalog();
ServicesCatalog pclCatalog = ServicesCatalog.getServicesCatalog(pcl);
for (ResolvedModule resolvedModule : cf.modules()) {
ModuleReference mref = resolvedModule.reference();
ModuleDescriptor descriptor = mref.descriptor();
if (!descriptor.provides().isEmpty()) {
String name = descriptor.name();
Module m = nameToModule.get(name);
ClassLoader loader = nameToLoader.get(name);
if (loader == null) {
bootCatalog.register(m);
} else if (loader == pcl) {
pclCatalog.register(m);
}
}
}
}
// record that there is a layer with modules defined to the class loader
for (ClassLoader loader : loaders) {
layer.bindToLoader(loader);
}
return nameToModule;
}
/**
* Find the runtime Module corresponding to the given ResolvedModule
* in the given parent layer (or its parents).
*/
private static Module findModule(ModuleLayer parent,
ResolvedModule resolvedModule) {
Configuration cf = resolvedModule.configuration();
String dn = resolvedModule.name();
return parent.layers()
.filter(l -> l.configuration() == cf)
.findAny()
.map(layer -> {
Optional<Module> om = layer.findModule(dn);
assert om.isPresent() : dn + " not found in layer";
Module m = om.get();
assert m.getLayer() == layer : m + " not in expected layer";
return m;
})
.orElse(null);
}
/**
* Initialize/setup a module's exports.
*
* @param m the module
* @param nameToModule map of module name to Module (for qualified exports)
*/
private static void initExports(Module m, Map<String, Module> nameToModule) {
Map<String, Set<Module>> exportedPackages = new HashMap<>();
for (Exports exports : m.getDescriptor().exports()) {
String source = exports.source();
if (exports.isQualified()) {
// qualified exports
Set<Module> targets = new HashSet<>();
for (String target : exports.targets()) {
Module m2 = nameToModule.get(target);
if (m2 != null) {
addExports0(m, source, m2);
targets.add(m2);
}
}
if (!targets.isEmpty()) {
exportedPackages.put(source, targets);
}
} else {
// unqualified exports
addExportsToAll0(m, source);
exportedPackages.put(source, EVERYONE_SET);
}
}
if (!exportedPackages.isEmpty())
m.exportedPackages = exportedPackages;
}
/**
* Initialize/setup a module's exports.
*
* @param m the module
* @param nameToSource map of module name to Module for modules that m reads
* @param nameToModule map of module name to Module for modules in the layer
* under construction
* @param parents the parent layers
*/
private static void initExportsAndOpens(Module m,
Map<String, Module> nameToSource,
Map<String, Module> nameToModule,
List<ModuleLayer> parents) {
ModuleDescriptor descriptor = m.getDescriptor();
Map<String, Set<Module>> openPackages = new HashMap<>();
Map<String, Set<Module>> exportedPackages = new HashMap<>();
// process the open packages first
for (Opens opens : descriptor.opens()) {
String source = opens.source();
if (opens.isQualified()) {
// qualified opens
Set<Module> targets = new HashSet<>();
for (String target : opens.targets()) {
Module m2 = findModule(target, nameToSource, nameToModule, parents);
if (m2 != null) {
addExports0(m, source, m2);
targets.add(m2);
}
}
if (!targets.isEmpty()) {
openPackages.put(source, targets);
}
} else {
// unqualified opens
addExportsToAll0(m, source);
openPackages.put(source, EVERYONE_SET);
}
}
// next the exports, skipping exports when the package is open
for (Exports exports : descriptor.exports()) {
String source = exports.source();
// skip export if package is already open to everyone
Set<Module> openToTargets = openPackages.get(source);
if (openToTargets != null && openToTargets.contains(EVERYONE_MODULE))
continue;
if (exports.isQualified()) {
// qualified exports
Set<Module> targets = new HashSet<>();
for (String target : exports.targets()) {
Module m2 = findModule(target, nameToSource, nameToModule, parents);
if (m2 != null) {
// skip qualified export if already open to m2
if (openToTargets == null || !openToTargets.contains(m2)) {
addExports0(m, source, m2);
targets.add(m2);
}
}
}
if (!targets.isEmpty()) {
exportedPackages.put(source, targets);
}
} else {
// unqualified exports
addExportsToAll0(m, source);
exportedPackages.put(source, EVERYONE_SET);
}
}
if (!openPackages.isEmpty())
m.openPackages = openPackages;
if (!exportedPackages.isEmpty())
m.exportedPackages = exportedPackages;
}
/**
* Find the runtime Module with the given name. The module name is the
* name of a target module in a qualified exports or opens directive.
*
* @param target The target module to find
* @param nameToSource The modules in parent layers that are read
* @param nameToModule The modules in the layer under construction
* @param parents The parent layers
*/
private static Module findModule(String target,
Map<String, Module> nameToSource,
Map<String, Module> nameToModule,
List<ModuleLayer> parents) {
Module m = nameToSource.get(target);
if (m == null) {
m = nameToModule.get(target);
if (m == null) {
for (ModuleLayer parent : parents) {
m = parent.findModule(target).orElse(null);
if (m != null) break;
}
}
}
return m;
}
// -- annotations --
/**
* {@inheritDoc}
* This method returns {@code null} when invoked on an unnamed module.
*/
@Override
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
return moduleInfoClass().getDeclaredAnnotation(annotationClass);
}
/**
* {@inheritDoc}
* This method returns an empty array when invoked on an unnamed module.
*/
@Override
public Annotation[] getAnnotations() {
return moduleInfoClass().getAnnotations();
}
/**
* {@inheritDoc}
* This method returns an empty array when invoked on an unnamed module.
*/
@Override
public Annotation[] getDeclaredAnnotations() {
return moduleInfoClass().getDeclaredAnnotations();
}
// cached class file with annotations
private volatile Class<?> moduleInfoClass;
private Class<?> moduleInfoClass() {
Class<?> clazz = this.moduleInfoClass;
if (clazz != null)
return clazz;
synchronized (this) {
clazz = this.moduleInfoClass;
if (clazz == null) {
if (isNamed()) {
PrivilegedAction<Class<?>> pa = this::loadModuleInfoClass;
clazz = AccessController.doPrivileged(pa);
}
if (clazz == null) {
class DummyModuleInfo { }
clazz = DummyModuleInfo.class;
}
this.moduleInfoClass = clazz;
}
return clazz;
}
}
private Class<?> loadModuleInfoClass() {
Class<?> clazz = null;
try (InputStream in = getResourceAsStream("module-info.class")) {
if (in != null)
clazz = loadModuleInfoClass(in);
} catch (Exception ignore) { }
return clazz;
}
/**
* Loads module-info.class as a package-private interface in a class loader
* that is a child of this module's class loader.
*/
private Class<?> loadModuleInfoClass(InputStream in) throws IOException {
final String MODULE_INFO = "module-info";
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
+ ClassWriter.COMPUTE_FRAMES);
ClassVisitor cv = new ClassVisitor(Opcodes.ASM6, cw) {
@Override
public void visit(int version,
int access,
String name,
String signature,
String superName,
String[] interfaces) {
cw.visit(version,
Opcodes.ACC_INTERFACE
+ Opcodes.ACC_ABSTRACT
+ Opcodes.ACC_SYNTHETIC,
MODULE_INFO,
null,
"java/lang/Object",
null);
}
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
// keep annotations
return super.visitAnnotation(desc, visible);
}
@Override
public void visitAttribute(Attribute attr) {
// drop non-annotation attributes
}
@Override
public ModuleVisitor visitModule(String name, int flags, String version) {
// drop Module attribute
return null;
}
};
ClassReader cr = new ClassReader(in);
cr.accept(cv, 0);
byte[] bytes = cw.toByteArray();
ClassLoader cl = new ClassLoader(loader) {
@Override
protected Class<?> findClass(String cn)throws ClassNotFoundException {
if (cn.equals(MODULE_INFO)) {
return super.defineClass(cn, bytes, 0, bytes.length);
} else {
throw new ClassNotFoundException(cn);
}
}
};
try {
return cl.loadClass(MODULE_INFO);
} catch (ClassNotFoundException e) {
throw new InternalError(e);
}
}
// -- misc --
/**
* Returns an input stream for reading a resource in this module.
* The {@code name} parameter is a {@code '/'}-separated path name that
* identifies the resource. As with {@link Class#getResourceAsStream
* Class.getResourceAsStream}, this method delegates to the module's class
* loader {@link ClassLoader#findResource(String,String)
* findResource(String,String)} method, invoking it with the module name
* (or {@code null} when the module is unnamed) and the name of the
* resource. If the resource name has a leading slash then it is dropped
* before delegation.
*
* <p> A resource in a named module may be <em>encapsulated</em> so that
* it cannot be located by code in other modules. Whether a resource can be
* located or not is determined as follows: </p>
*
* <ul>
* <li> If the resource name ends with "{@code .class}" then it is not
* encapsulated. </li>
*
* <li> A <em>package name</em> is derived from the resource name. If
* the package name is a {@linkplain #getPackages() package} in the
* module then the resource can only be located by the caller of this
* method when the package is {@linkplain #isOpen(String,Module) open}
* to at least the caller's module. If the resource is not in a
* package in the module then the resource is not encapsulated. </li>
* </ul>
*
* <p> In the above, the <em>package name</em> for a resource is derived
* from the subsequence of characters that precedes the last {@code '/'} in
* the name and then replacing each {@code '/'} character in the subsequence
* with {@code '.'}. A leading slash is ignored when deriving the package
* name. As an example, the package name derived for a resource named
* "{@code a/b/c/foo.properties}" is "{@code a.b.c}". A resource name
* with the name "{@code META-INF/MANIFEST.MF}" is never encapsulated
* because "{@code META-INF}" is not a legal package name. </p>
*
* <p> This method returns {@code null} if the resource is not in this
* module, the resource is encapsulated and cannot be located by the caller,
* or access to the resource is denied by the security manager. </p>
*
* @param name
* The resource name
*
* @return An input stream for reading the resource or {@code null}
*
* @throws IOException
* If an I/O error occurs
*
* @see Class#getResourceAsStream(String)
*/
@CallerSensitive
public InputStream getResourceAsStream(String name) throws IOException {
if (name.startsWith("/")) {
name = name.substring(1);
}
if (isNamed() && Resources.canEncapsulate(name)) {
Module caller = getCallerModule(Reflection.getCallerClass());
if (caller != this && caller != Object.class.getModule()) {
String pn = Resources.toPackageName(name);
if (getPackages().contains(pn)) {
if (caller == null && !isOpen(pn)) {
// no caller, package not open
return null;
}
if (!isOpen(pn, caller)) {
// package not open to caller
return null;
}
}
}
}
String mn = this.name;
// special-case built-in class loaders to avoid URL connection
if (loader == null) {
return BootLoader.findResourceAsStream(mn, name);
} else if (loader instanceof BuiltinClassLoader) {
return ((BuiltinClassLoader) loader).findResourceAsStream(mn, name);
}
// locate resource in module
URL url = loader.findResource(mn, name);
if (url != null) {
try {
return url.openStream();
} catch (SecurityException e) { }
}
return null;
}
/**
* Returns the string representation of this module. For a named module,
* the representation is the string {@code "module"}, followed by a space,
* and then the module name. For an unnamed module, the representation is
* the string {@code "unnamed module"}, followed by a space, and then an
* implementation specific string that identifies the unnamed module.
*
* @return The string representation of this module
*/
@Override
public String toString() {
if (isNamed()) {
return "module " + name;
} else {
String id = Integer.toHexString(System.identityHashCode(this));
return "unnamed module @" + id;
}
}
/**
* Returns the module that a given caller class is a member of. Returns
* {@code null} if the caller is {@code null}.
*/
private Module getCallerModule(Class<?> caller) {
return (caller != null) ? caller.getModule() : null;
}
// -- native methods --
// JVM_DefineModule
private static native void defineModule0(Module module,
boolean isOpen,
String version,
String location,
String[] pns);
// JVM_AddReadsModule
private static native void addReads0(Module from, Module to);
// JVM_AddModuleExports
private static native void addExports0(Module from, String pn, Module to);
// JVM_AddModuleExportsToAll
private static native void addExportsToAll0(Module from, String pn);
// JVM_AddModuleExportsToAllUnnamed
private static native void addExportsToAllUnnamed0(Module from, String pn);
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

取消

发行版

暂无发行版

贡献者

全部

语言

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/gwdcode/jdk11.0.2lib.src.java.base.java.git
git@gitee.com:gwdcode/jdk11.0.2lib.src.java.base.java.git
gwdcode
jdk11.0.2lib.src.java.base.java
JDK11.0.2-lib.src.java.base.java
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /