前文讲述了类名或方法的应用之一调试源码,具体请参阅:Java学习-025-类名或方法名应用之一 -- 调试源码

此文主要讲述类名或方法应用之二统计分析,通过在各个方法中插桩(调用桩方法),获取方法的调用关系。通过调用关系,我们可以统计出被调用次数比较多的方法,同时也可以构建全系统调用关系链;通过操作重要业务流程,可以统计组成重要业务流程的主要方法,加强相应的单元测试、功能、安全、性能等方面的测试。对于软件产品质量控制存在非凡的意义。

下面构建的演示示例调用关系如下所示:

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()

源码比较简单,也比较容易理解,直接上码了,敬请各位小主参阅。若有不足之处,敬请大神指正,不胜感激!

GetClassMethodName.java 源码文件内容如下所示:

/**
* 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.ffp
* @version V1.0.0: Jsoup com.java.demo GetClassMethodName.java, 2015-8-13 10:58:39 Exp $
*/
public class GetClassMethodName extends InvokeClass{
/**
*
*
* @author Aaron.ffp
* @version V1.0.0: Jsoup com.java.demo GetClassMethodName.java test_invoke_cus_Exception, 2015-8-15 10:14:08 Exp $
*
*/
@Test
public void test_invoke_cus_Exception(){
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 源码文件内容如下所示:

/**
* 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 $
*
*/
public 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 invokeMethod_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.java 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 源码文件内容如下所示:

/**
* 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, 2015年8月13日 下午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.demo HelperReporter.java getInvokeInfoCFML, 2015-8-16 9:20:11 Exp $
*
* @return String
*/
public String getInvokeInfoCFML(){
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 will be return ArrayList which contains (package|filename|method name|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> invokeChildParent = new ArrayList<String>(); // add invoked method info
invokeChildParent.add(0, this.eot ? this.ste[0].getClassName() : this.ste[1].getClassName());
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 will be return ArrayList which contains (package|filename|method name|line number) of invoked method and invoke method.
*
* @author Aaron.ffp
* @version V1.0.0: Jsoup com.java.demo HelperReporter.java getInvokeChildParentByException, 2015-8-14 13:15:05 Exp $
*
* @param ste : new Exception().getStackTrace()
*
* @return ArrayList<String>
*/
public ArrayList<String> getInvokeChildParentByException(StackTraceElement[] ste){
ArrayList<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 will be return ArrayList which contains (package|filename|method name|line number) of invoked method and invoke method.
*
* @author Aaron.ffp
* @version V1.0.0: Jsoup com.java.demo HelperReporter.java getInvokeChildParentByThread, 2015-8-14 13:17:44 Exp $
*
* @param ste : Thread.currentThread().getStackTrace()
*
* @return ArrayList<String>
*/
public ArrayList<String> getInvokeChildParentByThread(StackTraceElement[] ste){
ArrayList<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 执行 GetClassMethodName.java 文件,输出结果如下所示:

[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|45 PASSED: test_invoke_cus_Exception
PASSED: test_invoke_cus_thread ===============================================
Default test
Tests run: 2, Failures: 0, Skips: 0
=============================================== ===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
=============================================== [TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 1 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@626b2d4a: 24 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@cac736f: 35 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@726f3b58: 259 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@aec6354: 522 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@1ee0005: 28 ms

从结果输出可以看出,调用关系同初始设置的调用关系图谱。我们可以将上述日志保存至数据库,然后进行数据分析,从而得出我们需要的数据。相信这对各位小主来说不是难事。

对于构建全系统调用关系链,只是绘制相应的图表有些困难,后续研究一下,敬请期待!

至此, Java学习-026-类名或方法名应用之二 -- 统计分析 顺利完结,希望此文能够给初学 Java 的您一份参考。

最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^

Java学习-026-类名或方法名应用之二 -- 统计分析基础的更多相关文章

  1. java中由类名和方法名字符串实现其调用【反射机制】

    js里通过eval()函数,在知道某个方法名是可以实现调用该方法,那么在java里边又怎么实现的呢? java里边是通过反射机制来实现,代码如下: import java.lang.reflect.M ...

  2. Java学习-024-获取当前类名或方法名二三文

    今天,看朋友编写程序,打印日志时,需要记录当前类的类名以及当前方法的方法名,我发现 TA 将类名或者方法名直接写死在了代码中...虽说这样可以实现记录类名和方法名,但是当有特殊情况需要修改类名或者方法 ...

  3. Java 中 方法名或类名 变更 同时 更新 所有引用的 类名或方法名 的解决方案

    选中 类名,或属性名  Ctrl + 1  然后选择 理新当前文件,还是更新整个工作空间,然后修改对应的类名或方法名  回车即可. 如果.有SVN 版本在控制着,则 会提示,然后把对应的文件 锁定 再 ...

  4. Android日志打印类LogUtils,能够定位到类名,方法名以及出现错误的行数并保存日志文件

    Android日志打印类LogUtils,能够定位到类名,方法名以及出现错误的行数并保存日志文件 在开发中,我们常常用打印log的方式来调试我们的应用.在Java中我们常常使用方法System.out ...

  5. java学习笔记5--类的方法

    接着前面的学习: java学习笔记4--类与对象的基本概念(2) java学习笔记3--类与对象的基本概念(1) java学习笔记2--数据类型.数组 java学习笔记1--开发环境平台总结 本文地址 ...

  6. Java学习笔记4(方法)

    方法和c++中的函数类似,区别在于java的方法定义不限位置,而c++中的定义在主函数后面的函数调用前要声明: 求矩形面积方法示例: public class MethodDemo{ public s ...

  7. php 类名和方法名相同(构造函数)

    //php 5.6class father{ public function __construct() { echo __METHOD__; }} class son extends father{ ...

  8. java学习(五)--- 方法

    方法的定义 修饰符 返回值类型 方法名(参数类型 参数名){ ... 方法体 ... return 返回值; } 注意:非void方法必须有返回值 方法重载: 可以声明方法相同,但是参数类型不同的方法 ...

  9. Java学习笔记17---成员方法的重载与重写

    重载是指,一个类中定义了一个成员方法后,通过修改参数个数.参数类型或参数顺序,重新实现该方法,则这两个方法互为对方的重载方法. 重写是指,子类重新实现父类的成员方法. 重载后的方法,与原方法相比: ( ...

随机推荐

  1. MongoBD解决没有自动增长ID 的问题

    Sequence Numbers:序列号传统的数据库中,通常用一个递增的序列来提供主键,在 MongoDB中用 ObjectId 的来代替,我们可以通过如下的函数来获取主键 function coun ...

  2. iOS开发项目之二 [ App appicon与启动图]

    *appicon尺寸 *不是所有的appicon都是需要配置 *尽量不要透明--(透明的话,会在桌面显示成背景黑色) 1 如果没有配置5s的启动图,回去往下找,找到4s之后,会把界面以4s启动图的大小 ...

  3. VTK 5.10.1 VS2010 Configuration 配置

    Download VTK 5.10.1 Download VS2010 Download CMake 3.2.0 I assume you've already installed VS2010 an ...

  4. 利用Python的三元表达式解决Odoo中工资条中城镇、农村保险的问题

    Python中没有像C#中有三元表达式 A?B:C 但在python中可以通过 A if condition else B 的方式来达到同样的效果. 例如 : 1 if True else 0 输出 ...

  5. JSP编程-步步为营

    [第一个JSP举例] header.jsp <%@ page language="java" contentType="text/html; charset=utf ...

  6. OpenCV学习笔记——滑动条开关

    由于opencv库中并没有专门为开关而设的函数,可以用滑动条做开关 代码: #include<highgui.h> #include<cv.h> int g_switch_va ...

  7. ionic 写一个五星评价(非指令)

    Controller里的代码: .controller('evaluateCtrl', function($scope, $state, $stateParams, $ionicPopup,$ioni ...

  8. CSS3系列:流式(弹性)布局(flex布局)

    我的新伸缩盒子.http://www.cnblogs.com/leee/p/5533436.html

  9. PHP 错误与异常 笔记与总结(14 )记录和发送异常信息

    当发生异常时,把异常信息记录到日志文件中: <?php header('content-type:text/html; charset=utf-8'); class LogException e ...

  10. php中ajax跨域请求---小记

    php中ajax跨域请求---小记   前端时间,遇到的一个问题,情况大约是这样: 原来的写法: 前端js文件中: $.ajax({ type:'get', url:'http://wan.xxx.c ...