Use Cases
Sometimes, we know that a specific Java API is used. We want to know where the API is used.
The intuitive method is to search the source code, find all the places in use, and mark (such as logging ). Then, view the tag in the output log to know where the API is called. The disadvantage of this method is obvious:
1. If there are many places to call this method, you need to modify many
2. If you want to see where the method to call this method is used, you need to modify it more. It's a nightmare.
The better way is to use the java. Lang. throwable feature, which records the call stack of the method. We can use it to display the call of the method and display the entire call sequence. The specific method is to generate a throwable object in the API implementation, and then run it out:
Public int XXX (XXX) {throwable OBJ = new throwable ("XXX checker"); throw OBJ ;...}
In this way, when the method is called, the execution is terminated and an exception is thrown. We can see from the log the complete call stack of the change method.
If you do not want to terminate the program, simply do not deserve the exception and print it out:
Public int XXX (XXX) {try {throwable OBJ = new throwable ("XXX checker"); throw OBJ; catche (throwable e) {log. E (TAG, "XX Call trace" + E. printtrace ());}...}
However, if the API we are concerned about is a local method, this method cannot be used. Because the local method does not have Java implementation bodies. The solution is to find the JNI implementation and run exceptions in the JNI implementation to obtain the call track. For example, we want to know
TBD