The J2SE basic compaction series uses StackTraceElement encapsulation to print debugging code!

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.