Home > Java > javaTutorial > Java reflection advanced

Java reflection advanced

黄舟
Release: 2017-02-24 09:47:47
Original
1391 people have browsed it


We usually use reflection The first way, first let’s take a look at Class.forName(clssName):
Note: The incoming className must be Full path of the class, otherwise an error java.lang.ClassNotFoundException will be reported.

public static Class<?> forName(String className)                
throws ClassNotFoundException {        
return forName0(className, true, ClassLoader.getCallerClassLoader());
    }
Copy after login
Copy after login

By calling the forName0 method, use ClassLoader.getCallerClassLoader() to load ClassLoader

    // Returns the invoker&#39;s class loader, or null if none.
    // NOTE: This must always be invoked when there is exactly one intervening
    // frame from the core libraries on the stack between this method&#39;s
    // invocation and the desired invoker.
    static ClassLoader getCallerClassLoader() {
        // NOTE use of more generic Reflection.getCallerClass()
        Class caller = Reflection.getCallerClass(3);
        // This can be null if the VM is requesting it        
        if (caller == null) {            
        return null;
        }
        // Circumvent security check since this is package-private        
        return caller.getClassLoader0();
    }
Copy after login
Copy after login

After getting the ClassLoader, call claser.getClassLoader0() this is a private package that avoids security checks.

 // Package-private to allow ClassLoader access
    native ClassLoader getClassLoader0();
Copy after login
Copy after login

1. What is ClassLoader?

We usually use reflection The first way, first let’s take a look at Class.forName(clssName):
Note : The passed in className must be the full path of the class, otherwise an error java.lang.ClassNotFoundException will be reported.

public static Class<?> forName(String className)                
throws ClassNotFoundException {        
return forName0(className, true, ClassLoader.getCallerClassLoader());
    }
Copy after login
Copy after login

By calling the forName0 method, use ClassLoader.getCallerClassLoader() to load ClassLoader

    // Returns the invoker&#39;s class loader, or null if none.
    // NOTE: This must always be invoked when there is exactly one intervening
    // frame from the core libraries on the stack between this method&#39;s
    // invocation and the desired invoker.
    static ClassLoader getCallerClassLoader() {
        // NOTE use of more generic Reflection.getCallerClass()
        Class caller = Reflection.getCallerClass(3);
        // This can be null if the VM is requesting it        
        if (caller == null) {            
        return null;
        }
        // Circumvent security check since this is package-private        
        return caller.getClassLoader0();
    }
Copy after login
Copy after login

After getting the ClassLoader, call claser.getClassLoader0() this is a private package that avoids security checks.

 // Package-private to allow ClassLoader access
    native ClassLoader getClassLoader0();
Copy after login
Copy after login

1. What is ClassLoader?

The above is the content of advanced reflection in java. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template