Java Class类理解

Class是用来表示其他类的抽象,比如可以将类的类型,类的类名,类的hashcode封装成类的域,另外可以定义一些方法用以取得这些域值。如此而来,就封装了一个表示类型(type)的类。

Class类没有公开的构造函数。实例都是由JVM调用private 的构造函数创建的。

Class源码分析:

private static native void registerNatives();
    static {
        registerNatives();
    }

这是在源码中除声明static final常量之后的第一行代码,native 关键字在Java中是用于说明其修饰的方法是原生态方法,其实现是通过其他语言实现的文件中。因为Java语言不能对操作系统底层进行访问和操作,但可以通过JNI(Java Native Interface)接口调用其他语言来实现对底层的访问。JNI允许Java代码使用以其他语言编写的代码和代码库(windows DLL 动态链接库: https://support.microsoft.com/zh-cn/help/815065/what-is-a-dll)

不过我要提醒您,对 Java 外部的调用通常不能移植到其他平台上,在 applet 中还可能引发安全异常。实现本地代码将使您的 Java 应用程序无法通过 100% 纯 Java 测试。但是,如果必须执行本地调用,则要考虑几个准则:

  1. 将您的所有本地方法都封装在单个类中,这个类调用单个 DLL。对于每种目标操作系统,都可以用特定于适当平台的版本替换这个 DLL。这样就可以将本地代码的影响减至最小,并有助于将以后所需的移植问题包含在内。
  2. 本地方法要简单。尽量将您的 DLL 对任何第三方(包括 Microsoft)运行时 DLL 的依赖减到最小。使您的本地方法尽量独立,以将加载您的 DLL 和应用程序所需的开销减到最小。如果需要运行时 DLL,必须随应用程序一起提供它们。
    (在Windows 中实现Java本地方法: https://www.ibm.com/developerworks/cn/java/jnimthds/
private Class(ClassLoader loader) {
        // Initialize final field for classLoader.  The initialization value of non-null
        // prevents future JIT optimizations from assuming this final field is null.
        classLoader = loader;
    }

私有的构造函数,只能通过JVM来调用它,来构造一个Class实例。

//forName
//有重载方法,也有本地方法

public static Class<?> forName(String className)
                throws ClassNotFoundException {
        Class<?> caller = Reflection.getCallerClass();
        return forName0(className, true, ClassLoader.getClassLoader(caller), caller);
    }

public static Class<?> forName(String name, boolean initialize,
                                   ClassLoader loader)
        throws ClassNotFoundException
    {
        Class<?> caller = null;
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            // Reflective call to get caller class is only needed if a security manager
            // is present.  Avoid the overhead of making this call otherwise.
            caller = Reflection.getCallerClass();
            if (sun.misc.VM.isSystemDomainLoader(loader)) {
                ClassLoader ccl = ClassLoader.getClassLoader(caller);
                if (!sun.misc.VM.isSystemDomainLoader(ccl)) {
                    sm.checkPermission(
                        SecurityConstants.GET_CLASSLOADER_PERMISSION);
                }
            }
        }
        return forName0(name, initialize, loader, caller);
    }

/** Called after security check for system loader access checks have been made. */
private static native Class<?> forName0(String name, boolean initialize,
                                            ClassLoader loader,
                                            Class<?> caller)
        throws ClassNotFoundException;

重载方法中initialize这个boolean类型指定是否要初始化对应的Class实例,loader指定加载Class实例的加载器。

public T newInstance()          // T是个泛型参数,是Class实例所表示的Java类

        throws InstantiationException,IllegalAccessException

    {

        if (System.getSecurityManager()!=null) {

            checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());

        }

        return newInstance0();     //这是一个本地方法,也在Class类中定义

}
//以上为旧版本,在JDK 8中newInstance的代码行数远多于此,要进行Constructor lookup, Disable accessibility checks, Security checks等

这是一个实例方法,因此要由实例对象来调用。

public ClassLoader getClassLoader() {
        ClassLoader cl = getClassLoader0();
        if (cl == null)
            return null;
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            ClassLoader.checkClassLoaderPermission(cl, Reflection.getCallerClass());
        }
        return cl;
    }
// 本地方法
ClassLoader getClassLoader0() { return classLoader; }

后者为本地方法,返回Class对象代表的类的类加载器。如果类加载器是Bootstrap, 则返回Null。

参考资料
https://blog.csdn.net/bingduanlbd/article/details/8424243

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值