Here's how to get the class name and method name, see: Java Learning -024-Gets the current class name or method name two or three article .
Usually in the application development, debugging or viewing the method in which file is called the current file of this method, so in the actual application needs to obtain the corresponding package name, class name, method name, number of rows, so that quickly locate, and the number of times the statistical method is called, generate class method relationship chain.
Believe that love to delve into the small masters, through the previous article, has learned how to get the main melody method, from the method of adjustment. Then I directly on the code, please see the small master, if there are shortcomings, please the great god, thank you!
Getclassmethodname.java source content is as follows:
/** * AARON.FFP Inc. * Copyright (c) 2004-2015 all rights Reserved. */package Com.java.demo;import org.testng.annotations.test;/** * Get Information of class and method * * @author AARON.FF P * @version v1.0.0:jsoup Com.java.demo Getclassmethodname.java, 2015-8-13 10:58:39 Exp $ */public Class Getclassmethodna Me extends invokeclass{/** * Debug Code Demo Use Exception * * @author AARON.FFP * @version V1.0. 0:jsoup Com.java.demo Getclassmethodname.java test_invokemethod_001, 2015-8-14 12:50:32 EXP $ * */@Test PU Blic void test_invokemethod_001 () {System.out.println ("====== Main Invoke Method:GetClassMethodName.test_invokeM ethod_001 () =========================== "); THIS.INVOKEMETHOD_001 (); }/** * Debug code Demo Use Thread * * @author AARON.FFP * @version v1.0.0:jsoup Com.java.demo Ge Tclassmethodname.java test_invokemethod_002, 2015-8-14 12:51:33 EXP $ * */@Test public void Test_invokemetHod_002 () {System.out.println ("====== Main Invoke Method:GetClassMethodName.test_invokeMethod_002 () ============ =============== "); This.invokemethod_002 (); }}
Invokeclass.java source content is as follows:
/** * AARON.FFP Inc. * Copyright (c) 2004-2015 all rights Reserved. */package Com.java.demo;import com.demo.helperreporter;/** * Invoked class * * @author AARON.FFP * @version v1.0.0:jsoup Com.java.demo Invokeclass.java, 2015-8-14 01:15:12 Exp $ */public Class Invokeclass extends helperreporter{/** * by Exception * * @author AARON.FFP * @version v1.0.0:jsoup com.java.demo Invokeclass.java invokemethod_001, 2015-8-14 01:15:51 EXP $ * */public void invokemethod_001 () {stacktraceelement[] ste = new Exception (). Getstacktrace (); System.out.println ("====== invoked Method:InvokeClass.invokeMethod_001 () ==========================="); for (int i = 0; i < ste.length; i++) {if (i > 1) {break; } System.out.println ("ste[" + i + "].getclassname ()" + "\ T" + ste[i].getclassname () + "\ n" + "ste[" + i + "].getfilename ()" + "\ T" + STe[i].getfilename () + "\ n" + "ste[" + i + "].getmethodname ()" + "\ T" + ste[i].getmethodname ( ) + "\ n" + "ste[" + i + "].getlinenumber ()" + "\ T" + ste[i].getlinenumber () + "\ n" ); }}/** * by Thread * * @author AARON.FFP * @version v1.0.0:jsoup Com.java.demo Invokeclass.ja VA invokemethod_002, 2015-8-14 01:16:19 EXP $ * * */public void invokemethod_002 () {stacktraceelement[] s Te = Thread.CurrentThread (). Getstacktrace (); System.out.println ("====== invoked Method:InvokeClass.invokeMethod_002 () ==========================="); for (int i = 0; i < ste.length; i++) {if (I < 1 | | i > 2) {continue; } System.out.println ("ste[" + i + "].getclassname ()" + "\ T" + ste[i].getclassname () + "\ n" + "ste[" + i + "].getfilename ()" + "\T "+ ste[i].getfilename () +" \ n "+" ste["+ i +"].getmethodname () "+" \ T "+ Ste[i].getmetho Dname () + "\ n" + "ste[" + i + "].getlinenumber ()" + "\ T" + ste[i].getlinenumber () + "\ n" ); } }}
The source run results are as follows:
In the actual application, the small master reference this method to make the corresponding changes can also be encapsulated as a method of the relevant information, customized for the test framework, such as the output, note the Exception or Thread to obtain relevant information when the difference (this part of the follow-up will do some simple examples, for the small master to see, Please look forward to)!
At this point, Java learning -025-class name or method name application-Debugging the source of the successful end, I hope this article can give beginners Java you a reference.
Finally, very grateful to the pro-stop, I hope this article can be pro helpful. Warmly welcome the kiss to discuss together and progress together. Thank you so much! ^_^
Java Learning -025-class name or method name application-Debug Source