Many of the key codes in the Android source code are implemented in C + +, and Java is called by JNI, and you often see code like this in Java:
Static native Thread CurrentThread ();
How to find its corresponding C + + implementation According to the method name, there are two methods.
Let's start with an example of Java code Vmthread.java:
PackageJava.lang;classvmthread {thread thread; intVmdata; Vmthread (thread t) {thread=T; }native Static voidCreate (Thread T,LongstackSize); Static nativeThread CurrentThread(); Static native Booleaninterrupted (); Static native voidSleep (LongmsecintNsecthrowsinterruptedexception; Static native voidyield (); native voidinterrupt (); native Booleanisinterrupted ();
......}
We want to find the implementation of the CurrentThread method.
Method One:
As the Android source code for each native implementation will write a Java method name and C + + method Name Mapping list, so we directly search this list content.
[email protected] $ grep-rns '"CurrentThread"'./*
./art/compiler/dex/quick/dex_file_method_inliner.cc:108: "CurrentThread",//Knamecachecurrentthread
match to a binary file./DALVIK/VM/NATIVE/.JAVA_LANG_VMTHREAD.CPP.SWP
./dalvik/vm/native/java_lang_vmthread.cpp:241: { "CurrentThread", "() Ljava/lang/thread;",
./external/android-mock/tests/com/google/android/testing/mocking/androidmockgeneratortest.java:249:method method = Thread.class.getMethod ("CurrentThread");
./external/android-mock/tests/com/google/android/testing/mocking/androidmockgeneratortest.java:407:method method = Thread.class.getMethod ("CurrentThread");
You can see the information about the CurrentThread method found in the file./dalvik/vm/native/java_lang_vmthread.cpp, followed by () ljava/lang/ The Thread represents the return value of this method.
Enter Java_lang_vmthread.cpp this file to see:
- /* - * Java.lang.VMThread + */ -#include "Dalvik.h" +#include "Native/internalnativepriv.h" A at - /* - * static void Create (Thread T, long stacksize) - * - * This is eventually called as a result of Thread.Start (). - * in * Throws an exception on failure. - */ to Static voidDalvik_java_lang_vmthread_create (Constu4* args, jvalue*PResult) + { -object* threadobj = (object*) args[0]; theS8 stackSize = Get_arg_long (args, 1); * $ /*copying collector would pin threadobj for us since it is an argument*/Panax NotoginsengDvmcreateinterpthread (Threadobj, (int) stackSize); - return_void (); the } + A /* the * static Thread CurrentThread () + */ - Static voidDalvik_java_lang_vmthread_currentthread (Constu4*args, $jvalue*PResult) $ { - unused_parameter (args); - theReturn_ptr (Dvmthreadself ()threadobj); - }Wuyi
......
237 238 ConstDalviknativemethod dvm_java_lang_vmthread[] = {239{"Create", "(Ljava/lang/thread; J) V ", - Dalvik_java_lang_vmthread_create},241{"CurrentThread", "() Ljava/lang/thread;",242 dalvik_java_lang_vmthread_currentthread },243{"GetStatus", "() I",244 Dalvik_java_lang_vmthread_getstatus},245{"Holdslock", "(Ljava/lang/object;) Z",246 Dalvik_java_lang_vmthread_holdslock},247{"Interrupt", "() V",248 Dalvik_java_lang_vmthread_interrupt},249{"Interrupted", "() Z", - dalvik_java_lang_vmthread_interrupted},251{"Isinterrupted", "() Z",252 dalvik_java_lang_vmthread_isinterrupted},253{"Namechanged", "(ljava/lang/string;) V",254 dalvik_java_lang_vmthread_namechanged},255{"SetPriority", "(I) V", the dalvik_java_lang_vmthread_setpriority},257{"Sleep", "(JI) V",258 Dalvik_java_lang_vmthread_sleep},259{"Yield", "() V",260 Dalvik_java_lang_vmthread_yield},261 {null, NULL, NULL},262};
The No. 242 line in the source code to find the corresponding name, marked in red, its implementation is on line 44th.
This method is not very accurate, depends on the experience to determine whether the code is to find the search, the next method can be more accurate search.
Method Two:
or find Vmthread.java CurrentThread function, find more will find, C + + name is generally the package name + class name + method name, such as CurrentThread C + + name will certainly contain "Java_lang_vmthread_ CurrentThread", so you can search directly.
Grep-rns "java_lang_vmthread_currentthread"./*
Find the C + + implementation of the native function in Java code in the Android source