1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
| ------------前置代码-------
typedef struct { char *name; char *signature; void *fnPtr; } JNINativeMethod;
struct JNIEnv_;
#ifdef __cplusplus
typedef JNIEnv_ JNIEnv; #else
typedef const struct JNINativeInterface_ *JNIEnv; #endif
-------------正式代码开始------------ #define OBJ "Ljava/lang/Object;"
static JNINativeMethod methods[] = { {"currentTimeMillis", "()J", (void *)&JVM_CurrentTimeMillis}, {"nanoTime", "()J", (void *)&JVM_NanoTime}, {"arraycopy", "(" OBJ "I" OBJ "II)V", (void *)&JVM_ArrayCopy}, };
#undef OBJ
JNIEXPORT void JNICALL Java_java_lang_System_registerNatives(JNIEnv *env, jclass cls) { (*env)->RegisterNatives(env, cls, methods, sizeof(methods)/sizeof(methods[0])); }
-----------------其他代码补充----------
struct JNINativeInterface_ { ..... jint (JNICALL *RegisterNatives) (JNIEnv *env, jclass clazz, const JNINativeMethod *methods, jint nMethods); ...... }
struct JNIEnv_ { const struct JNINativeInterface_ *functions; ....... #ifdef __cplusplus jint RegisterNatives(jclass clazz, const JNINativeMethod *methods, jint nMethods) { return functions->RegisterNatives(this,clazz,methods,nMethods); } ....... #endif }
.... #include "runtime/interfaceSupport.inline.hpp" ..... struct JNINativeInterface_ jni_NativeInterface = { .... jni_RegisterNatives, ...... }
JNI_ENTRY(jint, jni_RegisterNatives(JNIEnv *env, jclass clazz, const JNINativeMethod *methods, jint nMethods)) JNIWrapper("RegisterNatives"); HOTSPOT_JNI_REGISTERNATIVES_ENTRY(env, clazz, (void *) methods, nMethods); jint ret = 0; DT_RETURN_MARK(RegisterNatives, jint, (const jint&)ret);
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz));
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);
TempNewSymbol name = SymbolTable::probe(meth_name, meth_name_len); TempNewSymbol signature = SymbolTable::probe(meth_sig, (int)strlen(meth_sig));
if (name == NULL || signature == NULL) { ResourceMark rm; stringStream st; st.print("Method %s.%s%s not found", k->external_name(), meth_name, meth_sig); THROW_MSG_(vmSymbols::java_lang_NoSuchMethodError(), st.as_string(), -1); }
bool res = Method::register_native(k, name, signature, (address) methods[index].fnPtr, THREAD); if (!res) { ret = -1; break; } } return ret; JNI_END
........ bool Method::register_native(Klass* k, Symbol* name, Symbol* signature, address entry, TRAPS) { Method* method = k->lookup_method(name, signature); if (method == NULL) { ResourceMark rm(THREAD); stringStream st; st.print("Method '"); print_external_name(&st, k, name, signature); st.print("' name or signature does not match"); THROW_MSG_(vmSymbols::java_lang_NoSuchMethodError(), st.as_string(), false); } if (!method->is_native()) { method = find_prefixed_native(k, name, signature, THREAD); if (method == NULL) { ResourceMark rm(THREAD); stringStream st; st.print("Method '"); print_external_name(&st, k, name, signature); st.print("' is not declared as native"); THROW_MSG_(vmSymbols::java_lang_NoSuchMethodError(), st.as_string(), false); } }
if (entry != NULL) { method->set_native_function(entry, native_bind_event_is_interesting); } else { method->clear_native_function(); } if (log_is_enabled(Debug, jni, resolve)) { ResourceMark rm(THREAD); log_debug(jni, resolve)("[Registering JNI native method %s.%s]", method->method_holder()->external_name(), method->name()->as_C_string()); } return true; }
|