首页 > Java > java教程 > 正文

java之反射进阶

黄舟
发布: 2017-02-24 09:47:47
原创
1538人浏览过


我们反射通常使用第一种方式,首先来看看class.forname(clssname):
注意:传入的classname必须是类的全路径,否则会报错java.lang.classnotfoundexception

public static Class<?> forName(String className)                
throws ClassNotFoundException {        
return forName0(className, true, ClassLoader.getCallerClassLoader());
    }
登录后复制

通过调用forName0方法,使用ClassLoader.getCallerClassLoader()来加载ClassLoader

千面视频动捕
千面视频动捕

千面视频动捕是一个AI视频动捕解决方案,专注于将视频中的人体关节二维信息转化为三维模型动作。

千面视频动捕 173
查看详情 千面视频动捕

立即学习Java免费学习笔记(深入)”;

    // Returns the invoker'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'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();
    }
登录后复制

在获取ClassLoader调用claser.getClassLoader0() 这是一个私有包,可以避免安全检查。

 // Package-private to allow ClassLoader access
    native ClassLoader getClassLoader0();
登录后复制

1、ClassLoader到底是个什么东西

我们反射通常使用第一种方式,首先来看看class.forname(clssname):
注意:传入的classname必须是类的全路径,否则会报错java.lang.classnotfoundexception

public static Class<?> forName(String className)                
throws ClassNotFoundException {        
return forName0(className, true, ClassLoader.getCallerClassLoader());
    }
登录后复制

通过调用forName0方法,使用ClassLoader.getCallerClassLoader()来加载ClassLoader

立即学习Java免费学习笔记(深入)”;

    // Returns the invoker'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'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();
    }
登录后复制

在获取ClassLoader调用claser.getClassLoader0() 这是一个私有包,可以避免安全检查。

 // Package-private to allow ClassLoader access
    native ClassLoader getClassLoader0();
登录后复制

1、ClassLoader到底是个什么东西

 以上就是java之反射进阶的内容,更多相关内容请关注PHP中文网(www.php.cn)!

相关标签:
java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号