Module.java revision 17602:6efd1f0d5d64
1/* 2 * Copyright (c) 2014, 2017, 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 26package java.lang; 27 28import java.io.IOException; 29import java.io.InputStream; 30import java.lang.annotation.Annotation; 31import java.lang.module.Configuration; 32import java.lang.module.ModuleReference; 33import java.lang.module.ModuleDescriptor; 34import java.lang.module.ModuleDescriptor.Exports; 35import java.lang.module.ModuleDescriptor.Opens; 36import java.lang.module.ModuleDescriptor.Version; 37import java.lang.module.ResolvedModule; 38import java.lang.reflect.AnnotatedElement; 39import java.net.URI; 40import java.net.URL; 41import java.security.AccessController; 42import java.security.PrivilegedAction; 43import java.util.Collections; 44import java.util.HashMap; 45import java.util.HashSet; 46import java.util.List; 47import java.util.Map; 48import java.util.Objects; 49import java.util.Optional; 50import java.util.Set; 51import java.util.concurrent.ConcurrentHashMap; 52import java.util.function.Function; 53import java.util.stream.Collectors; 54import java.util.stream.Stream; 55 56import jdk.internal.loader.BuiltinClassLoader; 57import jdk.internal.loader.BootLoader; 58import jdk.internal.misc.JavaLangAccess; 59import jdk.internal.misc.SharedSecrets; 60import jdk.internal.module.ModuleLoaderMap; 61import jdk.internal.module.ServicesCatalog; 62import jdk.internal.module.Resources; 63import jdk.internal.org.objectweb.asm.AnnotationVisitor; 64import jdk.internal.org.objectweb.asm.Attribute; 65import jdk.internal.org.objectweb.asm.ClassReader; 66import jdk.internal.org.objectweb.asm.ClassVisitor; 67import jdk.internal.org.objectweb.asm.ClassWriter; 68import jdk.internal.org.objectweb.asm.Opcodes; 69import jdk.internal.reflect.CallerSensitive; 70import jdk.internal.reflect.Reflection; 71import sun.security.util.SecurityConstants; 72 73/** 74 * Represents a run-time module, either {@link #isNamed() named} or unnamed. 75 * 76 * <p> Named modules have a {@link #getName() name} and are constructed by the 77 * Java Virtual Machine when a graph of modules is defined to the Java virtual 78 * machine to create a {@linkplain ModuleLayer module layer}. </p> 79 * 80 * <p> An unnamed module does not have a name. There is an unnamed module for 81 * each {@link ClassLoader ClassLoader}, obtained by invoking its {@link 82 * ClassLoader#getUnnamedModule() getUnnamedModule} method. All types that are 83 * not in a named module are members of their defining class loader's unnamed 84 * module. </p> 85 * 86 * <p> The package names that are parameters or returned by methods defined in 87 * this class are the fully-qualified names of the packages as defined in 88 * section 6.5.3 of <cite>The Java™ Language Specification</cite>, for 89 * example, {@code "java.lang"}. </p> 90 * 91 * <p> Unless otherwise specified, passing a {@code null} argument to a method 92 * in this class causes a {@link NullPointerException NullPointerException} to 93 * be thrown. </p> 94 * 95 * @since 9 96 * @spec JPMS 97 * @see Class#getModule() 98 */ 99 100public final class Module implements AnnotatedElement { 101 102 // the layer that contains this module, can be null 103 private final ModuleLayer layer; 104 105 // module name and loader, these fields are read by VM 106 private final String name; 107 private final ClassLoader loader; 108 109 // the module descriptor 110 private final ModuleDescriptor descriptor; 111 112 113 /** 114 * Creates a new named Module. The resulting Module will be defined to the 115 * VM but will not read any other modules, will not have any exports setup 116 * and will not be registered in the service catalog. 117 */ 118 Module(ModuleLayer layer, 119 ClassLoader loader, 120 ModuleDescriptor descriptor, 121 URI uri) 122 { 123 this.layer = layer; 124 this.name = descriptor.name(); 125 this.loader = loader; 126 this.descriptor = descriptor; 127 128 // define module to VM 129 130 boolean isOpen = descriptor.isOpen() || descriptor.isAutomatic(); 131 Version version = descriptor.version().orElse(null); 132 String vs = Objects.toString(version, null); 133 String loc = Objects.toString(uri, null); 134 String[] packages = descriptor.packages().toArray(new String[0]); 135 defineModule0(this, isOpen, vs, loc, packages); 136 } 137 138 139 /** 140 * Create the unnamed Module for the given ClassLoader. 141 * 142 * @see ClassLoader#getUnnamedModule 143 */ 144 Module(ClassLoader loader) { 145 this.layer = null; 146 this.name = null; 147 this.loader = loader; 148 this.descriptor = null; 149 } 150 151 152 /** 153 * Creates a named module but without defining the module to the VM. 154 * 155 * @apiNote This constructor is for VM white-box testing. 156 */ 157 Module(ClassLoader loader, ModuleDescriptor descriptor) { 158 this.layer = null; 159 this.name = descriptor.name(); 160 this.loader = loader; 161 this.descriptor = descriptor; 162 } 163 164 165 166 /** 167 * Returns {@code true} if this module is a named module. 168 * 169 * @return {@code true} if this is a named module 170 * 171 * @see ClassLoader#getUnnamedModule() 172 */ 173 public boolean isNamed() { 174 return name != null; 175 } 176 177 /** 178 * Returns the module name or {@code null} if this module is an unnamed 179 * module. 180 * 181 * @return The module name 182 */ 183 public String getName() { 184 return name; 185 } 186 187 /** 188 * Returns the {@code ClassLoader} for this module. 189 * 190 * <p> If there is a security manager then its {@code checkPermission} 191 * method if first called with a {@code RuntimePermission("getClassLoader")} 192 * permission to check that the caller is allowed to get access to the 193 * class loader. </p> 194 * 195 * @return The class loader for this module 196 * 197 * @throws SecurityException 198 * If denied by the security manager 199 */ 200 public ClassLoader getClassLoader() { 201 SecurityManager sm = System.getSecurityManager(); 202 if (sm != null) { 203 sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION); 204 } 205 return loader; 206 } 207 208 /** 209 * Returns the module descriptor for this module or {@code null} if this 210 * module is an unnamed module. 211 * 212 * @return The module descriptor for this module 213 */ 214 public ModuleDescriptor getDescriptor() { 215 return descriptor; 216 } 217 218 /** 219 * Returns the module layer that contains this module or {@code null} if 220 * this module is not in a module layer. 221 * 222 * A module layer contains named modules and therefore this method always 223 * returns {@code null} when invoked on an unnamed module. 224 * 225 * <p> <a href="reflect/Proxy.html#dynamicmodule">Dynamic modules</a> are 226 * named modules that are generated at runtime. A dynamic module may or may 227 * not be in a module layer. </p> 228 * 229 * @return The module layer that contains this module 230 * 231 * @see java.lang.reflect.Proxy 232 */ 233 public ModuleLayer getLayer() { 234 if (isNamed()) { 235 ModuleLayer layer = this.layer; 236 if (layer != null) 237 return layer; 238 239 // special-case java.base as it is created before the boot layer 240 if (loader == null && name.equals("java.base")) { 241 return ModuleLayer.boot(); 242 } 243 } 244 return null; 245 } 246 247 248 // -- 249 250 // special Module to mean "all unnamed modules" 251 private static final Module ALL_UNNAMED_MODULE = new Module(null); 252 253 // special Module to mean "everyone" 254 private static final Module EVERYONE_MODULE = new Module(null); 255 256 // set contains EVERYONE_MODULE, used when a package is opened or 257 // exported unconditionally 258 private static final Set<Module> EVERYONE_SET = Set.of(EVERYONE_MODULE); 259 260 261 // -- readability -- 262 263 // the modules that this module reads 264 private volatile Set<Module> reads; 265 266 // additional module (2nd key) that some module (1st key) reflectively reads 267 private static final WeakPairMap<Module, Module, Boolean> reflectivelyReads 268 = new WeakPairMap<>(); 269 270 271 /** 272 * Indicates if this module reads the given module. This method returns 273 * {@code true} if invoked to test if this module reads itself. It also 274 * returns {@code true} if invoked on an unnamed module (as unnamed 275 * modules read all modules). 276 * 277 * @param other 278 * The other module 279 * 280 * @return {@code true} if this module reads {@code other} 281 * 282 * @see #addReads(Module) 283 */ 284 public boolean canRead(Module other) { 285 Objects.requireNonNull(other); 286 287 // an unnamed module reads all modules 288 if (!this.isNamed()) 289 return true; 290 291 // all modules read themselves 292 if (other == this) 293 return true; 294 295 // check if this module reads other 296 if (other.isNamed()) { 297 Set<Module> reads = this.reads; // volatile read 298 if (reads != null && reads.contains(other)) 299 return true; 300 } 301 302 // check if this module reads the other module reflectively 303 if (reflectivelyReads.containsKeyPair(this, other)) 304 return true; 305 306 // if other is an unnamed module then check if this module reads 307 // all unnamed modules 308 if (!other.isNamed() 309 && reflectivelyReads.containsKeyPair(this, ALL_UNNAMED_MODULE)) 310 return true; 311 312 return false; 313 } 314 315 /** 316 * If the caller's module is this module then update this module to read 317 * the given module. 318 * 319 * This method is a no-op if {@code other} is this module (all modules read 320 * themselves), this module is an unnamed module (as unnamed modules read 321 * all modules), or this module already reads {@code other}. 322 * 323 * @implNote <em>Read edges</em> added by this method are <em>weak</em> and 324 * do not prevent {@code other} from being GC'ed when this module is 325 * strongly reachable. 326 * 327 * @param other 328 * The other module 329 * 330 * @return this module 331 * 332 * @throws IllegalCallerException 333 * If this is a named module and the caller's module is not this 334 * module 335 * 336 * @see #canRead 337 */ 338 @CallerSensitive 339 public Module addReads(Module other) { 340 Objects.requireNonNull(other); 341 if (this.isNamed()) { 342 Module caller = getCallerModule(Reflection.getCallerClass()); 343 if (caller != this) { 344 throw new IllegalCallerException(caller + " != " + this); 345 } 346 implAddReads(other, true); 347 } 348 return this; 349 } 350 351 /** 352 * Updates this module to read another module. 353 * 354 * @apiNote Used by the --add-reads command line option. 355 */ 356 void implAddReads(Module other) { 357 implAddReads(other, true); 358 } 359 360 /** 361 * Updates this module to read all unnamed modules. 362 * 363 * @apiNote Used by the --add-reads command line option. 364 */ 365 void implAddReadsAllUnnamed() { 366 implAddReads(Module.ALL_UNNAMED_MODULE, true); 367 } 368 369 /** 370 * Updates this module to read another module without notifying the VM. 371 * 372 * @apiNote This method is for VM white-box testing. 373 */ 374 void implAddReadsNoSync(Module other) { 375 implAddReads(other, false); 376 } 377 378 /** 379 * Makes the given {@code Module} readable to this module. 380 * 381 * If {@code syncVM} is {@code true} then the VM is notified. 382 */ 383 private void implAddReads(Module other, boolean syncVM) { 384 Objects.requireNonNull(other); 385 if (!canRead(other)) { 386 // update VM first, just in case it fails 387 if (syncVM) { 388 if (other == ALL_UNNAMED_MODULE) { 389 addReads0(this, null); 390 } else { 391 addReads0(this, other); 392 } 393 } 394 395 // add reflective read 396 reflectivelyReads.putIfAbsent(this, other, Boolean.TRUE); 397 } 398 } 399 400 401 // -- exported and open packages -- 402 403 // the packages are open to other modules, can be null 404 // if the value contains EVERYONE_MODULE then the package is open to all 405 private volatile Map<String, Set<Module>> openPackages; 406 407 // the packages that are exported, can be null 408 // if the value contains EVERYONE_MODULE then the package is exported to all 409 private volatile Map<String, Set<Module>> exportedPackages; 410 411 // additional exports or opens added at run-time 412 // this module (1st key), other module (2nd key) 413 // (package name, open?) (value) 414 private static final WeakPairMap<Module, Module, Map<String, Boolean>> 415 reflectivelyExports = new WeakPairMap<>(); 416 417 418 /** 419 * Returns {@code true} if this module exports the given package to at 420 * least the given module. 421 * 422 * <p> This method returns {@code true} if invoked to test if a package in 423 * this module is exported to itself. It always returns {@code true} when 424 * invoked on an unnamed module. A package that is {@link #isOpen open} to 425 * the given module is considered exported to that module at run-time and 426 * so this method returns {@code true} if the package is open to the given 427 * module. </p> 428 * 429 * <p> This method does not check if the given module reads this module. </p> 430 * 431 * @param pn 432 * The package name 433 * @param other 434 * The other module 435 * 436 * @return {@code true} if this module exports the package to at least the 437 * given module 438 * 439 * @see ModuleDescriptor#exports() 440 * @see #addExports(String,Module) 441 */ 442 public boolean isExported(String pn, Module other) { 443 Objects.requireNonNull(pn); 444 Objects.requireNonNull(other); 445 return implIsExportedOrOpen(pn, other, /*open*/false); 446 } 447 448 /** 449 * Returns {@code true} if this module has <em>opened</em> a package to at 450 * least the given module. 451 * 452 * <p> This method returns {@code true} if invoked to test if a package in 453 * this module is open to itself. It returns {@code true} when invoked on an 454 * {@link ModuleDescriptor#isOpen open} module with a package in the module. 455 * It always returns {@code true} when invoked on an unnamed module. </p> 456 * 457 * <p> This method does not check if the given module reads this module. </p> 458 * 459 * @param pn 460 * The package name 461 * @param other 462 * The other module 463 * 464 * @return {@code true} if this module has <em>opened</em> the package 465 * to at least the given module 466 * 467 * @see ModuleDescriptor#opens() 468 * @see #addOpens(String,Module) 469 * @see AccessibleObject#setAccessible(boolean) 470 * @see java.lang.invoke.MethodHandles#privateLookupIn 471 */ 472 public boolean isOpen(String pn, Module other) { 473 Objects.requireNonNull(pn); 474 Objects.requireNonNull(other); 475 return implIsExportedOrOpen(pn, other, /*open*/true); 476 } 477 478 /** 479 * Returns {@code true} if this module exports the given package 480 * unconditionally. 481 * 482 * <p> This method always returns {@code true} when invoked on an unnamed 483 * module. A package that is {@link #isOpen(String) opened} unconditionally 484 * is considered exported unconditionally at run-time and so this method 485 * returns {@code true} if the package is opened unconditionally. </p> 486 * 487 * <p> This method does not check if the given module reads this module. </p> 488 * 489 * @param pn 490 * The package name 491 * 492 * @return {@code true} if this module exports the package unconditionally 493 * 494 * @see ModuleDescriptor#exports() 495 */ 496 public boolean isExported(String pn) { 497 Objects.requireNonNull(pn); 498 return implIsExportedOrOpen(pn, EVERYONE_MODULE, /*open*/false); 499 } 500 501 /** 502 * Returns {@code true} if this module has <em>opened</em> a package 503 * unconditionally. 504 * 505 * <p> This method always returns {@code true} when invoked on an unnamed 506 * module. Additionally, it always returns {@code true} when invoked on an 507 * {@link ModuleDescriptor#isOpen open} module with a package in the 508 * module. </p> 509 * 510 * <p> This method does not check if the given module reads this module. </p> 511 * 512 * @param pn 513 * The package name 514 * 515 * @return {@code true} if this module has <em>opened</em> the package 516 * unconditionally 517 * 518 * @see ModuleDescriptor#opens() 519 */ 520 public boolean isOpen(String pn) { 521 Objects.requireNonNull(pn); 522 return implIsExportedOrOpen(pn, EVERYONE_MODULE, /*open*/true); 523 } 524 525 526 /** 527 * Returns {@code true} if this module exports or opens the given package 528 * to the given module. If the other module is {@code EVERYONE_MODULE} then 529 * this method tests if the package is exported or opened unconditionally. 530 */ 531 private boolean implIsExportedOrOpen(String pn, Module other, boolean open) { 532 // all packages in unnamed modules are open 533 if (!isNamed()) 534 return true; 535 536 // all packages are exported/open to self 537 if (other == this && containsPackage(pn)) 538 return true; 539 540 // all packages in open and automatic modules are open 541 if (descriptor.isOpen() || descriptor.isAutomatic()) 542 return containsPackage(pn); 543 544 // exported/opened via module declaration/descriptor 545 if (isStaticallyExportedOrOpen(pn, other, open)) 546 return true; 547 548 // exported via addExports/addOpens 549 if (isReflectivelyExportedOrOpen(pn, other, open)) 550 return true; 551 552 // not exported or open to other 553 return false; 554 } 555 556 /** 557 * Returns {@code true} if this module exports or opens a package to 558 * the given module via its module declaration. 559 */ 560 private boolean isStaticallyExportedOrOpen(String pn, Module other, boolean open) { 561 // package is open to everyone or <other> 562 Map<String, Set<Module>> openPackages = this.openPackages; 563 if (openPackages != null) { 564 Set<Module> targets = openPackages.get(pn); 565 if (targets != null) { 566 if (targets.contains(EVERYONE_MODULE)) 567 return true; 568 if (other != EVERYONE_MODULE && targets.contains(other)) 569 return true; 570 } 571 } 572 573 if (!open) { 574 // package is exported to everyone or <other> 575 Map<String, Set<Module>> exportedPackages = this.exportedPackages; 576 if (exportedPackages != null) { 577 Set<Module> targets = exportedPackages.get(pn); 578 if (targets != null) { 579 if (targets.contains(EVERYONE_MODULE)) 580 return true; 581 if (other != EVERYONE_MODULE && targets.contains(other)) 582 return true; 583 } 584 } 585 } 586 587 return false; 588 } 589 590 591 /** 592 * Returns {@code true} if this module reflectively exports or opens given 593 * package package to the given module. 594 */ 595 private boolean isReflectivelyExportedOrOpen(String pn, Module other, boolean open) { 596 // exported or open to all modules 597 Map<String, Boolean> exports = reflectivelyExports.get(this, EVERYONE_MODULE); 598 if (exports != null) { 599 Boolean b = exports.get(pn); 600 if (b != null) { 601 boolean isOpen = b.booleanValue(); 602 if (!open || isOpen) return true; 603 } 604 } 605 606 if (other != EVERYONE_MODULE) { 607 608 // exported or open to other 609 exports = reflectivelyExports.get(this, other); 610 if (exports != null) { 611 Boolean b = exports.get(pn); 612 if (b != null) { 613 boolean isOpen = b.booleanValue(); 614 if (!open || isOpen) return true; 615 } 616 } 617 618 // other is an unnamed module && exported or open to all unnamed 619 if (!other.isNamed()) { 620 exports = reflectivelyExports.get(this, ALL_UNNAMED_MODULE); 621 if (exports != null) { 622 Boolean b = exports.get(pn); 623 if (b != null) { 624 boolean isOpen = b.booleanValue(); 625 if (!open || isOpen) return true; 626 } 627 } 628 } 629 630 } 631 632 return false; 633 } 634 635 636 /** 637 * If the caller's module is this module then update this module to export 638 * the given package to the given module. 639 * 640 * <p> This method has no effect if the package is already exported (or 641 * <em>open</em>) to the given module. </p> 642 * 643 * @apiNote As specified in section 5.4.3 of the <cite>The Java™ 644 * Virtual Machine Specification </cite>, if an attempt to resolve a 645 * symbolic reference fails because of a linkage error, then subsequent 646 * attempts to resolve the reference always fail with the same error that 647 * was thrown as a result of the initial resolution attempt. 648 * 649 * @param pn 650 * The package name 651 * @param other 652 * The module 653 * 654 * @return this module 655 * 656 * @throws IllegalArgumentException 657 * If {@code pn} is {@code null}, or this is a named module and the 658 * package {@code pn} is not a package in this module 659 * @throws IllegalCallerException 660 * If this is a named module and the caller's module is not this 661 * module 662 * 663 * @jvms 5.4.3 Resolution 664 * @see #isExported(String,Module) 665 */ 666 @CallerSensitive 667 public Module addExports(String pn, Module other) { 668 if (pn == null) 669 throw new IllegalArgumentException("package is null"); 670 Objects.requireNonNull(other); 671 672 if (isNamed()) { 673 Module caller = getCallerModule(Reflection.getCallerClass()); 674 if (caller != this) { 675 throw new IllegalCallerException(caller + " != " + this); 676 } 677 implAddExportsOrOpens(pn, other, /*open*/false, /*syncVM*/true); 678 } 679 680 return this; 681 } 682 683 /** 684 * If this module has <em>opened</em> a package to at least the caller 685 * module then update this module to open the package to the given module. 686 * Opening a package with this method allows all types in the package, 687 * and all their members, not just public types and their public members, 688 * to be reflected on by the given module when using APIs that support 689 * private access or a way to bypass or suppress default Java language 690 * access control checks. 691 * 692 * <p> This method has no effect if the package is already <em>open</em> 693 * to the given module. </p> 694 * 695 * @apiNote This method can be used for cases where a <em>consumer 696 * module</em> uses a qualified opens to open a package to an <em>API 697 * module</em> but where the reflective access to the members of classes in 698 * the consumer module is delegated to code in another module. Code in the 699 * API module can use this method to open the package in the consumer module 700 * to the other module. 701 * 702 * @param pn 703 * The package name 704 * @param other 705 * The module 706 * 707 * @return this module 708 * 709 * @throws IllegalArgumentException 710 * If {@code pn} is {@code null}, or this is a named module and the 711 * package {@code pn} is not a package in this module 712 * @throws IllegalCallerException 713 * If this is a named module and this module has not opened the 714 * package to at least the caller's module 715 * 716 * @see #isOpen(String,Module) 717 * @see AccessibleObject#setAccessible(boolean) 718 * @see java.lang.invoke.MethodHandles#privateLookupIn 719 */ 720 @CallerSensitive 721 public Module addOpens(String pn, Module other) { 722 if (pn == null) 723 throw new IllegalArgumentException("package is null"); 724 Objects.requireNonNull(other); 725 726 if (isNamed()) { 727 Module caller = getCallerModule(Reflection.getCallerClass()); 728 if (caller != this && (caller == null || !isOpen(pn, caller))) 729 throw new IllegalCallerException(pn + " is not open to " + caller); 730 implAddExportsOrOpens(pn, other, /*open*/true, /*syncVM*/true); 731 } 732 733 return this; 734 } 735 736 737 /** 738 * Updates this module to export a package unconditionally. 739 * 740 * @apiNote This method is for JDK tests only. 741 */ 742 void implAddExports(String pn) { 743 implAddExportsOrOpens(pn, Module.EVERYONE_MODULE, false, true); 744 } 745 746 /** 747 * Updates this module to export a package to another module. 748 * 749 * @apiNote Used by Instrumentation::redefineModule and --add-exports 750 */ 751 void implAddExports(String pn, Module other) { 752 implAddExportsOrOpens(pn, other, false, true); 753 } 754 755 /** 756 * Updates this module to export a package to all unnamed modules. 757 * 758 * @apiNote Used by the --add-exports command line option. 759 */ 760 void implAddExportsToAllUnnamed(String pn) { 761 implAddExportsOrOpens(pn, Module.ALL_UNNAMED_MODULE, false, true); 762 } 763 764 /** 765 * Updates this export to export a package unconditionally without 766 * notifying the VM. 767 * 768 * @apiNote This method is for VM white-box testing. 769 */ 770 void implAddExportsNoSync(String pn) { 771 implAddExportsOrOpens(pn.replace('/', '.'), Module.EVERYONE_MODULE, false, false); 772 } 773 774 /** 775 * Updates a module to export a package to another module without 776 * notifying the VM. 777 * 778 * @apiNote This method is for VM white-box testing. 779 */ 780 void implAddExportsNoSync(String pn, Module other) { 781 implAddExportsOrOpens(pn.replace('/', '.'), other, false, false); 782 } 783 784 /** 785 * Updates this module to open a package unconditionally. 786 * 787 * @apiNote This method is for JDK tests only. 788 */ 789 void implAddOpens(String pn) { 790 implAddExportsOrOpens(pn, Module.EVERYONE_MODULE, true, true); 791 } 792 793 /** 794 * Updates this module to open a package to another module. 795 * 796 * @apiNote Used by Instrumentation::redefineModule and --add-opens 797 */ 798 void implAddOpens(String pn, Module other) { 799 implAddExportsOrOpens(pn, other, true, true); 800 } 801 802 /** 803 * Updates this module to export a package to all unnamed modules. 804 * 805 * @apiNote Used by the --add-opens command line option. 806 */ 807 void implAddOpensToAllUnnamed(String pn) { 808 implAddExportsOrOpens(pn, Module.ALL_UNNAMED_MODULE, true, true); 809 } 810 811 812 /** 813 * Updates a module to export or open a module to another module. 814 * 815 * If {@code syncVM} is {@code true} then the VM is notified. 816 */ 817 private void implAddExportsOrOpens(String pn, 818 Module other, 819 boolean open, 820 boolean syncVM) { 821 Objects.requireNonNull(other); 822 Objects.requireNonNull(pn); 823 824 // all packages are open in unnamed, open, and automatic modules 825 if (!isNamed() || descriptor.isOpen() || descriptor.isAutomatic()) 826 return; 827 828 // nothing to do if already exported/open to other 829 if (implIsExportedOrOpen(pn, other, open)) 830 return; 831 832 // can only export a package in the module 833 if (!containsPackage(pn)) { 834 throw new IllegalArgumentException("package " + pn 835 + " not in contents"); 836 } 837 838 // update VM first, just in case it fails 839 if (syncVM) { 840 if (other == EVERYONE_MODULE) { 841 addExportsToAll0(this, pn); 842 } else if (other == ALL_UNNAMED_MODULE) { 843 addExportsToAllUnnamed0(this, pn); 844 } else { 845 addExports0(this, pn, other); 846 } 847 } 848 849 // add package name to reflectivelyExports if absent 850 Map<String, Boolean> map = reflectivelyExports 851 .computeIfAbsent(this, other, 852 (m1, m2) -> new ConcurrentHashMap<>()); 853 854 if (open) { 855 map.put(pn, Boolean.TRUE); // may need to promote from FALSE to TRUE 856 } else { 857 map.putIfAbsent(pn, Boolean.FALSE); 858 } 859 } 860 861 862 // -- services -- 863 864 // additional service type (2nd key) that some module (1st key) uses 865 private static final WeakPairMap<Module, Class<?>, Boolean> reflectivelyUses 866 = new WeakPairMap<>(); 867 868 /** 869 * If the caller's module is this module then update this module to add a 870 * service dependence on the given service type. This method is intended 871 * for use by frameworks that invoke {@link java.util.ServiceLoader 872 * ServiceLoader} on behalf of other modules or where the framework is 873 * passed a reference to the service type by other code. This method is 874 * a no-op when invoked on an unnamed module or an automatic module. 875 * 876 * <p> This method does not cause {@link Configuration#resolveAndBind 877 * resolveAndBind} to be re-run. </p> 878 * 879 * @param service 880 * The service type 881 * 882 * @return this module 883 * 884 * @throws IllegalCallerException 885 * If this is a named module and the caller's module is not this 886 * module 887 * 888 * @see #canUse(Class) 889 * @see ModuleDescriptor#uses() 890 */ 891 @CallerSensitive 892 public Module addUses(Class<?> service) { 893 Objects.requireNonNull(service); 894 895 if (isNamed() && !descriptor.isAutomatic()) { 896 Module caller = getCallerModule(Reflection.getCallerClass()); 897 if (caller != this) { 898 throw new IllegalCallerException(caller + " != " + this); 899 } 900 implAddUses(service); 901 } 902 903 return this; 904 } 905 906 /** 907 * Update this module to add a service dependence on the given service 908 * type. 909 */ 910 void implAddUses(Class<?> service) { 911 if (!canUse(service)) { 912 reflectivelyUses.putIfAbsent(this, service, Boolean.TRUE); 913 } 914 } 915 916 917 /** 918 * Indicates if this module has a service dependence on the given service 919 * type. This method always returns {@code true} when invoked on an unnamed 920 * module or an automatic module. 921 * 922 * @param service 923 * The service type 924 * 925 * @return {@code true} if this module uses service type {@code st} 926 * 927 * @see #addUses(Class) 928 */ 929 public boolean canUse(Class<?> service) { 930 Objects.requireNonNull(service); 931 932 if (!isNamed()) 933 return true; 934 935 if (descriptor.isAutomatic()) 936 return true; 937 938 // uses was declared 939 if (descriptor.uses().contains(service.getName())) 940 return true; 941 942 // uses added via addUses 943 return reflectivelyUses.containsKeyPair(this, service); 944 } 945 946 947 948 // -- packages -- 949 950 // Additional packages that are added to the module at run-time. 951 private volatile Map<String, Boolean> extraPackages; 952 953 private boolean containsPackage(String pn) { 954 if (descriptor.packages().contains(pn)) 955 return true; 956 Map<String, Boolean> extraPackages = this.extraPackages; 957 if (extraPackages != null && extraPackages.containsKey(pn)) 958 return true; 959 return false; 960 } 961 962 963 /** 964 * Returns the set of package names for the packages in this module. 965 * 966 * <p> For named modules, the returned set contains an element for each 967 * package in the module. </p> 968 * 969 * <p> For unnamed modules, this method is the equivalent to invoking the 970 * {@link ClassLoader#getDefinedPackages() getDefinedPackages} method of 971 * this module's class loader and returning the set of package names. </p> 972 * 973 * @return the set of the package names of the packages in this module 974 */ 975 public Set<String> getPackages() { 976 if (isNamed()) { 977 978 Set<String> packages = descriptor.packages(); 979 Map<String, Boolean> extraPackages = this.extraPackages; 980 if (extraPackages == null) { 981 return packages; 982 } else { 983 return Stream.concat(packages.stream(), 984 extraPackages.keySet().stream()) 985 .collect(Collectors.toSet()); 986 } 987 988 } else { 989 // unnamed module 990 Stream<Package> packages; 991 if (loader == null) { 992 packages = BootLoader.packages(); 993 } else { 994 packages = SharedSecrets.getJavaLangAccess().packages(loader); 995 } 996 return packages.map(Package::getName).collect(Collectors.toSet()); 997 } 998 } 999 1000 /** 1001 * Add a package to this module without notifying the VM. 1002 * 1003 * @apiNote This method is VM white-box testing. 1004 */ 1005 void implAddPackageNoSync(String pn) { 1006 implAddPackage(pn.replace('/', '.'), false); 1007 } 1008 1009 /** 1010 * Add a package to this module. 1011 * 1012 * If {@code syncVM} is {@code true} then the VM is notified. This method is 1013 * a no-op if this is an unnamed module or the module already contains the 1014 * package. 1015 * 1016 * @throws IllegalArgumentException if the package name is not legal 1017 * @throws IllegalStateException if the package is defined to another module 1018 */ 1019 private void implAddPackage(String pn, boolean syncVM) { 1020 // no-op if unnamed module 1021 if (!isNamed()) 1022 return; 1023 1024 // no-op if module contains the package 1025 if (containsPackage(pn)) 1026 return; 1027 1028 // check package name is legal for named modules 1029 if (pn.isEmpty()) 1030 throw new IllegalArgumentException("Cannot add <unnamed> package"); 1031 for (int i=0; i<pn.length(); i++) { 1032 char c = pn.charAt(i); 1033 if (c == '/' || c == ';' || c == '[') { 1034 throw new IllegalArgumentException("Illegal character: " + c); 1035 } 1036 } 1037 1038 // create extraPackages if needed 1039 Map<String, Boolean> extraPackages = this.extraPackages; 1040 if (extraPackages == null) { 1041 synchronized (this) { 1042 extraPackages = this.extraPackages; 1043 if (extraPackages == null) 1044 this.extraPackages = extraPackages = new ConcurrentHashMap<>(); 1045 } 1046 } 1047 1048 // update VM first in case it fails. This is a no-op if another thread 1049 // beats us to add the package first 1050 if (syncVM) { 1051 // throws IllegalStateException if defined to another module 1052 addPackage0(this, pn); 1053 } 1054 extraPackages.putIfAbsent(pn, Boolean.TRUE); 1055 } 1056 1057 1058 // -- creating Module objects -- 1059 1060 /** 1061 * Defines all module in a configuration to the runtime. 1062 * 1063 * @return a map of module name to runtime {@code Module} 1064 * 1065 * @throws IllegalArgumentException 1066 * If defining any of the modules to the VM fails 1067 */ 1068 static Map<String, Module> defineModules(Configuration cf, 1069 Function<String, ClassLoader> clf, 1070 ModuleLayer layer) 1071 { 1072 Map<String, Module> nameToModule = new HashMap<>(); 1073 Map<String, ClassLoader> moduleToLoader = new HashMap<>(); 1074 1075 boolean isBootLayer = (ModuleLayer.boot() == null); 1076 Set<ClassLoader> loaders = new HashSet<>(); 1077 1078 // map each module to a class loader 1079 for (ResolvedModule resolvedModule : cf.modules()) { 1080 String name = resolvedModule.name(); 1081 ClassLoader loader = clf.apply(name); 1082 if (loader != null) { 1083 moduleToLoader.put(name, loader); 1084 loaders.add(loader); 1085 } else if (!(clf instanceof ModuleLoaderMap.Mapper)) { 1086 throw new IllegalArgumentException("loader can't be 'null'"); 1087 } 1088 } 1089 1090 // define each module in the configuration to the VM 1091 for (ResolvedModule resolvedModule : cf.modules()) { 1092 ModuleReference mref = resolvedModule.reference(); 1093 ModuleDescriptor descriptor = mref.descriptor(); 1094 String name = descriptor.name(); 1095 URI uri = mref.location().orElse(null); 1096 ClassLoader loader = moduleToLoader.get(resolvedModule.name()); 1097 Module m; 1098 if (loader == null && isBootLayer && name.equals("java.base")) { 1099 // java.base is already defined to the VM 1100 m = Object.class.getModule(); 1101 } else { 1102 m = new Module(layer, loader, descriptor, uri); 1103 } 1104 nameToModule.put(name, m); 1105 moduleToLoader.put(name, loader); 1106 } 1107 1108 // setup readability and exports 1109 for (ResolvedModule resolvedModule : cf.modules()) { 1110 ModuleReference mref = resolvedModule.reference(); 1111 ModuleDescriptor descriptor = mref.descriptor(); 1112 1113 String mn = descriptor.name(); 1114 Module m = nameToModule.get(mn); 1115 assert m != null; 1116 1117 // reads 1118 Set<Module> reads = new HashSet<>(); 1119 1120 // name -> source Module when in parent layer 1121 Map<String, Module> nameToSource = Collections.emptyMap(); 1122 1123 for (ResolvedModule other : resolvedModule.reads()) { 1124 Module m2 = null; 1125 if (other.configuration() == cf) { 1126 // this configuration 1127 m2 = nameToModule.get(other.name()); 1128 assert m2 != null; 1129 } else { 1130 // parent layer 1131 for (ModuleLayer parent: layer.parents()) { 1132 m2 = findModule(parent, other); 1133 if (m2 != null) 1134 break; 1135 } 1136 assert m2 != null; 1137 if (nameToSource.isEmpty()) 1138 nameToSource = new HashMap<>(); 1139 nameToSource.put(other.name(), m2); 1140 } 1141 reads.add(m2); 1142 1143 // update VM view 1144 addReads0(m, m2); 1145 } 1146 m.reads = reads; 1147 1148 // automatic modules read all unnamed modules 1149 if (descriptor.isAutomatic()) { 1150 m.implAddReads(ALL_UNNAMED_MODULE, true); 1151 } 1152 1153 // export and open packages, skipped for open and automatic 1154 // modules since they are treated as if all packages are open 1155 if (!descriptor.isOpen() && !descriptor.isAutomatic()) { 1156 initExportsAndOpens(m, nameToSource, nameToModule, layer.parents()); 1157 } 1158 } 1159 1160 // register the modules in the boot layer 1161 if (isBootLayer) { 1162 for (ResolvedModule resolvedModule : cf.modules()) { 1163 ModuleReference mref = resolvedModule.reference(); 1164 ModuleDescriptor descriptor = mref.descriptor(); 1165 if (!descriptor.provides().isEmpty()) { 1166 String name = descriptor.name(); 1167 Module m = nameToModule.get(name); 1168 ClassLoader loader = moduleToLoader.get(name); 1169 ServicesCatalog catalog; 1170 if (loader == null) { 1171 catalog = BootLoader.getServicesCatalog(); 1172 } else { 1173 catalog = ServicesCatalog.getServicesCatalog(loader); 1174 } 1175 catalog.register(m); 1176 } 1177 } 1178 } 1179 1180 // record that there is a layer with modules defined to the class loader 1181 for (ClassLoader loader : loaders) { 1182 layer.bindToLoader(loader); 1183 } 1184 1185 return nameToModule; 1186 } 1187 1188 1189 /** 1190 * Find the runtime Module corresponding to the given ResolvedModule 1191 * in the given parent layer (or its parents). 1192 */ 1193 private static Module findModule(ModuleLayer parent, 1194 ResolvedModule resolvedModule) { 1195 Configuration cf = resolvedModule.configuration(); 1196 String dn = resolvedModule.name(); 1197 return parent.layers() 1198 .filter(l -> l.configuration() == cf) 1199 .findAny() 1200 .map(layer -> { 1201 Optional<Module> om = layer.findModule(dn); 1202 assert om.isPresent() : dn + " not found in layer"; 1203 Module m = om.get(); 1204 assert m.getLayer() == layer : m + " not in expected layer"; 1205 return m; 1206 }) 1207 .orElse(null); 1208 } 1209 1210 1211 /** 1212 * Initialize the maps of exported and open packages for module m. 1213 */ 1214 private static void initExportsAndOpens(Module m, 1215 Map<String, Module> nameToSource, 1216 Map<String, Module> nameToModule, 1217 List<ModuleLayer> parents) { 1218 Map<String, Set<Module>> openPackages = new HashMap<>(); 1219 Map<String, Set<Module>> exportedPackages = new HashMap<>(); 1220 1221 // process the open packages first 1222 ModuleDescriptor descriptor = m.getDescriptor(); 1223 for (Opens opens : descriptor.opens()) { 1224 String source = opens.source(); 1225 1226 if (opens.isQualified()) { 1227 // qualified opens 1228 Set<Module> targets = new HashSet<>(); 1229 for (String target : opens.targets()) { 1230 Module m2 = findModule(target, nameToSource, nameToModule, parents); 1231 if (m2 != null) { 1232 addExports0(m, source, m2); 1233 targets.add(m2); 1234 } 1235 } 1236 if (!targets.isEmpty()) { 1237 openPackages.put(source, targets); 1238 } 1239 } else { 1240 // unqualified opens 1241 addExportsToAll0(m, source); 1242 openPackages.put(source, EVERYONE_SET); 1243 } 1244 } 1245 1246 // next the exports, skipping exports when the package is open 1247 for (Exports exports : descriptor.exports()) { 1248 String source = exports.source(); 1249 1250 // skip export if package is already open to everyone 1251 Set<Module> openToTargets = openPackages.get(source); 1252 if (openToTargets != null && openToTargets.contains(EVERYONE_MODULE)) 1253 continue; 1254 1255 if (exports.isQualified()) { 1256 // qualified exports 1257 Set<Module> targets = new HashSet<>(); 1258 for (String target : exports.targets()) { 1259 Module m2 = findModule(target, nameToSource, nameToModule, parents); 1260 if (m2 != null) { 1261 // skip qualified export if already open to m2 1262 if (openToTargets == null || !openToTargets.contains(m2)) { 1263 addExports0(m, source, m2); 1264 targets.add(m2); 1265 } 1266 } 1267 } 1268 if (!targets.isEmpty()) { 1269 exportedPackages.put(source, targets); 1270 } 1271 1272 } else { 1273 // unqualified exports 1274 addExportsToAll0(m, source); 1275 exportedPackages.put(source, EVERYONE_SET); 1276 } 1277 } 1278 1279 if (!openPackages.isEmpty()) 1280 m.openPackages = openPackages; 1281 if (!exportedPackages.isEmpty()) 1282 m.exportedPackages = exportedPackages; 1283 } 1284 1285 /** 1286 * Find the runtime Module with the given name. The module name is the 1287 * name of a target module in a qualified exports or opens directive. 1288 * 1289 * @param target The target module to find 1290 * @param nameToSource The modules in parent layers that are read 1291 * @param nameToModule The modules in the layer under construction 1292 * @param parents The parent layers 1293 */ 1294 private static Module findModule(String target, 1295 Map<String, Module> nameToSource, 1296 Map<String, Module> nameToModule, 1297 List<ModuleLayer> parents) { 1298 Module m = nameToSource.get(target); 1299 if (m == null) { 1300 m = nameToModule.get(target); 1301 if (m == null) { 1302 for (ModuleLayer parent : parents) { 1303 m = parent.findModule(target).orElse(null); 1304 if (m != null) break; 1305 } 1306 } 1307 } 1308 return m; 1309 } 1310 1311 1312 // -- annotations -- 1313 1314 /** 1315 * {@inheritDoc} 1316 * This method returns {@code null} when invoked on an unnamed module. 1317 */ 1318 @Override 1319 public <T extends Annotation> T getAnnotation(Class<T> annotationClass) { 1320 return moduleInfoClass().getDeclaredAnnotation(annotationClass); 1321 } 1322 1323 /** 1324 * {@inheritDoc} 1325 * This method returns an empty array when invoked on an unnamed module. 1326 */ 1327 @Override 1328 public Annotation[] getAnnotations() { 1329 return moduleInfoClass().getAnnotations(); 1330 } 1331 1332 /** 1333 * {@inheritDoc} 1334 * This method returns an empty array when invoked on an unnamed module. 1335 */ 1336 @Override 1337 public Annotation[] getDeclaredAnnotations() { 1338 return moduleInfoClass().getDeclaredAnnotations(); 1339 } 1340 1341 // cached class file with annotations 1342 private volatile Class<?> moduleInfoClass; 1343 1344 private Class<?> moduleInfoClass() { 1345 Class<?> clazz = this.moduleInfoClass; 1346 if (clazz != null) 1347 return clazz; 1348 1349 synchronized (this) { 1350 clazz = this.moduleInfoClass; 1351 if (clazz == null) { 1352 if (isNamed()) { 1353 PrivilegedAction<Class<?>> pa = this::loadModuleInfoClass; 1354 clazz = AccessController.doPrivileged(pa); 1355 } 1356 if (clazz == null) { 1357 class DummyModuleInfo { } 1358 clazz = DummyModuleInfo.class; 1359 } 1360 this.moduleInfoClass = clazz; 1361 } 1362 return clazz; 1363 } 1364 } 1365 1366 private Class<?> loadModuleInfoClass() { 1367 Class<?> clazz = null; 1368 try (InputStream in = getResourceAsStream("module-info.class")) { 1369 if (in != null) 1370 clazz = loadModuleInfoClass(in); 1371 } catch (Exception ignore) { } 1372 return clazz; 1373 } 1374 1375 /** 1376 * Loads module-info.class as a package-private interface in a class loader 1377 * that is a child of this module's class loader. 1378 */ 1379 private Class<?> loadModuleInfoClass(InputStream in) throws IOException { 1380 final String MODULE_INFO = "module-info"; 1381 1382 ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS 1383 + ClassWriter.COMPUTE_FRAMES); 1384 1385 ClassVisitor cv = new ClassVisitor(Opcodes.ASM5, cw) { 1386 @Override 1387 public void visit(int version, 1388 int access, 1389 String name, 1390 String signature, 1391 String superName, 1392 String[] interfaces) { 1393 cw.visit(version, 1394 Opcodes.ACC_INTERFACE 1395 + Opcodes.ACC_ABSTRACT 1396 + Opcodes.ACC_SYNTHETIC, 1397 MODULE_INFO, 1398 null, 1399 "java/lang/Object", 1400 null); 1401 } 1402 @Override 1403 public AnnotationVisitor visitAnnotation(String desc, boolean visible) { 1404 // keep annotations 1405 return super.visitAnnotation(desc, visible); 1406 } 1407 @Override 1408 public void visitAttribute(Attribute attr) { 1409 // drop non-annotation attributes 1410 } 1411 }; 1412 1413 ClassReader cr = new ClassReader(in); 1414 cr.accept(cv, 0); 1415 byte[] bytes = cw.toByteArray(); 1416 1417 ClassLoader cl = new ClassLoader(loader) { 1418 @Override 1419 protected Class<?> findClass(String cn)throws ClassNotFoundException { 1420 if (cn.equals(MODULE_INFO)) { 1421 return super.defineClass(cn, bytes, 0, bytes.length); 1422 } else { 1423 throw new ClassNotFoundException(cn); 1424 } 1425 } 1426 }; 1427 1428 try { 1429 return cl.loadClass(MODULE_INFO); 1430 } catch (ClassNotFoundException e) { 1431 throw new InternalError(e); 1432 } 1433 } 1434 1435 1436 // -- misc -- 1437 1438 1439 /** 1440 * Returns an input stream for reading a resource in this module. 1441 * The {@code name} parameter is a {@code '/'}-separated path name that 1442 * identifies the resource. As with {@link Class#getResourceAsStream 1443 * Class.getResourceAsStream}, this method delegates to the module's class 1444 * loader {@link ClassLoader#findResource(String,String) 1445 * findResource(String,String)} method, invoking it with the module name 1446 * (or {@code null} when the module is unnamed) and the name of the 1447 * resource. If the resource name has a leading slash then it is dropped 1448 * before delegation. 1449 * 1450 * <p> A resource in a named module may be <em>encapsulated</em> so that 1451 * it cannot be located by code in other modules. Whether a resource can be 1452 * located or not is determined as follows: </p> 1453 * 1454 * <ul> 1455 * <li> If the resource name ends with "{@code .class}" then it is not 1456 * encapsulated. </li> 1457 * 1458 * <li> A <em>package name</em> is derived from the resource name. If 1459 * the package name is a {@linkplain #getPackages() package} in the 1460 * module then the resource can only be located by the caller of this 1461 * method when the package is {@linkplain #isOpen(String,Module) open} 1462 * to at least the caller's module. If the resource is not in a 1463 * package in the module then the resource is not encapsulated. </li> 1464 * </ul> 1465 * 1466 * <p> In the above, the <em>package name</em> for a resource is derived 1467 * from the subsequence of characters that precedes the last {@code '/'} in 1468 * the name and then replacing each {@code '/'} character in the subsequence 1469 * with {@code '.'}. A leading slash is ignored when deriving the package 1470 * name. As an example, the package name derived for a resource named 1471 * "{@code a/b/c/foo.properties}" is "{@code a.b.c}". A resource name 1472 * with the name "{@code META-INF/MANIFEST.MF}" is never encapsulated 1473 * because "{@code META-INF}" is not a legal package name. </p> 1474 * 1475 * <p> This method returns {@code null} if the resource is not in this 1476 * module, the resource is encapsulated and cannot be located by the caller, 1477 * or access to the resource is denied by the security manager. </p> 1478 * 1479 * @param name 1480 * The resource name 1481 * 1482 * @return An input stream for reading the resource or {@code null} 1483 * 1484 * @throws IOException 1485 * If an I/O error occurs 1486 * 1487 * @see Class#getResourceAsStream(String) 1488 */ 1489 @CallerSensitive 1490 public InputStream getResourceAsStream(String name) throws IOException { 1491 if (name.startsWith("/")) { 1492 name = name.substring(1); 1493 } 1494 1495 if (isNamed() && Resources.canEncapsulate(name)) { 1496 Module caller = getCallerModule(Reflection.getCallerClass()); 1497 if (caller != this && caller != Object.class.getModule()) { 1498 String pn = Resources.toPackageName(name); 1499 if (getPackages().contains(pn)) { 1500 if (caller == null && !isOpen(pn)) { 1501 // no caller, package not open 1502 return null; 1503 } 1504 if (!isOpen(pn, caller)) { 1505 // package not open to caller 1506 return null; 1507 } 1508 } 1509 } 1510 } 1511 1512 String mn = this.name; 1513 1514 // special-case built-in class loaders to avoid URL connection 1515 if (loader == null) { 1516 return BootLoader.findResourceAsStream(mn, name); 1517 } else if (loader instanceof BuiltinClassLoader) { 1518 return ((BuiltinClassLoader) loader).findResourceAsStream(mn, name); 1519 } 1520 1521 // locate resource in module 1522 URL url = loader.findResource(mn, name); 1523 if (url != null) { 1524 try { 1525 return url.openStream(); 1526 } catch (SecurityException e) { } 1527 } 1528 1529 return null; 1530 } 1531 1532 /** 1533 * Returns the string representation of this module. For a named module, 1534 * the representation is the string {@code "module"}, followed by a space, 1535 * and then the module name. For an unnamed module, the representation is 1536 * the string {@code "unnamed module"}, followed by a space, and then an 1537 * implementation specific string that identifies the unnamed module. 1538 * 1539 * @return The string representation of this module 1540 */ 1541 @Override 1542 public String toString() { 1543 if (isNamed()) { 1544 return "module " + name; 1545 } else { 1546 String id = Integer.toHexString(System.identityHashCode(this)); 1547 return "unnamed module @" + id; 1548 } 1549 } 1550 1551 /** 1552 * Returns the module that a given caller class is a member of. Returns 1553 * {@code null} if the caller is {@code null}. 1554 */ 1555 private Module getCallerModule(Class<?> caller) { 1556 return (caller != null) ? caller.getModule() : null; 1557 } 1558 1559 1560 // -- native methods -- 1561 1562 // JVM_DefineModule 1563 private static native void defineModule0(Module module, 1564 boolean isOpen, 1565 String version, 1566 String location, 1567 String[] pns); 1568 1569 // JVM_AddReadsModule 1570 private static native void addReads0(Module from, Module to); 1571 1572 // JVM_AddModuleExports 1573 private static native void addExports0(Module from, String pn, Module to); 1574 1575 // JVM_AddModuleExportsToAll 1576 private static native void addExportsToAll0(Module from, String pn); 1577 1578 // JVM_AddModuleExportsToAllUnnamed 1579 private static native void addExportsToAllUnnamed0(Module from, String pn); 1580 1581 // JVM_AddModulePackage 1582 private static native void addPackage0(Module m, String pn); 1583} 1584