Example usage for java.lang Throwable getStackTrace

List of usage examples for java.lang Throwable getStackTrace

Introduction

In this page you can find the example usage for java.lang Throwable getStackTrace.

Prototype

public StackTraceElement[] getStackTrace() 

Source Link

Document

Provides programmatic access to the stack trace information printed by #printStackTrace() .

Usage

From source file:com.salesforce.ide.core.internal.utils.ForceExceptionUtils.java

public static void throwNewCoreException(Throwable th) throws CoreException {
    IStatus status = new MultiStatus(ForceIdeCorePlugin.getPluginId(), Constants.ERROR_CODE__44,
            getRootCauseMessage(th), ForceExceptionUtils.getRootCause(th));
    Utils.addTraceToStatus((MultiStatus) status, th.getStackTrace(), IStatus.ERROR);
    throw new CoreException(status);
}

From source file:org.mule.config.ExceptionHelper.java

/**
 * Removes some internal Mule entries from the stacktrace. Modifies the
 * passed-in throwable stacktrace.//w  w  w.j  a  v  a 2s  .c o m
 */
public static Throwable summarise(Throwable t, int depth) {
    t = sanitize(t);
    StackTraceElement[] trace = t.getStackTrace();

    int newStackDepth = Math.min(trace.length, depth);
    StackTraceElement[] newTrace = new StackTraceElement[newStackDepth];

    System.arraycopy(trace, 0, newTrace, 0, newStackDepth);
    t.setStackTrace(newTrace);

    return t;
}

From source file:org.eclipse.wb.internal.core.utils.exception.DesignerExceptionUtils.java

/**
 * @return <code>true</code> if given {@link Throwable} has given sequence on class/method names.
 *//*from   ww w.  ja v a 2s .  c  o m*/
public static boolean hasTraceElementsSequence(Throwable e, String[][] expected) {
    StackTraceElement[] elements = e.getStackTrace();
    traceElements: for (int i = 0; i < elements.length; i++) {
        for (int j = 0; j < expected.length; j++) {
            if (i + j < elements.length) {
                StackTraceElement element = elements[i + j];
                String expectedClass = expected[j][0];
                String expectedMethod = expected[j][1];
                if (element.getClassName().equals(expectedClass)
                        && element.getMethodName().equals(expectedMethod)) {
                    continue;
                }
            }
            continue traceElements;
        }
        return true;
    }
    return false;
}

From source file:org.rhq.core.pc.inventory.AvailabilityExecutor.java

static private String getSmallStackTrace(Throwable t) {
    StringBuilder smallStack = new StringBuilder();

    StackTraceElement[] stack = (null == t) ? new Exception().getStackTrace() : t.getStackTrace();
    for (int i = 1; i < stack.length; i++) {
        StackTraceElement ste = stack[i];
        if (ste.getClassName().startsWith("org.rhq")) {
            smallStack.append(ste.toString());
            smallStack.append("\n");
        }//  w  w  w. j a  va 2s  . c om
    }
    return smallStack.toString();
}

From source file:com.icesoft.faces.util.CoreUtils.java

public static boolean throwablesEqual(Throwable th1, Throwable th2) {
    if (th1 == null && th2 == null)
        return true;
    if (th1 == null || th2 == null)
        return false;
    if (th1.getClass() != th2.getClass())
        return false;

    if (!th1.getMessage().equals(th2.getMessage()))
        return false;

    StackTraceElement[] st1 = th1.getStackTrace();
    StackTraceElement[] st2 = th2.getStackTrace();
    if (st1.length != st2.length)
        return false;

    for (int i = 0; i < st1.length; i++) {
        if (!st1[i].equals(st2[i]))
            return false;
    }/*from  w ww  .  j  av  a2 s  .c o  m*/
    return true;
}

From source file:com.android.tools.idea.diagnostics.crash.CrashReport.java

/**
 * Returns an exception description (similar to {@link ExceptionUtil#getThrowableText(Throwable)} with the exception message
 * removed in order to strip off any PII. The exception message is include for some specific exceptions where we know that the
 * message will not have any PII.//from  w  w  w  .j  av  a 2s.c  om
 */
@NotNull
public static String getDescription(@NotNull Throwable t) {
    if (THROWABLE_CLASSES_TO_TRACK_MESSAGES.contains(t.getClass())) {
        return ExceptionUtil.getThrowableText(t);
    }

    StringBuilder sb = new StringBuilder(256);

    sb.append(t.getClass().getName());
    sb.append(": <elided>\n"); // note: some message is needed for the backend to parse the report properly

    for (StackTraceElement el : t.getStackTrace()) {
        sb.append("\tat ");
        sb.append(el);
        sb.append('\n');
    }

    return sb.toString();
}

From source file:org.mule.config.ExceptionHelper.java

/**
 * Removes some internal Mule entries from the stacktrace. Modifies the
 * passed-in throwable stacktrace.// w w  w. j av a 2 s  .  c  o  m
 */
