This article mainly studies the relevant content of StackTraceElement to obtain method call stack information, as detailed introduction and examples are as follows.
StackTrace (stack track) stores information on the method call stack. printStackTrace() commonly used in exception processing is essentially printing the stack information of exception calls.
StackTraceElement represents a method object in StackTrace (stack track), and its properties include the class name of the method, the method name, the file name, and the number of lines called.
public final class StackTraceElement implements java.io.Serializable {// Normally initialized by VM (public constructor added in 1.5) private String declareClass;private String methodName;private String fileName;private int lineNumber;}StackTraceElement is defined as final, which shows that it is not allowed to be inherited as a Java basic class.
There are two ways to obtain StackTraceElement, both of which return StackTraceElement array, which is the information of this stack.
1. Thread.currentThread().getStackTrace()
2. new Throwable().getStackTrace()
StackTraceElement array contains the content of StackTrace (stack track). By traversing it, you can get the calling process between methods, that is, you can get the current method and its caller's method name, number of call lines and other information.
public class TestClass {public static void main(String[] args) {new TestClass().methodA();}private void methodA(){System.out.println("-----------------------------------");methodB();}private void methodB(){System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- {StackTraceElement stackTraceElement=elements[i];String className=stackTraceElement.getClassName();String methodName=stackTraceElement.getMethodName();String fileName=stackTraceElement.getFileName();int lineNumber=stackTraceElement.getLineNumber();System.out.println("StackTraceElement array subscript i="+i+",fileName=" +fileName+",className="+className+",methodName="+methodName+",lineNumber="+lineNumber);}}}1. We can encapsulate a log library. When printing the target log, we can also print out the number of lines where the log is located through this call stack. In this way, we can quickly locate the log output line, and no longer search globally to search.
public static void d(String tag, String msg, Object... params) { StackTraceElement targetStackTraceElement = getTargetStackTraceElement(); Log.d(tag, "(" + targetStackTraceElement.getFileName() + ":" + targetStackTraceElement.getLineNumber() + ")"); Log.d(tag, String.format(msg, params));}2. If we write an SDK and hope that a certain method will be called in a fixed position, we can also check when this method is called to see if the calling position of this method is correct.
For example, it must be executed in Activity.onResume, PVSdk.onResume, so when we call the PVSdk.onResume method, we use the PVSdk.onResume method to detect whether this method is called in the Activity onResume method by obtaining the information of the call stack.
public class PVSdk {public static void onResume() {StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();Boolean result = false;for (StackTraceElement stackTraceElement: stackTrace) {String methodName = stackTraceElement.getMethodName();String className = stackTraceElement.getClassName();try {Boolean assignableFromClass = Class.forName(className).isAssignableFrom(Activity.class);if (assignableFromClass && "onResume".equals(methodName)) {result = true;break;}}catch (ClassNotFoundException e) {}}if (!result) throw new RuntimeException("PVSdk.onResume must in Activity.onResume");}}3. When we are conducting source code analysis, if we want to analyze the execution process of the entire code, we can obtain the information from the printing stack. This is quite useful when analyzing the source code.
The above is all the detailed explanation of this article about the StackTraceElement method call stack information example , I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!