This article describes one of the application of the class name or method debug source, see:Java Learning -025-class name or method name application-Debug source code
This paper mainly describes the two statistical analysis of the application of the class name or method, and obtains the call relationship of the method by inserting piles in each method (calling the pile method). By invoking the relationship, we can count the methods which are called more times, and we can construct the whole system call relation chain, and by operating the important business process, we can count the main methods of composing important business process, and strengthen the test of unit test, function, security, performance and so on. There is an extraordinary significance for the quality control of software products.
The following example builds the demo call relationship as follows:
Getclassmethodname.test_invoke_cus_exception () | | -Invokeclass.invoke_cus_exception () | | --invokeclass.invokemethod_001 () getclassmethodname.test_invoke_cus_thread () | | -Invokeclass.invoke_cus_thread () | | --invokeclass.invokemethod_002 ( ) | | | | --invokeclass.invokemethod_003 ( ) | | | | -Invokeclass.getinvokeclass () | | -Invokeclass.getinvokeclass () | | -Invokeclass.invoke_cus_exception () | | -INVOKECLASS.INVOKEMETHOD_001 ()
Source code is relatively simple, it is easier to understand, directly on the code, please refer to the small master. If there is any deficiency, please ask the great God to correct me, I appreciate it!
The contents of the Getclassmethodname.java source file are 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{/** * * * @author AARON.FFP * @version v1.0.0:jsoup Com.java.demo getclassme Thodname.java test_invoke_cus_exception, 2015-8-15 10:14:08 EXP $ * */@Test public void Test_invoke_cus_exc Eption () {System.out.println ("====== Main Invoke Method:GetClassMethodName.test_invoke_cus_Exception () ========= ================== "); This.invoke_cus_exception (); System.out.println ("\ n"); }/** * * * @author AARON.FFP * @version v1.0.0:jsoup com.java.demo Getclassmethodname.java test _invoke_cus_thread, 2015-8-15 10:13:25 EXP $ * */@Test public void test_iNvoke_cus_thread () {System.out.println ("====== Main Invoke Method:GetClassMethodName.test_invoke_cus_thread () = = ========================= "); This.invoke_cus_thread (); System.out.println ("\ n"); }}
invokeclass.java source file are as follows:
/** * AARON.FFP Inc. * Copyright (c) 2004-2015 all rights Reserved. */package com.java.demo;/** * 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{/** * * * @author AARON.FFP * @version v1.0.0:jsoup Com.java.demo invokeclass.java invoke_cus_thread, 2015-8-15 10:14:37 EXP $ * */P ublic void Invoke_cus_thread () {This.setste (Thread.CurrentThread (). Getstacktrace (), false); System.out.println (This.getinvokeinfocfml ()); This.invokemethod_002 (); This.getinvokeclass (); This.invoke_cus_exception (); }/** * * * @author AARON.FFP * @version v1.0.0:jsoup com.java.demo Invokeclass.java invoke_cus_ Exception, 2015-8-15 10:14:55 EXP $ * */public void invoke_cus_exception () {This.setste (New Exception () . Getstacktrace (), true); System.out.println (This.getinvokeinfocfml ()); THIS.INVOKEMETHOD_001 (); }/** * 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 (); This.setste (Ste, True); System.out.println (This.getinvokeinfocfml ()); }/** * by Thread * * @author AARON.FFP * @version v1.0.0:jsoup Com.java.demo Invokeclass.java in vokemethod_002, 2015-8-14 01:16:19 EXP $ * */public void invokemethod_002 () {stacktraceelement[] ste = Thread.CurrentThread (). Getstacktrace (); This.setste (Ste, false); System.out.println (This.getinvokeinfocfml ()); This.invokemethod_003 (); }/** * Invoke other method which belong to the other class * * @author Aaron. FFP * @version v1.0.0:jsoup com.java.demo invokeclass.java invokemethod_003, 2015-8-15 10:16:19 EXP $ * */ public void invokemethod_003 () {stacktraceelement[] ste = Thread.CurrentThread (). Getstacktrace (); This.setste (Ste, false); System.out.println (This.getinvokeinfocfml ()); This.getinvokeclass (); }/** * Invoked method * * @author AARON.FFP * @version v1.0.0:jsoup Com.java.demo Invokeclass.ja VA getinvokeclass, 2015-8-15 10:19:19 EXP $ * * */public void Getinvokeclass () {stacktraceelement[] ste = Thread.CurrentThread (). Getstacktrace (); This.setste (Ste, false); System.out.println (This.getinvokeinfocfml ()); }}
helperreporter.java source file are as follows:
/** * AARON.FFP Inc. * Copyright (c) 2004-2015 all rights Reserved. */package com.java.demo;import java.text.simpledateformat;import Java.util.arraylist;import java.util.Date;import org.testng.log4testng.logger;/** * * @author AARON.FFP * @version v1.0.0:jsoup com.demo Helperreporter.java, August 13, 2015 PM 2:52:23 EXP $ */public class Helperreporter {private Logger Logger = Logger.getlogger (This.getclass ()); Private stacktraceelement[] Ste; Store the type of stacktraceelement. True Exception, False Thread private Boolean EOT = true; public void Setste (stacktraceelement[] Ste, Boolean eot) {this.ste = ste; This.eot = EOT; }/** * Get string about invoke info, * * @author AARON.FFP * @version v1.0.0:jsoup Com.java.dem o Helperreporter.java getinvokeinfocfml, 2015-8-16 9:20:11 Exp $ * * @return String */public string Getin Vokeinfocfml () {String invokeinfocfml = ""; try {thread.sleep (5); } catch (Interruptedexception e) {logger.error ("", e); } String current_time = (new SimpleDateFormat ("Yyyymmdd-hhmmss-sss")). Format (new Date ()); arraylist<string> ICP = this.getinvokechildparent (); INVOKEINFOCFML = "[" + Current_time + "]" + icp.get (0) + "|" + icp.get (1) + "|" + Icp.get (2) + "|" + Icp.get (3) + "| Invoked by | "+ Icp.get (4) +" | "+ Icp.get (5) +" | "+ icp.get (6) +" | "+ icp.get (7); return INVOKEINFOCFML; }/** * Get information of Invoke relation by stacktraceelement[] which caused by new Exception (). Getstacktrace () or Thread.CurrentThread (). Getstacktrace () * It'll be Return ArrayList which contains (Package|filename|method Nam E|line number) of invoked method and invoke method. * * @author AARON.FFP * @version v1.0.0:jsoup com.java.demo Helperreporter.java getinvokechildparent, 2015-8-16 9:15:05 EXP $ * * @return Arraylist<string> */Public arraylist<string> getinvokechildparent () {arraylist<string> invoke Childparent = new arraylist<string> (); Add invoked method info Invokechildparent.add (0, This.eot? This.ste[0].getclassname (): This.ste[1].getclassnam E ()); Invokechildparent.add (1, This.eot this.ste[0].getfilename (): This.ste[1].getfilename ()); Invokechildparent.add (2, This.eot this.ste[0].getmethodname (): This.ste[1].getmethodname ()); Invokechildparent.add (3, "" + (This.eot?) This.ste[0].getlinenumber (): This.ste[1].getlinenumber ())); Add Invoke Method Info Invokechildparent.add (4, This.eot? This.ste[1].getclassname (): This.ste[2].getclassname ()); Invokechildparent.add (5, This.eot this.ste[1].getfilename (): This.ste[2].getfilename ()); Invokechildparent.add (6, This.eot this.ste[1].getmethodname (): This.ste[2].getmethodname ()); Invokechildparent.add (7, "" + (This.eot this.ste[1].getlinenumber (): This.ste[2].getlinenumber ())); return invokechildparent; }/** * Get information of Invoke relation by new Exception (). Getstacktrace (). * It'll be Return ArrayList which contains (Package|filename|method name|line number) of invoked method and invoke Metho d. * * @author AARON.FFP * @version v1.0.0:jsoup com.java.demo Helperreporter.java getinvokechildparentbyexc Eption, 2015-8-14 13:15:05 Exp $ * * @param ste:new Exception (). Getstacktrace () * * @return arraylist& Lt string> */Public arraylist<string> getinvokechildparentbyexception (stacktraceelement[] ste) {Array list<string> invokechildparent = new arraylist<string> (); Add invoked method info Invokechildparent.add (0, Ste[0].getclassname ()); Invokechildparent.add (1, Ste[0].getfilename ()); Invokechildparent.add (2, Ste[0].getmethodname ()); InvokechIldparent.add (3, "" + Ste[0].getlinenumber ()); Add Invoke Method Info Invokechildparent.add (4, Ste[1].getclassname ()); Invokechildparent.add (5, Ste[1].getfilename ()); Invokechildparent.add (6, Ste[1].getmethodname ()); Invokechildparent.add (7, "" + Ste[1].getlinenumber ()); return invokechildparent; }/** * Get information of Invoke relation by Thread.CurrentThread (). Getstacktrace (). * It'll be Return ArrayList which contains (Package|filename|method name|line number) of invoked method and invoke Metho d. * * @author AARON.FFP * @version v1.0.0:jsoup com.java.demo Helperreporter.java getinvokechildparentbythr EAD, 2015-8-14 13:17:44 Exp $ * * @param ste:Thread.currentThread (). Getstacktrace () * * @return Arrayl ist<string> */Public arraylist<string> getinvokechildparentbythread (stacktraceelement[] ste) {A rraylist<string> invokechildparent = new ARraylist<string> (); Add invoked method info Invokechildparent.add (0, Ste[1].getclassname ()); Invokechildparent.add (1, Ste[1].getfilename ()); Invokechildparent.add (2, Ste[1].getmethodname ()); Invokechildparent.add (3, "" + Ste[1].getlinenumber ()); Add Invoke Method Info Invokechildparent.add (4, Ste[2].getclassname ()); Invokechildparent.add (5, Ste[2].getfilename ()); Invokechildparent.add (6, Ste[2].getmethodname ()); Invokechildparent.add (7, "" + Ste[2].getlinenumber ()); return invokechildparent; }}
[TestNG] Running:c:\users\ \appdata\local\temp\testng-eclipse--1803609229\testng-customsuite.xml ====== Main Invoke Method:GetClassMethodName.test_invoke_cus_Exception () =========================== [20150817-235837-968] com.java.demo.invokeclass| invokeclass.java|invoke_cus_exception|41| Invoked by |com.java.demo.getclassmethodname| GETCLASSMETHODNAME.JAVA|TEST_INVOKE_CUS_EXCEPTION|27[20150817-235837-974] com.java.demo.invokeclass| invokeclass.java|invokemethod_001|56| Invoked by |com.java.demo.invokeclass| invokeclass.java|invoke_cus_exception|45 ====== Main Invoke Method:GetClassMethodName.test_invoke_cus_thread () = = = ======================= [20150817-235837-983] com.java.demo.invokeclass| invokeclass.java|invoke_cus_thread|22| Invoked by |com.java.demo.getclassmethodname| GETCLASSMETHODNAME.JAVA|TEST_INVOKE_CUS_THREAD|43[20150817-235837-988] com.java.demo.invokeclass| invokeclass.java|invokemethod_002|71| Invoked by |com.java.demo.invokeclass| invokeclass.java|invoke_cus_thread|26[20150817-235837-993] com.java.demo.invokeclass| invokeclass.java|invokemethod_003|88| Invoked by |com.java.demo.invokeclass| INVOKECLASS.JAVA|INVOKEMETHOD_002|77[20150817-235837-998] com.java.demo.invokeclass| invokeclass.java|getinvokeclass|105| Invoked by |com.java.demo.invokeclass| INVOKECLASS.JAVA|INVOKEMETHOD_003|94[20150817-235838-003] com.java.demo.invokeclass| invokeclass.java|getinvokeclass|105| Invoked by |com.java.demo.invokeclass| INVOKECLASS.JAVA|INVOKE_CUS_THREAD|28[20150817-235838-008] com.java.demo.invokeclass| invokeclass.java|invoke_cus_exception|41| Invoked by |com.java.demo.invokeclass| INVOKECLASS.JAVA|INVOKE_CUS_THREAD|30[20150817-235838-013] com.java.demo.invokeclass| invokeclass.java|invokemethod_001|56| Invoked by |com.java.demo.invokeclass| invokeclass.java|invoke_cus_exception|45passed:test_invoke_cus_exceptionpassed:test_invoke_cus_thread========= ====================================== Default test Tests run:2, failures:0, skips:0==============================================================================================default suitetotal tests Run:2, failures:0, Skips : 0===============================================[testng] Time taken by [Failedreporter passed=0 failed=0 skipped=0]: 1 ms[testng] Time taken by [email protected]: + ms[testng] Time taken by [email protected]: ms[testng] Time Taken by [email protected]: 259 ms[testng] Time taken by [email protected]: 522 ms[testng] Time taken by [email& Nbsp;protected]: ms
As you can see from the result output, the call relationship graph of the invocation relationship is the same as the initial setting. We can save the above logs to the database and then analyze the data to get the data we need. I believe this is not difficult for the small master.
For building system-wide call chain, it is difficult to draw the corresponding chart, follow-up study, please look forward to!
At this point, Java learning -025-class name or method name application of the second-statistical analysis of the smooth 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 II--statistical Analysis Foundation