Btrace检测list参数、结果

使用Btrace时我希望监控到传递的list参数值,这时调用println(str(list1));这样的语句就可以打印list中的所有值,代码如下:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * TODO Comment of TestList
 */
public class TestList {
    public static List<String> mergeList(List<String> list1, List<String> list2) {
        List<String> list = new ArrayList<String>();
        if (list1 != null && list1.size() > 0) {
            for (String str : list1) {
                list.add(str);
            }
        }

        if (list2 != null && list2.size() > 0) {
            for (String str : list2) {
                list.add(str);
            }
        }
        return list;
    }

    public static void main(String[] args) {
        int i = 0;
        while (i < 2000000) {
            System.out.println(i);
            i++;
        }

        List<String> list1 = Arrays.asList("a,b,c,d,e,f,g".split(","));
        List<String> list2 = Arrays.asList("h,i,j,k,l,m,n".split(","));
        mergeList(list1, list2);

    }

}


import com.sun.btrace.annotations.BTrace;
import com.sun.btrace.annotations.Kind;
import com.sun.btrace.annotations.Location;
import com.sun.btrace.annotations.OnMethod;
import com.sun.btrace.annotations.Return;

import static com.sun.btrace.BTraceUtils.*;

@BTrace
public class PrintArgArray {

    @OnMethod(clazz = "TestList", method = "mergeList", location = @Location(Kind.RETURN))
    public static void testList(java.util.List<String> list1, java.util.List<String> list2,
                                @Return java.util.List<String> list) {
        println(str(list1));
        println("----------------------------------");
        println(str(list2));
        println("----------------------------------");
        println(str(list));
    }

}

首先启动TestList类,然后运行btrace pid PrintArgArray.java,可以看到打印的结果。


上面时打印普通的String list,而对于List中是其它对象的呢?代码如下:

import java.io.Serializable;

public class User implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -7020619477594468968L;

    private long              id;
    private String            userName;
    private String            gender;

    public User(long id, String userName, String gender) {
        this.id = id;
        this.userName = userName;
        this.gender = gender;
    }

    /**
     * @return the id
     */
    public long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(long id) {
        this.id = id;
    }

    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * @return the gender
     */
    public String getGender() {
        return gender;
    }

    /**
     * @param gender the gender to set
     */
    public void setGender(String gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "User = [ userName = " + userName + " gender = " + gender + "]";
    }
}



import java.util.ArrayList;
import java.util.List;

public class TestList2 {
    public static void main(String[] args) {
        int i = 0;
        while (i < 2000000) {
            System.out.println(i);
            i++;
        }

        User user = null;
        List<User> list1 = new ArrayList<User>();

        for (int j = 0; j < 10; j++) {
            user = new User(j, "userName1" + j, "gender1" + j);
            list1.add(user);
        }
        List<User> list2 = new ArrayList<User>();

        for (int j = 0; j < 10; j++) {
            user = new User(j, "userName2" + j, "gender2" + j);
            list2.add(user);
        }
        mergeUserList(list1, list2);
    }

    public static List<User> mergeUserList(List<User> list1, List<User> list2) {
        List<User> list = new ArrayList<User>();
        list.addAll(list1);
        list.addAll(list2);
        return list;
    }
}



import static com.sun.btrace.BTraceUtils.println;
import static com.sun.btrace.BTraceUtils.str;

import com.sun.btrace.annotations.BTrace;
import com.sun.btrace.annotations.Kind;
import com.sun.btrace.annotations.Location;
import com.sun.btrace.annotations.OnMethod;
import com.sun.btrace.annotations.Return;

@BTrace
public class PrintArgArray2 {

    @OnMethod(clazz = "TestList2", method = "mergeUserList", location = @Location(Kind.RETURN))
    public static void testUserList(java.util.List<User> list1, java.util.List<User> list2,
                                    @Return java.util.List<User> list) {
        println(str(list1));
        println("----------------------------------");
        println(str(list2));
        println("----------------------------------");
        println(str(list));
    }
}

合并两个list,list中存放的为user对象,最后用btrace打印两个参数list和结果list中的user值,结果如下

[User = [ userName = userName10 gender = gender10], User = [ userName = userName11 gender = gender11], User = [ userName = userName12 gender = gender12], User = [ userName = userName13 gender = gender13], User = [ userName = userName14 gender = gender14], User = [ userName = userName15 gender = gender15], User = [ userName = userName16 gender = gender16], User = [ userName = userName17 gender = gender17], User = [ userName = userName18 gender = gender18], User = [ userName = userName19 gender = gender19]]
----------------------------------
[User = [ userName = userName20 gender = gender20], User = [ userName = userName21 gender = gender21], User = [ userName = userName22 gender = gender22], User = [ userName = userName23 gender = gender23], User = [ userName = userName24 gender = gender24], User = [ userName = userName25 gender = gender25], User = [ userName = userName26 gender = gender26], User = [ userName = userName27 gender = gender27], User = [ userName = userName28 gender = gender28], User = [ userName = userName29 gender = gender29]]
----------------------------------
[User = [ userName = userName10 gender = gender10], User = [ userName = userName11 gender = gender11], User = [ userName = userName12 gender = gender12], User = [ userName = userName13 gender = gender13], User = [ userName = userName14 gender = gender14], User = [ userName = userName15 gender = gender15], User = [ userName = userName16 gender = gender16], User = [ userName = userName17 gender = gender17], User = [ userName = userName18 gender = gender18], User = [ userName = userName19 gender = gender19], User = [ userName = userName20 gender = gender20], User = [ userName = userName21 gender = gender21], User = [ userName = userName22 gender = gender22], User = [ userName = userName23 gender = gender23], User = [ userName = userName24 gender = gender24], User = [ userName = userName25 gender = gender25], User = [ userName = userName26 gender = gender26], User = [ userName = userName27 gender = gender27], User = [ userName = userName28 gender = gender28], User = [ userName = userName29 gender = gender29]]

注意运行第二个实例前要把user这个类要实现toString方法,这样才会打印出每个字段的值。

其次先编译User这个类,我是在当前目录下运行的,或者也可以制定classpath。否则会报BTrace compilation failed错误


你可能感兴趣的:(Btrace检测list参数、结果)