list Stack Trace Elements - Android java.lang

Android examples for java.lang:Throwable

Description

list Stack Trace Elements

Demo Code


import android.content.Context;
import android.text.TextUtils;

public class Main{
    private final static String STACK_TRACE_ELEMENT_NULL = "[ stacktrace elements is null ]";
    /**/*from w ww. j a v a 2s.  c o m*/
     *
     */
    public static void listStackTraceElements() {
        StackTraceElement[] stackTraceElements = getStackTraceElements();
        if (stackTraceElements != null) {
            for (int i = 0; i < stackTraceElements.length; i++) {
                L.d(stackTraceElements[i].getClassName() + " "
                        + stackTraceElements[i].getMethodName() + " "
                        + stackTraceElements[i].getFileName() + " "
                        + stackTraceElements[i].getLineNumber());
            }
        }
    }
    /**
     *
     * @return
     */
    private static StackTraceElement[] getStackTraceElements() {
        StackTraceElement[] stackTraceElements = Thread.currentThread()
                .getStackTrace();
        return stackTraceElements;
    }
    /**
     * current method name
     * @return
     */
    public static String getMethodName(Context context) {
        StackTraceElement[] stackTraceElements = getStackTraceElements();
        if (stackTraceElements != null && stackTraceElements.length > 0) {
            for (StackTraceElement element : stackTraceElements) {
                String className = element.getClassName();
                if (!TextUtils.isEmpty(className)
                        && className.contains(context.getClass()
                                .getSimpleName())) {
                    return element.getMethodName();
                }
            }
        }
        return STACK_TRACE_ELEMENT_NULL;
    }
}

Related Tutorials