public static Throwable sanitize(Throwable t) {
    if (t == null) {
        return null;
    }
    StackTraceElement[] trace = t.getStackTrace();
    List<StackTraceElement> newTrace = new ArrayList<StackTraceElement>();
    for (StackTraceElement stackTraceElement : trace) {
        if (!isMuleInternalClass(stackTraceElement.getClassName())) {
            newTrace.add(stackTraceElement);
        }
    }

    StackTraceElement[] clean = new StackTraceElement[newTrace.size()];
    newTrace.toArray(clean);
    t.setStackTrace(clean);

    Throwable cause = t.getCause();
    while (cause != null) {
        sanitize(cause);
        cause = cause.getCause();
    }

    return t;
}

From source file:com.qpark.eip.core.ToString.java

private static String getStackTrace(final Throwable t, final boolean isCause) {
    StringBuffer sb = new StringBuffer(1024);
    if (isCause) {
        sb.append("Caused by: ");
    }/*from   w  w w.j a  v  a 2 s. c  o  m*/
    sb.append(t.getClass().getName()).append(": ").append(t.getMessage()).append("\n");
    StackTraceElement[] stack = t.getStackTrace();
    int classNameLines = 0;
    int classNameLinesMax = 3;
    boolean printDots = false;
    boolean firstLine = true;
    for (StackTraceElement elem : stack) {
        if (firstLine || elem.getClassName().startsWith(CLASSNAME) && classNameLines <= classNameLinesMax) {
            sb.append("\tat ").append(elem.toString()).append("\n");
            classNameLines++;
            firstLine = false;
            printDots = false;
        } else {
            if (!printDots) {
                sb.append("\tat ...\n");
                printDots = true;
            }
            classNameLines = 0;
        }
    }
    Throwable cause = t.getCause();
    if (cause != null) {
        sb.append(getStackTrace(cause, true));
    }
    return sb.toString();
}

From source file:org.openspaces.test.client.executor.ExecutorUtils.java

/**
 * Strip stack trace of provided {@link Throwable} object up to supplied classname <b>and</b>
 * method name. This method is useful when you wish to hide internal stackTrace.<br> This method
 * scans the {@link Throwable#getStackTrace()} and removes all {@link StackTraceElement} up to
 * supplied classname and methodName. The Throwable's cause is also scanned in a
 * <i>recursive</i> manner. <p> <u>NOTE</u>: If the provided classname and method was not found
 * in the stackTrace, the {@link Throwable} stackTrace stays unmodified.
 *
 * <pre>//from  w  w w.j  a  v  a 2 s  . c  o  m
 * For example:
 * Original stackTrace:
 * java.lang.Exception: Example stack trace...
 * at test.TestSTrace.applCall3(Test.java:926)
 * at test.TestSTrace.applCall2(Test.java:921)
 * at test.TestSTrace.applCall1(Test.java:916)
 * at test.TestSTrace.startApplCall(Test.java:912)
 * at test.TestSTrace.internalCall3(Test.java:941)
 * at test.TestSTrace.internalCall2(Test.java:936)
 * at test.TestSTrace.internalCall1(Test.java:931)
 * at test.TestSTrace.main(Test.java:977)
 *
 * TGridHelper.stripStackTrace(throwable, "test.TestSTrace", "startApplCall");
 * throwable.printStackTrace();
 *
 * After:
 * java.lang.Exception: Example stack trace...
 * at test.TestSTrace.applCall3(Test.java:941)
 * at test.TestSTrace.applCall2(Test.java:936)
 * at test.TestSTrace.applCall1(Test.java:931)
 * at test.TestSTrace.startApplCall(Test.java:912)
 * </pre>
 *
 * @param ex         the throwable object to strip stack trace.
 * @param className  strip stackTrace up to classname.
 * @param methodName strip stackTrace up to methodName.
 */
public static void stripStackTrace(Throwable ex, String className, String methodName) {
    boolean found = false;
    StackTraceElement[] ste = ex.getStackTrace();
    List<StackTraceElement> stList = asList(ste);
    for (int i = stList.size() - 1; i >= 0; i--) {
        StackTraceElement traceList = stList.get(i);
        String traceClassName = traceList.getClassName();
        String traceMethodName = traceList.getMethodName();

        /* only if classname and method are equals with StackTraceElement */
        if (!(traceClassName.equals(className) && traceMethodName.equals(methodName)))
            stList.remove(i);
        else {
            found = true;
            break;
        }
    }

    /* replace only if provided classname and method was found the the stackTrace */
    if (found)
        ex.setStackTrace(stList.toArray(new StackTraceElement[stList.size()]));

    /* recursive call on all causes */
    Throwable cause = ex.getCause();
    if (cause != null)
        stripStackTrace(cause, className, methodName);
}

From source file:com.squarespace.gibson.GibsonUtils.java

private static List<StackTraceElement> getStackTrace(Throwable throwable) {
    StackTraceElement[] elements = null;
    if (STACK_TRACE != null) {
        try {/*from ww w.j ava2 s .  c  o  m*/
            elements = (StackTraceElement[]) STACK_TRACE.invoke(throwable);
        } catch (IllegalAccessException err) {
            LOG.error(Gibson.MARKER, "IllegalAccessException", err);
        } catch (InvocationTargetException err) {
            LOG.error(Gibson.MARKER, "InvocationTargetException", err);
        }
    }

    if (elements == null) {
        elements = throwable.getStackTrace();
    }

    if (elements != null) {
        return Arrays.asList(elements);
    }

    return null;
}