408 * @param name The <a href="ClassLoader.html#name">binary name</a>
409 * of the class
410 * @return {@code Class} object of the given name defined in the given module;
411 * {@code null} if not found.
412 *
413 * @throws NullPointerException if the given module or name is {@code null}
414 *
415 * @throws LinkageError if the linkage fails
416 *
417 * @throws SecurityException
418 * <ul>
419 * <li> if the caller is not the specified module and
420 * {@code RuntimePermission("getClassLoader")} permission is denied; or</li>
421 * <li> access to the module content is denied. For example,
422 * permission check will be performed when a class loader calls
423 * {@link ModuleReader#open(String)} to read the bytes of a class file
424 * in a module.</li>
425 * </ul>
426 *
427 * @since 9
428 */
429 @CallerSensitive
430 public static Class<?> forName(Module module, String name) {
431 Objects.requireNonNull(module);
432 Objects.requireNonNull(name);
433
434 Class<?> caller = Reflection.getCallerClass();
435 if (caller != null && caller.getModule() != module) {
436 // if caller is null, Class.forName is the last java frame on the stack.
437 // java.base has all permissions
438 SecurityManager sm = System.getSecurityManager();
439 if (sm != null) {
440 sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
441 }
442 }
443
444 PrivilegedAction<ClassLoader> pa = module::getClassLoader;
445 ClassLoader cl = AccessController.doPrivileged(pa);
446 if (cl != null) {
447 return cl.loadClass(module, name);
802 }
803
804 // Package-private to allow ClassLoader access
805 ClassLoader getClassLoader0() { return classLoader; }
806
807 /**
808 * Returns the module that this class or interface is a member of.
809 *
810 * If this class represents an array type then this method returns the
811 * {@code Module} for the element type. If this class represents a
812 * primitive type or void, then the {@code Module} object for the
813 * {@code java.base} module is returned.
814 *
815 * If this class is in an unnamed module then the {@link
816 * ClassLoader#getUnnamedModule() unnamed} {@code Module} of the class
817 * loader for this class is returned.
818 *
819 * @return the module that this class or interface is a member of
820 *
821 * @since 9
822 */
823 public Module getModule() {
824 return module;
825 }
826
827 // set by VM
828 private transient Module module;
829
830 // Initialized in JVM not by private constructor
831 // This field is filtered from reflection access, i.e. getDeclaredField
832 // will throw NoSuchFieldException
833 private final ClassLoader classLoader;
834
835 /**
836 * Returns an array of {@code TypeVariable} objects that represent the
837 * type variables declared by the generic declaration represented by this
838 * {@code GenericDeclaration} object, in declaration order. Returns an
839 * array of length 0 if the underlying generic declaration declares no type
840 * variables.
841 *
907 return getSuperclass();
908 }
909
910 // Historical irregularity:
911 // Generic signature marks interfaces with superclass = Object
912 // but this API returns null for interfaces
913 if (isInterface()) {
914 return null;
915 }
916
917 return info.getSuperclass();
918 }
919
920 /**
921 * Gets the package of this class.
922 *
923 * <p>If this class represents an array type, a primitive type or void,
924 * this method returns {@code null}.
925 *
926 * @return the package of this class.
927 */
928 public Package getPackage() {
929 if (isPrimitive() || isArray()) {
930 return null;
931 }
932 ClassLoader cl = getClassLoader0();
933 return cl != null ? cl.definePackage(this)
934 : BootLoader.definePackage(this);
935 }
936
937 /**
938 * Returns the fully qualified package name.
939 *
940 * <p> If this class is a top level class, then this method returns the fully
941 * qualified name of the package that the class is a member of, or the
942 * empty string if the class is in an unnamed package.
943 *
944 * <p> If this class is a member class, then this method is equivalent to
945 * invoking {@code getPackageName()} on the {@link #getEnclosingClass
946 * enclosing class}.
947 *
948 * <p> If this class is a {@link #isLocalClass local class} or an {@link
949 * #isAnonymousClass() anonymous class}, then this method is equivalent to
950 * invoking {@code getPackageName()} on the {@link #getDeclaringClass
951 * declaring class} of the {@link #getEnclosingMethod enclosing method} or
952 * {@link #getEnclosingConstructor enclosing constructor}.
953 *
954 * <p> This method returns {@code null} if this class represents an array type,
955 * a primitive type or void.
956 *
957 * @return the fully qualified package name
958 *
959 * @since 9
960 * @jls 6.7 Fully Qualified Names
961 */
962 public String getPackageName() {
963 String pn = this.packageName;
964 if (pn == null && !isArray() && !isPrimitive()) {
965 String cn = getName();
966 int dot = cn.lastIndexOf('.');
967 pn = (dot != -1) ? cn.substring(0, dot).intern() : "";
968 this.packageName = pn;
969 }
970 return pn;
971 }
972
973 // cached package name
974 private transient String packageName;
975
976 /**
977 * Returns the interfaces directly implemented by the class or interface
978 * represented by this object.
979 *
980 * <p>If this object represents a class, the return value is an array
981 * containing objects representing all interfaces directly implemented by
982 * the class. The order of the interface objects in the array corresponds
983 * to the order of the interface names in the {@code implements} clause of
984 * the declaration of the class represented by this object. For example,
985 * given the declaration:
986 * <blockquote>
987 * {@code class Shimmer implements FloorWax, DessertTopping { ... }}
2474 *
2475 * @since 1.1
2476 */
2477 @CallerSensitive
2478 public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
2479 throws NoSuchMethodException, SecurityException
2480 {
2481 SecurityManager sm = System.getSecurityManager();
2482 if (sm != null) {
2483 checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
2484 }
2485
2486 return getReflectionFactory().copyConstructor(
2487 getConstructor0(parameterTypes, Member.DECLARED));
2488 }
2489
2490 /**
2491 * Finds a resource with a given name.
2492 *
2493 * <p> If this class is in a named {@link Module Module} then this method
2494 * will attempt to find the resource in the module by means of the absolute
2495 * resource name, subject to the rules for encapsulation specified in the
2496 * {@code Module} {@link Module#getResourceAsStream getResourceAsStream}
2497 * method.
2498 *
2499 * <p> Otherwise, if this class is not in a named module then the rules for
2500 * searching resources associated with a given class are implemented by the
2501 * defining {@linkplain ClassLoader class loader} of the class. This method
2502 * delegates to this object's class loader. If this object was loaded by
2503 * the bootstrap class loader, the method delegates to {@link
2504 * ClassLoader#getSystemResourceAsStream}.
2505 *
2506 * <p> Before finding a resource in the caller's module or delegation to a
2507 * class loader, an absolute resource name is constructed from the given
2508 * resource name using this algorithm:
2509 *
2510 * <ul>
2511 *
2512 * <li> If the {@code name} begins with a {@code '/'}
2513 * (<code>'\u002f'</code>), then the absolute name of the resource is the
2514 * portion of the {@code name} following the {@code '/'}.
2515 *
2516 * <li> Otherwise, the absolute name is of the following form:
2517 *
2518 * <blockquote>
2519 * {@code modified_package_name/name}
2520 * </blockquote>
2521 *
2522 * <p> Where the {@code modified_package_name} is the package name of this
2523 * object with {@code '/'} substituted for {@code '.'}
2524 * (<code>'\u002e'</code>).
2525 *
2526 * </ul>
2527 *
2528 * @param name name of the desired resource
2529 * @return A {@link java.io.InputStream} object; {@code null} if no
2530 * resource with this name is found, the resource is in a package
2531 * that is not {@link Module#isOpen(String, Module) open} to at
2532 * least the caller module, or access to the resource is denied
2533 * by the security manager.
2534 * @throws NullPointerException If {@code name} is {@code null}
2535 * @since 1.1
2536 */
2537 @CallerSensitive
2538 public InputStream getResourceAsStream(String name) {
2539 name = resolveName(name);
2540
2541 Module module = getModule();
2542 if (module.isNamed()) {
2543 if (!ResourceHelper.isSimpleResource(name)) {
2544 Module caller = Reflection.getCallerClass().getModule();
2545 if (caller != module) {
2546 Set<String> packages = module.getDescriptor().packages();
2547 String pn = ResourceHelper.getPackageName(name);
2548 if (packages.contains(pn) && !module.isOpen(pn, caller)) {
2549 // resource is in package not open to caller
2550 return null;
2551 }
2552 }
2553 }
2554
2555 String mn = module.getName();
2568 }
2569
2570 } catch (IOException | SecurityException e) {
2571 return null;
2572 }
2573 }
2574
2575 // unnamed module
2576 ClassLoader cl = getClassLoader0();
2577 if (cl == null) {
2578 return ClassLoader.getSystemResourceAsStream(name);
2579 } else {
2580 return cl.getResourceAsStream(name);
2581 }
2582 }
2583
2584 /**
2585 * Finds a resource with a given name.
2586 *
2587 * <p> If this class is in a named {@link Module Module} then this method
2588 * will attempt to find the resource in the module by means of the absolute
2589 * resource name, subject to the rules for encapsulation specified in the
2590 * {@code Module} {@link Module#getResourceAsStream getResourceAsStream}
2591 * method.
2592 *
2593 * <p> Otherwise, if this class is not in a named module then the rules for
2594 * searching resources associated with a given class are implemented by the
2595 * defining {@linkplain ClassLoader class loader} of the class. This method
2596 * delegates to this object's class loader. If this object was loaded by
2597 * the bootstrap class loader, the method delegates to {@link
2598 * ClassLoader#getSystemResource}.
2599 *
2600 * <p> Before delegation, an absolute resource name is constructed from the
2601 * given resource name using this algorithm:
2602 *
2603 * <ul>
2604 *
2605 * <li> If the {@code name} begins with a {@code '/'}
2606 * (<code>'\u002f'</code>), then the absolute name of the resource is the
2607 * portion of the {@code name} following the {@code '/'}.
2608 *
2609 * <li> Otherwise, the absolute name is of the following form:
2610 *
2611 * <blockquote>
2612 * {@code modified_package_name/name}
2613 * </blockquote>
2614 *
2615 * <p> Where the {@code modified_package_name} is the package name of this
2616 * object with {@code '/'} substituted for {@code '.'}
2617 * (<code>'\u002e'</code>).
2618 *
2619 * </ul>
2620 *
2621 * @param name name of the desired resource
2622 * @return A {@link java.net.URL} object; {@code null} if no resource with
2623 * this name is found, the resource cannot be located by a URL, the
2624 * resource is in a package that is not
2625 * {@link Module#isOpen(String, Module) open} to at least the caller
2626 * module, or access to the resource is denied by the security
2627 * manager.
2628 * @throws NullPointerException If {@code name} is {@code null}
2629 * @since 1.1
2630 */
2631 @CallerSensitive
2632 public URL getResource(String name) {
2633 name = resolveName(name);
2634
2635 Module module = getModule();
2636 if (module.isNamed()) {
2637 if (!ResourceHelper.isSimpleResource(name)) {
2638 Module caller = Reflection.getCallerClass().getModule();
2639 if (caller != module) {
2640 Set<String> packages = module.getDescriptor().packages();
2641 String pn = ResourceHelper.getPackageName(name);
2642 if (packages.contains(pn) && !module.isOpen(pn, caller)) {
2643 // resource is in package not open to caller
2644 return null;
2645 }
2646 }
2647 }
2648 String mn = getModule().getName();
2649 ClassLoader cl = getClassLoader0();
|
408 * @param name The <a href="ClassLoader.html#name">binary name</a>
409 * of the class
410 * @return {@code Class} object of the given name defined in the given module;
411 * {@code null} if not found.
412 *
413 * @throws NullPointerException if the given module or name is {@code null}
414 *
415 * @throws LinkageError if the linkage fails
416 *
417 * @throws SecurityException
418 * <ul>
419 * <li> if the caller is not the specified module and
420 * {@code RuntimePermission("getClassLoader")} permission is denied; or</li>
421 * <li> access to the module content is denied. For example,
422 * permission check will be performed when a class loader calls
423 * {@link ModuleReader#open(String)} to read the bytes of a class file
424 * in a module.</li>
425 * </ul>
426 *
427 * @since 9
428 * @spec JPMS
429 */
430 @CallerSensitive
431 public static Class<?> forName(Module module, String name) {
432 Objects.requireNonNull(module);
433 Objects.requireNonNull(name);
434
435 Class<?> caller = Reflection.getCallerClass();
436 if (caller != null && caller.getModule() != module) {
437 // if caller is null, Class.forName is the last java frame on the stack.
438 // java.base has all permissions
439 SecurityManager sm = System.getSecurityManager();
440 if (sm != null) {
441 sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
442 }
443 }
444
445 PrivilegedAction<ClassLoader> pa = module::getClassLoader;
446 ClassLoader cl = AccessController.doPrivileged(pa);
447 if (cl != null) {
448 return cl.loadClass(module, name);
803 }
804
805 // Package-private to allow ClassLoader access
806 ClassLoader getClassLoader0() { return classLoader; }
807
808 /**
809 * Returns the module that this class or interface is a member of.
810 *
811 * If this class represents an array type then this method returns the
812 * {@code Module} for the element type. If this class represents a
813 * primitive type or void, then the {@code Module} object for the
814 * {@code java.base} module is returned.
815 *
816 * If this class is in an unnamed module then the {@link
817 * ClassLoader#getUnnamedModule() unnamed} {@code Module} of the class
818 * loader for this class is returned.
819 *
820 * @return the module that this class or interface is a member of
821 *
822 * @since 9
823 * @spec JPMS
824 */
825 public Module getModule() {
826 return module;
827 }
828
829 // set by VM
830 private transient Module module;
831
832 // Initialized in JVM not by private constructor
833 // This field is filtered from reflection access, i.e. getDeclaredField
834 // will throw NoSuchFieldException
835 private final ClassLoader classLoader;
836
837 /**
838 * Returns an array of {@code TypeVariable} objects that represent the
839 * type variables declared by the generic declaration represented by this
840 * {@code GenericDeclaration} object, in declaration order. Returns an
841 * array of length 0 if the underlying generic declaration declares no type
842 * variables.
843 *
909 return getSuperclass();
910 }
911
912 // Historical irregularity:
913 // Generic signature marks interfaces with superclass = Object
914 // but this API returns null for interfaces
915 if (isInterface()) {
916 return null;
917 }
918
919 return info.getSuperclass();
920 }
921
922 /**
923 * Gets the package of this class.
924 *
925 * <p>If this class represents an array type, a primitive type or void,
926 * this method returns {@code null}.
927 *
928 * @return the package of this class.
929 * @revised 9
930 * @spec JPMS
931 */
932 public Package getPackage() {
933 if (isPrimitive() || isArray()) {
934 return null;
935 }
936 ClassLoader cl = getClassLoader0();
937 return cl != null ? cl.definePackage(this)
938 : BootLoader.definePackage(this);
939 }
940
941 /**
942 * Returns the fully qualified package name.
943 *
944 * <p> If this class is a top level class, then this method returns the fully
945 * qualified name of the package that the class is a member of, or the
946 * empty string if the class is in an unnamed package.
947 *
948 * <p> If this class is a member class, then this method is equivalent to
949 * invoking {@code getPackageName()} on the {@link #getEnclosingClass
950 * enclosing class}.
951 *
952 * <p> If this class is a {@link #isLocalClass local class} or an {@link
953 * #isAnonymousClass() anonymous class}, then this method is equivalent to
954 * invoking {@code getPackageName()} on the {@link #getDeclaringClass
955 * declaring class} of the {@link #getEnclosingMethod enclosing method} or
956 * {@link #getEnclosingConstructor enclosing constructor}.
957 *
958 * <p> If this class represents an array type then this method returns the
959 * package name of the element type. If this class represents a primitive
960 * type or void then the package name "{@code java.lang}" is returned.
961 *
962 * @return the fully qualified package name
963 *
964 * @since 9
965 * @spec JPMS
966 * @jls 6.7 Fully Qualified Names
967 */
968 public String getPackageName() {
969 String pn = this.packageName;
970 if (pn == null) {
971 Class<?> c = this;
972 while (c.isArray()) {
973 c = c.getComponentType();
974 }
975 if (c.isPrimitive()) {
976 pn = "java.lang";
977 } else {
978 String cn = c.getName();
979 int dot = cn.lastIndexOf('.');
980 pn = (dot != -1) ? cn.substring(0, dot).intern() : "";
981 }
982 this.packageName = pn;
983 }
984 return pn;
985 }
986
987 // cached package name
988 private transient String packageName;
989
990 /**
991 * Returns the interfaces directly implemented by the class or interface
992 * represented by this object.
993 *
994 * <p>If this object represents a class, the return value is an array
995 * containing objects representing all interfaces directly implemented by
996 * the class. The order of the interface objects in the array corresponds
997 * to the order of the interface names in the {@code implements} clause of
998 * the declaration of the class represented by this object. For example,
999 * given the declaration:
1000 * <blockquote>
1001 * {@code class Shimmer implements FloorWax, DessertTopping { ... }}
2488 *
2489 * @since 1.1
2490 */
2491 @CallerSensitive
2492 public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
2493 throws NoSuchMethodException, SecurityException
2494 {
2495 SecurityManager sm = System.getSecurityManager();
2496 if (sm != null) {
2497 checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
2498 }
2499
2500 return getReflectionFactory().copyConstructor(
2501 getConstructor0(parameterTypes, Member.DECLARED));
2502 }
2503
2504 /**
2505 * Finds a resource with a given name.
2506 *
2507 * <p> If this class is in a named {@link Module Module} then this method
2508 * will attempt to find the resource in the module. This is done by
2509 * delegating to the module's class loader {@link
2510 * ClassLoader#findResource(String,String) findResource(String,String)}
2511 * method, invoking it with the module name and the absolute name of the
2512 * resource. Resources in named modules are subject to the rules for
2513 * encapsulation specified in the {@code Module} {@link
2514 * Module#getResourceAsStream getResourceAsStream} method and so this
2515 * method returns {@code null} when the resource is a
2516 * non-"{@code .class}" resource in a package that is not open to the
2517 * caller's module.
2518 *
2519 * <p> Otherwise, if this class is not in a named module then the rules for
2520 * searching resources associated with a given class are implemented by the
2521 * defining {@linkplain ClassLoader class loader} of the class. This method
2522 * delegates to this object's class loader. If this object was loaded by
2523 * the bootstrap class loader, the method delegates to {@link
2524 * ClassLoader#getSystemResourceAsStream}.
2525 *
2526 * <p> Before delegation, an absolute resource name is constructed from the
2527 * given resource name using this algorithm:
2528 *
2529 * <ul>
2530 *
2531 * <li> If the {@code name} begins with a {@code '/'}
2532 * (<code>'\u002f'</code>), then the absolute name of the resource is the
2533 * portion of the {@code name} following the {@code '/'}.
2534 *
2535 * <li> Otherwise, the absolute name is of the following form:
2536 *
2537 * <blockquote>
2538 * {@code modified_package_name/name}
2539 * </blockquote>
2540 *
2541 * <p> Where the {@code modified_package_name} is the package name of this
2542 * object with {@code '/'} substituted for {@code '.'}
2543 * (<code>'\u002e'</code>).
2544 *
2545 * </ul>
2546 *
2547 * @param name name of the desired resource
2548 * @return A {@link java.io.InputStream} object; {@code null} if no
2549 * resource with this name is found, the resource is in a package
2550 * that is not {@link Module#isOpen(String, Module) open} to at
2551 * least the caller module, or access to the resource is denied
2552 * by the security manager.
2553 * @throws NullPointerException If {@code name} is {@code null}
2554 *
2555 * @see Module#getResourceAsStream(String)
2556 * @since 1.1
2557 * @revised 9
2558 * @spec JPMS
2559 */
2560 @CallerSensitive
2561 public InputStream getResourceAsStream(String name) {
2562 name = resolveName(name);
2563
2564 Module module = getModule();
2565 if (module.isNamed()) {
2566 if (!ResourceHelper.isSimpleResource(name)) {
2567 Module caller = Reflection.getCallerClass().getModule();
2568 if (caller != module) {
2569 Set<String> packages = module.getDescriptor().packages();
2570 String pn = ResourceHelper.getPackageName(name);
2571 if (packages.contains(pn) && !module.isOpen(pn, caller)) {
2572 // resource is in package not open to caller
2573 return null;
2574 }
2575 }
2576 }
2577
2578 String mn = module.getName();
2591 }
2592
2593 } catch (IOException | SecurityException e) {
2594 return null;
2595 }
2596 }
2597
2598 // unnamed module
2599 ClassLoader cl = getClassLoader0();
2600 if (cl == null) {
2601 return ClassLoader.getSystemResourceAsStream(name);
2602 } else {
2603 return cl.getResourceAsStream(name);
2604 }
2605 }
2606
2607 /**
2608 * Finds a resource with a given name.
2609 *
2610 * <p> If this class is in a named {@link Module Module} then this method
2611 * will attempt to find the resource in the module. This is done by
2612 * delegating to the module's class loader {@link
2613 * ClassLoader#findResource(String,String) findResource(String,String)}
2614 * method, invoking it with the module name and the absolute name of the
2615 * resource. Resources in named modules are subject to the rules for
2616 * encapsulation specified in the {@code Module} {@link
2617 * Module#getResourceAsStream getResourceAsStream} method and so this
2618 * method returns {@code null} when the resource is a
2619 * non-"{@code .class}" resource in a package that is not open to the
2620 * caller's module.
2621 *
2622 * <p> Otherwise, if this class is not in a named module then the rules for
2623 * searching resources associated with a given class are implemented by the
2624 * defining {@linkplain ClassLoader class loader} of the class. This method
2625 * delegates to this object's class loader. If this object was loaded by
2626 * the bootstrap class loader, the method delegates to {@link
2627 * ClassLoader#getSystemResource}.
2628 *
2629 * <p> Before delegation, an absolute resource name is constructed from the
2630 * given resource name using this algorithm:
2631 *
2632 * <ul>
2633 *
2634 * <li> If the {@code name} begins with a {@code '/'}
2635 * (<code>'\u002f'</code>), then the absolute name of the resource is the
2636 * portion of the {@code name} following the {@code '/'}.
2637 *
2638 * <li> Otherwise, the absolute name is of the following form:
2639 *
2640 * <blockquote>
2641 * {@code modified_package_name/name}
2642 * </blockquote>
2643 *
2644 * <p> Where the {@code modified_package_name} is the package name of this
2645 * object with {@code '/'} substituted for {@code '.'}
2646 * (<code>'\u002e'</code>).
2647 *
2648 * </ul>
2649 *
2650 * @param name name of the desired resource
2651 * @return A {@link java.net.URL} object; {@code null} if no resource with
2652 * this name is found, the resource cannot be located by a URL, the
2653 * resource is in a package that is not
2654 * {@link Module#isOpen(String, Module) open} to at least the caller
2655 * module, or access to the resource is denied by the security
2656 * manager.
2657 * @throws NullPointerException If {@code name} is {@code null}
2658 * @since 1.1
2659 * @revised 9
2660 * @spec JPMS
2661 */
2662 @CallerSensitive
2663 public URL getResource(String name) {
2664 name = resolveName(name);
2665
2666 Module module = getModule();
2667 if (module.isNamed()) {
2668 if (!ResourceHelper.isSimpleResource(name)) {
2669 Module caller = Reflection.getCallerClass().getModule();
2670 if (caller != module) {
2671 Set<String> packages = module.getDescriptor().packages();
2672 String pn = ResourceHelper.getPackageName(name);
2673 if (packages.contains(pn) && !module.isOpen(pn, caller)) {
2674 // resource is in package not open to caller
2675 return null;
2676 }
2677 }
2678 }
2679 String mn = getModule().getName();
2680 ClassLoader cl = getClassLoader0();
|