dump Method Stack - Java java.lang

Java examples for java.lang:Throwable

Description

dump Method Stack

Demo Code


//package com.java2s;

public class Main {
    public static String dumpMethodStack(StackTraceElement topTraceElement,
            Thread thread) {//from   w  w w  . ja  v a 2 s .com
        StringBuffer buf = new StringBuffer();
        StackTraceElement[] stackElements = thread.getStackTrace();
        if (stackElements != null) {
            for (int i = 0; i < stackElements.length && i < 50; i++) {
                StackTraceElement ele = stackElements[i];
                if (ele == topTraceElement)
                    break;
                String str = ele.toString();
                if (isTracerMethod(str))
                    continue;
                buf.append(str);
                if (i != stackElements.length - 1)
                    buf.append("\n");
            }
        }
        return buf.toString();
    }

    private static boolean isTracerMethod(String str) {
        return str.startsWith("net.juniper.jmp.tracer")
                || str.startsWith("net.juniper.jmp.monitor")
                || str.startsWith("java.lang.Thread.getStackTrace");
    }
}

Related Tutorials