宁波网站建设的企业,用织梦怎么做网站,长春做网站好的公司,莞城东莞网站建设目录
版本信息#xff1a;
写在前面#xff1a;
源码论证#xff1a;
总结#xff1a; 版本信息#xff1a; jdk版本#xff1a;jdk8u40 写在前面#xff1a;
在Java类库中很多类都有一个registerNatives的native方法#xff0c;并且写在static静态代码块中进行初…目录
版本信息
写在前面
源码论证
总结 版本信息 jdk版本jdk8u40 写在前面
在Java类库中很多类都有一个registerNatives的native方法并且写在static静态代码块中进行初始化调用有不少的读者应该会对这个方法感兴趣但是此方法是一个native方法让不少的读者望而却步所以笔者写在这一篇关于registerNatives方法的文章
要真正弄懂registerNatives方法非常的复杂因为你需要明白 C/C 的执行和动态链接库的原理、以及JVM对于动态链接库的封装处理、以及Java语言作为解释性语言JVM的解释器引擎如何对其做处理、以及JVM对于类加载的一个过程他是如何链接native方法的
这并不是在劝退各位读者而是让读者有一个对知识的认知。当然笔者认为高级的封装是有他的意义存在屏蔽掉底层的各种复杂的实现。所以笔者也会尽量用通俗易懂的方式介绍registerNatives方法 源码论证
首先需要说明白registerNatives这个native方法存在的意义把类中其他native方法的实现映射到JVM虚拟机中内部的方法native方法的实现是c/c语言而JVM也是c语言实现所以是完成一个其他native方法的映射
在JVM的实现Hotspot源码中一般情况下native层面的源码文件命名是跟类名一致比如拿Thread.java 类来说在Hotspot中他的native层面文件就是 Thread.c 。 正好Thread类中也有registerNatives接下来我们就看到Thread中registerNatives方法的native层面源码实现 /src/share/native/java/lang/Thread.c 文件中
typedef struct {char *name;char *signature;void *fnPtr;
} JNINativeMethod;static JNINativeMethod methods[] {{start0, ()V, (void *)JVM_StartThread},{stop0, ( OBJ )V, (void *)JVM_StopThread},{isAlive, ()Z, (void *)JVM_IsThreadAlive},{suspend0, ()V, (void *)JVM_SuspendThread},{resume0, ()V, (void *)JVM_ResumeThread},{setPriority0, (I)V, (void *)JVM_SetThreadPriority},{yield, ()V, (void *)JVM_Yield},{sleep, (J)V, (void *)JVM_Sleep},{currentThread, () THD, (void *)JVM_CurrentThread},{countStackFrames, ()I, (void *)JVM_CountStackFrames},{interrupt0, ()V, (void *)JVM_Interrupt},{isInterrupted, (Z)Z, (void *)JVM_IsInterrupted},{holdsLock, ( OBJ )Z, (void *)JVM_HoldsLock},{getThreads, ()[ THD, (void *)JVM_GetAllThreads},{dumpThreads, ([ THD )[[ STE, (void *)JVM_DumpThreads},{setNativeName, ( STR )V, (void *)JVM_SetNativeThreadName},
};
JNINativeMethod结构体定义了native方法的名字、签名、目标方法的地址。在Thread.c 文件中定义了JNINativeMethod结构体数组完成了native方法名和目标方法映射而这些方法名恰好是Thread.java 类中其他native方法这也对应上上文说的 把类中其他native方法的实现映射到JVM虚拟机中内部的方法 等同于说执行Thread类中sleep方法就会执行JVM_Sleep这个JVM内部的方法
接下来我们看到registerNatives方法的实现看它是如何完成的映射。
JNIEXPORT void JNICALL
Java_java_lang_Thread_registerNatives(JNIEnv *env, jclass cls)
{// JNIEnv结构体是JNI给c语言提供访问JVM使用的内部有很多函数去访问JVM// 而这里就是调用JNIEnv结构体中RegisterNatives方法(*env)-RegisterNatives(env, cls, methods, ARRAY_LENGTH(methods));
}
又开发过JNI的读者可能这里就很容易理解JNIEnv这个结构体是JNI给c语言提供访问JVM使用的JNIEnv结构体内部有很多函数去访问JVM而这里就是调用JNIEnv结构体中RegisterNatives方法所以我们看到 src/share/vm/prims/jni.cpp 文件中RegisterNatives方法。
JNI_ENTRY(jint, jni_RegisterNatives(JNIEnv *env, jclass clazz,const JNINativeMethod *methods,jint nMethods))jint ret 0;// 获取到Klass对象在Hotspot中Klass对象可以理解是Java的类KlassHandle h_k(thread, java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz)));// 遍历JNINativeMethod结构体数组for (int index 0; index nMethods; index) {// 获取到方法名和签名const char* meth_name methods[index].name;const char* meth_sig methods[index].signature;int meth_name_len (int)strlen(meth_name);// 把方法名和签名转换成Hotspot中符号信息实际上这一步无需关心TempNewSymbol name SymbolTable::probe(meth_name, meth_name_len);TempNewSymbol signature SymbolTable::probe(meth_sig, (int)strlen(meth_sig));// 我们需要把目标方法注册到类中的native方法// 这一步就是完成注册bool res register_native(h_k, name, signature,(address) methods[index].fnPtr, THREAD);}return ret;
JNI_END
获取到Klass对象在Hotspot中Klass对象可以理解为Java的类因为类中定义了方法而这一步就是要把目标方法JVM内部方法注册到类中的native方法遍历JNINativeMethod结构体数组获取到方法名和签名信息因为需要通过它们找到native方法调用register_native方法完成注册工作
接下来只需要看到register_native方法的实现即可。
static bool register_native(KlassHandle k, Symbol* name, Symbol* signature, address entry, TRAPS) {// 通过方法名和签名找到类中的方法Method* method k()-lookup_method(name, signature);………… // 省略一些判断if (entry ! NULL) {// 把目标方法JVM内部方法注册到类中的native中// 下次调用native方法时执行的就是JVM内部的方法method-set_native_function(entry,Method::native_bind_event_is_interesting);}………… // 省略一些判断return true;
}
这里就特别的简单了通过方法名和签名得到方法然后把目标方法JVM内部方法设置到方法中下次执行native方法时就会执行JVM内部的方法 总结
整体流程不难但是笔者是直接 告诉答案 如果说需要完整的闭环JNI和native方法的注册和native方法的执行这个过程将会特别的复杂不过学习某个层面就应该把他的下层当作黑盒来理解即可 文章转载自: http://www.morning.sfhjx.cn.gov.cn.sfhjx.cn http://www.morning.mlgsc.com.gov.cn.mlgsc.com http://www.morning.ldzxf.cn.gov.cn.ldzxf.cn http://www.morning.fxygn.cn.gov.cn.fxygn.cn http://www.morning.ywtbk.cn.gov.cn.ywtbk.cn http://www.morning.ghxkm.cn.gov.cn.ghxkm.cn http://www.morning.bwkhp.cn.gov.cn.bwkhp.cn http://www.morning.hwljx.cn.gov.cn.hwljx.cn http://www.morning.pxsn.cn.gov.cn.pxsn.cn http://www.morning.pxspq.cn.gov.cn.pxspq.cn http://www.morning.bncrx.cn.gov.cn.bncrx.cn http://www.morning.lbpqk.cn.gov.cn.lbpqk.cn http://www.morning.ghpld.cn.gov.cn.ghpld.cn http://www.morning.hpdpp.cn.gov.cn.hpdpp.cn http://www.morning.ghzfx.cn.gov.cn.ghzfx.cn http://www.morning.qllcp.cn.gov.cn.qllcp.cn http://www.morning.qfkxj.cn.gov.cn.qfkxj.cn http://www.morning.qsy40.cn.gov.cn.qsy40.cn http://www.morning.rpwm.cn.gov.cn.rpwm.cn http://www.morning.ykmg.cn.gov.cn.ykmg.cn http://www.morning.yixingshengya.com.gov.cn.yixingshengya.com http://www.morning.xdnhw.cn.gov.cn.xdnhw.cn http://www.morning.gyjld.cn.gov.cn.gyjld.cn http://www.morning.ylkkh.cn.gov.cn.ylkkh.cn http://www.morning.gqtxz.cn.gov.cn.gqtxz.cn http://www.morning.gqjwz.cn.gov.cn.gqjwz.cn http://www.morning.bxdlrcz.cn.gov.cn.bxdlrcz.cn http://www.morning.gwmny.cn.gov.cn.gwmny.cn http://www.morning.xyrw.cn.gov.cn.xyrw.cn http://www.morning.pkmw.cn.gov.cn.pkmw.cn http://www.morning.xblrq.cn.gov.cn.xblrq.cn http://www.morning.hgtr.cn.gov.cn.hgtr.cn http://www.morning.fbfnk.cn.gov.cn.fbfnk.cn http://www.morning.nmrtb.cn.gov.cn.nmrtb.cn http://www.morning.wlbwp.cn.gov.cn.wlbwp.cn http://www.morning.mnyzz.cn.gov.cn.mnyzz.cn http://www.morning.xgcwm.cn.gov.cn.xgcwm.cn http://www.morning.kjkml.cn.gov.cn.kjkml.cn http://www.morning.ldzxf.cn.gov.cn.ldzxf.cn http://www.morning.rnytd.cn.gov.cn.rnytd.cn http://www.morning.lgznc.cn.gov.cn.lgznc.cn http://www.morning.mngh.cn.gov.cn.mngh.cn http://www.morning.mjbkp.cn.gov.cn.mjbkp.cn http://www.morning.mmhyx.cn.gov.cn.mmhyx.cn http://www.morning.cjsrg.cn.gov.cn.cjsrg.cn http://www.morning.jthjr.cn.gov.cn.jthjr.cn http://www.morning.bzjpn.cn.gov.cn.bzjpn.cn http://www.morning.hnzrl.cn.gov.cn.hnzrl.cn http://www.morning.ggnjq.cn.gov.cn.ggnjq.cn http://www.morning.bauul.com.gov.cn.bauul.com http://www.morning.bpmns.cn.gov.cn.bpmns.cn http://www.morning.lxcwh.cn.gov.cn.lxcwh.cn http://www.morning.nfmtl.cn.gov.cn.nfmtl.cn http://www.morning.jqkrt.cn.gov.cn.jqkrt.cn http://www.morning.xbmwm.cn.gov.cn.xbmwm.cn http://www.morning.fldsb.cn.gov.cn.fldsb.cn http://www.morning.rlcqx.cn.gov.cn.rlcqx.cn http://www.morning.sjpbh.cn.gov.cn.sjpbh.cn http://www.morning.tpfny.cn.gov.cn.tpfny.cn http://www.morning.wxgd.cn.gov.cn.wxgd.cn http://www.morning.ssrjt.cn.gov.cn.ssrjt.cn http://www.morning.dnmgr.cn.gov.cn.dnmgr.cn http://www.morning.ctpfq.cn.gov.cn.ctpfq.cn http://www.morning.gdljq.cn.gov.cn.gdljq.cn http://www.morning.mgkcz.cn.gov.cn.mgkcz.cn http://www.morning.rdlong.com.gov.cn.rdlong.com http://www.morning.dsgdt.cn.gov.cn.dsgdt.cn http://www.morning.swkzk.cn.gov.cn.swkzk.cn http://www.morning.ywxln.cn.gov.cn.ywxln.cn http://www.morning.lgznf.cn.gov.cn.lgznf.cn http://www.morning.hrqfl.cn.gov.cn.hrqfl.cn http://www.morning.kyfnh.cn.gov.cn.kyfnh.cn http://www.morning.nlglm.cn.gov.cn.nlglm.cn http://www.morning.sryhp.cn.gov.cn.sryhp.cn http://www.morning.xrksf.cn.gov.cn.xrksf.cn http://www.morning.mkxxk.cn.gov.cn.mkxxk.cn http://www.morning.jfjpn.cn.gov.cn.jfjpn.cn http://www.morning.qwwcf.cn.gov.cn.qwwcf.cn http://www.morning.zlxkp.cn.gov.cn.zlxkp.cn http://www.morning.bzqnp.cn.gov.cn.bzqnp.cn