Java Code Examples for jdk.internal.loader.BootLoader#loadClass()

The following examples show how to use jdk.internal.loader.BootLoader#loadClass() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Package.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
private Class<?> getPackageInfo() {
    if (packageInfo == null) {
        // find package-info.class defined by loader
        String cn = packageName() + ".package-info";
        Module module = module();
        PrivilegedAction<ClassLoader> pa = module::getClassLoader;
        ClassLoader loader = AccessController.doPrivileged(pa);
        Class<?> c;
        if (loader != null) {
            c = loader.loadClass(module, cn);
        } else {
            c = BootLoader.loadClass(module, cn);
        }

        if (c != null) {
            packageInfo = c;
        } else {
            // store a proxy for the package info that has no annotations
            class PackageInfoProxy {}
            packageInfo = PackageInfoProxy.class;
        }
    }
    return packageInfo;
}
 
Example 2
Source File: Package.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private Class<?> getPackageInfo() {
    if (packageInfo == null) {
        // find package-info.class defined by loader
        String cn = packageName() + ".package-info";
        Module module = module();
        PrivilegedAction<ClassLoader> pa = module::getClassLoader;
        ClassLoader loader = AccessController.doPrivileged(pa);
        Class<?> c;
        if (loader != null) {
            c = loader.loadClass(module, cn);
        } else {
            c = BootLoader.loadClass(module, cn);
        }

        if (c != null) {
            packageInfo = c;
        } else {
            // store a proxy for the package info that has no annotations
            class PackageInfoProxy {}
            packageInfo = PackageInfoProxy.class;
        }
    }
    return packageInfo;
}
 
Example 3
Source File: Class.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns the {@code Class} with the given <a href="ClassLoader.html#name">
 * binary name</a> in the given module.
 *
 * <p> This method attempts to locate, load, and link the class or interface.
 * It does not run the class initializer.  If the class is not found, this
 * method returns {@code null}. </p>
 *
 * <p> If the class loader of the given module defines other modules and
 * the given name is a class defined in a different module, this method
 * returns {@code null} after the class is loaded. </p>
 *
 * <p> This method does not check whether the requested class is
 * accessible to its caller. </p>
 *
 * @apiNote
 * This method returns {@code null} on failure rather than
 * throwing a {@link ClassNotFoundException}, as is done by
 * the {@link #forName(String, boolean, ClassLoader)} method.
 * The security check is a stack-based permission check if the caller
 * loads a class in another module.
 *
 * @param  module   A module
 * @param  name     The <a href="ClassLoader.html#name">binary name</a>
 *                  of the class
 * @return {@code Class} object of the given name defined in the given module;
 *         {@code null} if not found.
 *
 * @throws NullPointerException if the given module or name is {@code null}
 *
 * @throws LinkageError if the linkage fails
 *
 * @throws SecurityException
 *         <ul>
 *         <li> if the caller is not the specified module and
 *         {@code RuntimePermission("getClassLoader")} permission is denied; or</li>
 *         <li> access to the module content is denied. For example,
 *         permission check will be performed when a class loader calls
 *         {@link ModuleReader#open(String)} to read the bytes of a class file
 *         in a module.</li>
 *         </ul>
 *
 * @since 9
 * @spec JPMS
 */
@CallerSensitive
public static Class<?> forName(Module module, String name) {
    Objects.requireNonNull(module);
    Objects.requireNonNull(name);

    Class<?> caller = Reflection.getCallerClass();
    if (caller != null && caller.getModule() != module) {
        // if caller is null, Class.forName is the last java frame on the stack.
        // java.base has all permissions
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
        }
    }

    PrivilegedAction<ClassLoader> pa = module::getClassLoader;
    ClassLoader cl = AccessController.doPrivileged(pa);
    if (cl != null) {
        return cl.loadClass(module, name);
    } else {
        return BootLoader.loadClass(module, name);
    }
}
 
Example 4
Source File: Class.java    From Bytecoder with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the {@code Class} with the given <a href="ClassLoader.html#binary-name">
 * binary name</a> in the given module.
 *
 * <p> This method attempts to locate and load the class or interface.
 * It does not link the class, and does not run the class initializer.
 * If the class is not found, this method returns {@code null}. </p>
 *
 * <p> If the class loader of the given module defines other modules and
 * the given name is a class defined in a different module, this method
 * returns {@code null} after the class is loaded. </p>
 *
 * <p> This method does not check whether the requested class is
 * accessible to its caller. </p>
 *
 * @apiNote
 * This method returns {@code null} on failure rather than
 * throwing a {@link ClassNotFoundException}, as is done by
 * the {@link #forName(String, boolean, ClassLoader)} method.
 * The security check is a stack-based permission check if the caller
 * loads a class in another module.
 *
 * @param  module   A module
 * @param  name     The <a href="ClassLoader.html#binary-name">binary name</a>
 *                  of the class
 * @return {@code Class} object of the given name defined in the given module;
 *         {@code null} if not found.
 *
 * @throws NullPointerException if the given module or name is {@code null}
 *
 * @throws LinkageError if the linkage fails
 *
 * @throws SecurityException
 *         <ul>
 *         <li> if the caller is not the specified module and
 *         {@code RuntimePermission("getClassLoader")} permission is denied; or</li>
 *         <li> access to the module content is denied. For example,
 *         permission check will be performed when a class loader calls
 *         {@link ModuleReader#open(String)} to read the bytes of a class file
 *         in a module.</li>
 *         </ul>
 *
 * @jls 12.2 Loading of Classes and Interfaces
 * @jls 12.3 Linking of Classes and Interfaces
 * @since 9
 * @spec JPMS
 */
@CallerSensitive
public static Class<?> forName(Module module, String name) {
    Objects.requireNonNull(module);
    Objects.requireNonNull(name);

    ClassLoader cl;
    SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        Class<?> caller = Reflection.getCallerClass();
        if (caller != null && caller.getModule() != module) {
            // if caller is null, Class.forName is the last java frame on the stack.
            // java.base has all permissions
            sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
        }
        PrivilegedAction<ClassLoader> pa = module::getClassLoader;
        cl = AccessController.doPrivileged(pa);
    } else {
        cl = module.getClassLoader();
    }

    if (cl != null) {
        return cl.loadClass(module, name);
    } else {
        return BootLoader.loadClass(module, name);
    }
}