This was learned during Alcatel-Lucent's internship.
I was always familiar with several Java API packages, such as lang and util. I thought I was familiar with it, but now I know that I am still far away. You need to collect the related knowledge of each package online and expand it.
For example, if StackTraceElement is used to obtain the call stack information of a method, you can view the code flow for better testing during development, and trace the beginning and end of each method, in this case,
The Code is as follows:
[Java]
Public class Trace {
Public void methodStart (){
StackTraceElement thisMethodStack = (new Exception (). getStackTrace () [1];
System. out. println ("[" + thisMethodStack. toString () + "] ----- MethodStart ");
}
Public void methodEnd (){
StackTraceElement thisMethodStack = (new Exception (). getStackTrace () [1];
System. out. println ("[" + thisMethodStack. toString () + "] ----- MethodEnd ");
}
Public void trace (String string ){
StackTraceElement thisMethodStack = (new Exception (). getStackTrace () [1];
System. out. println ("[" + thisMethodStack. toString () + "]" + string );
}
}
The preceding print statement can be replaced with traceAPI of different companies.
The following is a test:
[Java]
Public class TraceTest {
Trace trace = new Trace ();
Public static void main (String [] args ){
New TraceTest (). test ();
}
Private void test (){
Trace. methodStart ();
System. out. println ("111 \ ndddd \ n ");
Trace. trace ("this the trace ");
Trace. methodEnd ();
}
}
One of them is the top trace class. I wanted to mention the thisMethodStack statement that all the three methods have before. After testing, I learned that the thisMethodStack statement can only be put in it. In this case, in the output result, the correct number of trace rows is displayed. For more information, see []. The method name and number of rows in the class name are displayed.
[Java]
[TraceTest. test (TraceTest. java: 10)] ----- MethodStart
111
Dddd
[TraceTest. test (TraceTest. java: 12)] this the trace
[TraceTest. test (TraceTest. java: 13)] ----- MethodEnd
Another point is that for statements
[Java]
StackTraceElement thisMethodStack = (new Exception (). getStackTrace () [1]; if it is placed at the beginning of the trace class;
[Java]
In the end, it must be 1 in the brackets. If it is changed to 0, the number of rows in the main method that calls the method is printed. If it is changed to 2, after the method call is completed, which row is returned. For example:
[Java] view plaincopy
Otherwise, if it is 0: Then the number of rows in each method of the trace statement is returned. If it is 2
[Java]
[TraceTest. main (TraceTest. java: 6)] ----- MethodStart
111
Dddd
[TraceTest. main (TraceTest. java: 6)] this the trace
[TraceTest. main (TraceTest. java: 6)] ----- MethodEnd
[Java]
Is the place after the return.
[Java]
A common method.
[Java]
Author: Allen_Zhao_2012