Jacoco项目主页:http://www.eclemma.org/jacoco/
本文地址:JacocoAnalyse
Content
调用的开源框架
Ant
开发文档:http://www.jacoco.org/jacoco/trunk/doc/ant.html
ASM
项目主页:http://asm.ow2.org/
ASM分析:ASM Analyse
测试源码对比
插入前源码
public class Hello
{
public Hello()
{
int rand = (int)(Math.random() * 100.0D);
if (rand % 2 == 0)
System.out.println("Hi,0");
else {
System.out.println("Hi,1");
}
System.out.println("End");
}
}
public class HelloTest
{
public static void main(String[] args)
{
Hello h = new Hello();
}
}
插入后源码
public class Hello
{
public Hello()
{
arrayOfBoolean[0] = true;
int rand = (int)(Math.random() * 100.0D);
if (rand % 2 == 0) { arrayOfBoolean[1] = true;
System.out.println("Hi,0"); arrayOfBoolean[2] = true;
} else {
System.out.println("Hi,1"); arrayOfBoolean[3] = true;
}
System.out.println("End");
arrayOfBoolean[4] = true;
}
}
public class HelloTest
{
public HelloTest()
{
arrayOfBoolean[0] = true;
}
public static void main(String[] arg0) {
boolean[] arrayOfBoolean = $jacocoInit(); Hello h = new Hello();
arrayOfBoolean[1] = true;
}
}
插入前字节码
F:\Jacoco\target\classes>javap -c Hello
Compiled from "Hello.java"
public class Hello {
public Hello();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: invokestatic #2 // Method java/lang/Math.random:()D
7: ldc2_w #3 // double 100.0d
10: dmul
11: d2i
12: istore_1
13: iload_1
14: iconst_2
15: irem
16: ifne 30
19: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream;
22: ldc #6 // String Hi,0
24: invokevirtual #7 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
27: goto 38
30: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream;
33: ldc #8 // String Hi,1
35: invokevirtual #7 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
38: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream;
41: ldc #9 // String End
43: invokevirtual #7 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
46: return
}
插入后字节码
F:\Jacoco\target\classes-instr>javap -c Hello
Compiled from "Hello.java"
public class Hello {
public Hello();
Code:
0: invokestatic #49 // Method $jacocoInit:()[Z
3: astore_1
4: aload_0
5: invokespecial #1 // Method java/lang/Object."<init>":()V
8: aload_1
9: iconst_0
10: iconst_1
11: bastore
12: invokestatic #2 // Method java/lang/Math.random:()D
15: ldc2_w #3 // double 100.0d
18: dmul
19: d2i
20: istore_2
21: iload_2
22: iconst_2
23: irem
24: ifne 46
27: aload_1
28: iconst_1
29: iconst_1
30: bastore
31: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream;
34: ldc #6 // String Hi,0
36: invokevirtual #7