< prev index next >

src/java.base/share/classes/java/lang/Class.java

Print this page




 471     @CallerSensitive
 472     public static Class<?> forName(Module module, String name) {
 473         Objects.requireNonNull(module);
 474         Objects.requireNonNull(name);
 475 
 476         ClassLoader cl;
 477         SecurityManager sm = System.getSecurityManager();
 478         if (sm != null) {
 479             Class<?> caller = Reflection.getCallerClass();
 480             if (caller != null && caller.getModule() != module) {
 481                 // if caller is null, Class.forName is the last java frame on the stack.
 482                 // java.base has all permissions
 483                 sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
 484             }
 485             PrivilegedAction<ClassLoader> pa = module::getClassLoader;
 486             cl = AccessController.doPrivileged(pa);
 487         } else {
 488             cl = module.getClassLoader();
 489         }
 490 

 491         if (cl != null) {
 492             return cl.loadClass(module, name);
 493         } else {
 494             return BootLoader.loadClass(module, name);
 495         }



 496     }




 497 
 498     /**
 499      * Creates a new instance of the class represented by this {@code Class}
 500      * object.  The class is instantiated as if by a {@code new}
 501      * expression with an empty argument list.  The class is initialized if it
 502      * has not already been initialized.
 503      *
 504      * @deprecated This method propagates any exception thrown by the
 505      * nullary constructor, including a checked exception.  Use of
 506      * this method effectively bypasses the compile-time exception
 507      * checking that would otherwise be performed by the compiler.
 508      * The {@link
 509      * java.lang.reflect.Constructor#newInstance(java.lang.Object...)
 510      * Constructor.newInstance} method avoids this problem by wrapping
 511      * any exception thrown by the constructor in a (checked) {@link
 512      * java.lang.reflect.InvocationTargetException}.
 513      *
 514      * <p>The call
 515      *
 516      * <pre>{@code




 471     @CallerSensitive
 472     public static Class<?> forName(Module module, String name) {
 473         Objects.requireNonNull(module);
 474         Objects.requireNonNull(name);
 475 
 476         ClassLoader cl;
 477         SecurityManager sm = System.getSecurityManager();
 478         if (sm != null) {
 479             Class<?> caller = Reflection.getCallerClass();
 480             if (caller != null && caller.getModule() != module) {
 481                 // if caller is null, Class.forName is the last java frame on the stack.
 482                 // java.base has all permissions
 483                 sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
 484             }
 485             PrivilegedAction<ClassLoader> pa = module::getClassLoader;
 486             cl = AccessController.doPrivileged(pa);
 487         } else {
 488             cl = module.getClassLoader();
 489         }
 490 
 491         Class<?> ret;
 492         if (cl != null) {
 493             ret = cl.loadClass(module, name);
 494         } else {
 495             ret = BootLoader.loadClass(module, name);
 496         }
 497         if (ret != null) {
 498             // The loaded class should also be linked
 499             linkClass(ret);
 500         }
 501         return ret;
 502     }
 503 
 504     private static native void linkClass(Class<?> c);
 505     
 506     /**
 507      * Creates a new instance of the class represented by this {@code Class}
 508      * object.  The class is instantiated as if by a {@code new}
 509      * expression with an empty argument list.  The class is initialized if it
 510      * has not already been initialized.
 511      *
 512      * @deprecated This method propagates any exception thrown by the
 513      * nullary constructor, including a checked exception.  Use of
 514      * this method effectively bypasses the compile-time exception
 515      * checking that would otherwise be performed by the compiler.
 516      * The {@link
 517      * java.lang.reflect.Constructor#newInstance(java.lang.Object...)
 518      * Constructor.newInstance} method avoids this problem by wrapping
 519      * any exception thrown by the constructor in a (checked) {@link
 520      * java.lang.reflect.InvocationTargetException}.
 521      *
 522      * <p>The call
 523      *
 524      * <pre>{@code


< prev index next >