Example usage for java.lang StackTraceElement toString

List of usage examples for java.lang StackTraceElement toString

Introduction

In this page you can find the example usage for java.lang StackTraceElement toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of this stack trace element.

Usage

From source file:Main.java

public static String getStackTraceAsString(Throwable e) {
    String stackTraceString = e.getClass().getName() + ": " + e.getMessage();
    for (StackTraceElement ste : e.getStackTrace()) {
        stackTraceString += "\n" + ste.toString();
    }//  w  w  w  . j  a v  a  2s  .  c om
    return stackTraceString;
}

From source file:Main.java

/**
 * Pass an exception to get back the stack trace
 * @param pException//w ww.j ava 2 s .c om
 * @return stackTrace
 */
public static String getStackTrace(Exception pException) {
    if (pException != null) {
        final StringBuilder strBuilder = new StringBuilder("Cause of error: ");
        strBuilder.append(pException.getMessage());
        strBuilder.append("\n");
        strBuilder.append(pException.toString());
        strBuilder.append("\n");
        final StackTraceElement[] elementArray = pException.getStackTrace();
        for (final StackTraceElement element : elementArray) {
            strBuilder.append(element.toString());
            strBuilder.append("\n");
        }
        return strBuilder.toString();
    } else {
        return null;
    }
}

From source file:Main.java

public static void dumpAllStackTraces() {
    synchronized (System.err) {
        Map<Thread, StackTraceElement[]> allStackTraces = Thread.getAllStackTraces();
        for (Map.Entry<Thread, StackTraceElement[]> entry : allStackTraces.entrySet()) {
            System.err.println("Stack Trace for " + entry.getKey().getName());
            StackTraceElement[] elements = entry.getValue();
            for (StackTraceElement element : elements) {
                System.err.println("\tat " + element.toString());
            }/*from   w w  w.j a v a2  s  .com*/
            System.err.println();
        }
    }
}

From source file:com.jaeksoft.searchlib.util.ExceptionUtils.java

public final static String getFirstLocation(StackTraceElement[] stackTrace) {
    for (StackTraceElement element : stackTrace) {
        String ele = element.toString();
        if (ele != null && ele.length() > 0)
            return ele;
    }//from  ww w . j a  v a 2  s .  com
    return null;
}

From source file:Main.java

/**
 * Determines the name of the invoking method.<br />
 * <br />//from www.j av  a 2 s .  co  m
 * <i><u>Note:</u><br />
 * The method looks for the method name within the current thread's stack trace. The
 * current implementation looks for the name at a fixed position.</i>
 *
 * @return a method name
 */
public static String getMethodName() {

    Thread currentThread = Thread.currentThread();
    StackTraceElement[] stackTrace = currentThread.getStackTrace();

    int elements = stackTrace.length;
    int expectedIndex = 3;

    if (expectedIndex < elements) {

        StackTraceElement element = stackTrace[expectedIndex];

        String[] substrings = element.toString().split("\\(");
        String name = substrings[0];

        return name;
    }

    return "unknown method";
}

From source file:com.github.nukesparrow.htmlunit.Util.java

public static MapBuilder mapBuilderBaseData() {
    MapBuilder b = new MapBuilder().put("time", System.currentTimeMillis());
    StringBuilder sb = new StringBuilder();
    for (StackTraceElement e : Thread.currentThread().getStackTrace()) {
        sb.append(e.toString()).append('\n');
    }//from  ww  w  .  j  a  v  a  2 s.c o  m
    b.put("stack", sb.toString());
    return b;
}

From source file:Main.java

public static String[] getStackTrace(long threadID) {
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    ThreadInfo threadInfo = threadMXBean.getThreadInfo(threadID, Integer.MAX_VALUE);

    List<String> buf = new ArrayList<String>();

    if (threadInfo == null) {
        return buf.toArray((String[]) Array.newInstance(String.class, buf.size()));
    }//  w  w w. j a  v  a  2  s.com

    StackTraceElement[] stackTrace = threadInfo.getStackTrace();
    for (int i = 0; i < stackTrace.length; i++) {
        StackTraceElement ste = stackTrace[i];
        buf.add("\t" + ste.toString());

        if (i == 0 && threadInfo.getLockInfo() != null) {
            Thread.State ts = threadInfo.getThreadState();
            switch (ts) {
            case BLOCKED:
                buf.add("\t-  blocked on " + threadInfo.getLockInfo());
                break;
            case WAITING:
                buf.add("\t-  waiting on " + threadInfo.getLockInfo());
                break;
            case TIMED_WAITING:
                buf.add("\t-  waiting on " + threadInfo.getLockInfo());
                break;
            default:
            }
        }

        for (MonitorInfo mi : threadInfo.getLockedMonitors()) {
            if (mi.getLockedStackDepth() == i) {
                buf.add("\t-  locked " + mi);
            }
        }
    }

    LockInfo[] locks = threadInfo.getLockedSynchronizers();
    if (locks.length > 0) {
        buf.add("\n\tNumber of locked synchronizers = " + locks.length);
        for (LockInfo li : locks) {
            buf.add("\t- " + li);
        }
    }

    return buf.toArray((String[]) Array.newInstance(String.class, buf.size()));
}

From source file:com.mobage.air.extension.Dispatcher.java

public static void dispatch(FREContext context, String eventType, Throwable exception) {
    try {/*from   w w  w  .  j  a  v  a 2 s.c  o m*/
        JSONArray args = new JSONArray();

        args.put(exception.toString());
        for (StackTraceElement item : exception.getStackTrace()) {
            args.put(item.toString());
        }

        context.dispatchStatusEventAsync(eventType, args.toString());
    } catch (Exception e) {
        Log.wtf(TAG, e);
    }
}

From source file:com.p000ison.dev.simpleclans2.exceptions.handling.ExceptionReport.java

private static Object buildThrowableJSON(Throwable thrown) {
    JSONArray stackTrace = new JSONArray();

    for (StackTraceElement element : thrown.getStackTrace()) {
        stackTrace.add(element.toString());
    }/*w w  w  . j  a  va2  s  .c o  m*/

    return stackTrace;
}

From source file:com.ms.commons.utilities.CoreUtilities.java

/**
 * /* w ww.  jav a2s  . c om*/
 * 
 * @param e
 * @return
 */
public static String getExceptionText(Throwable e) {
    if (e != null) {
        StringBuilder sb = new StringBuilder();
        StackTraceElement[] st = e.getStackTrace();
        for (StackTraceElement s : st) {
            sb.append(s.toString()).append("\r\n");
        }
        return sb.toString();
    }
    return null;
}