Example usage for java.lang Throwable setStackTrace

List of usage examples for java.lang Throwable setStackTrace

Introduction

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

Prototype

public void setStackTrace(StackTraceElement[] stackTrace) 

Source Link

Document

Sets the stack trace elements that will be returned by #getStackTrace() and printed by #printStackTrace() and related methods.

Usage

From source file:Main.java

public static void main(String[] argv) throws FileNotFoundException {
    Throwable t = new Exception("from java2s.com");

    t.setStackTrace(new Exception("java2s.com").getStackTrace());

}

From source file:Main.java

/**
 * @param thread a thread//from ww  w  .  j  a  v  a2 s. c o m
 * @return a human-readable representation of the thread's stack trace
 */
public static String formatStackTrace(Thread thread) {
    Throwable t = new Throwable(
            String.format("Stack trace for thread %s (State: %s):", thread.getName(), thread.getState()));
    t.setStackTrace(thread.getStackTrace());
    StringWriter sw = new StringWriter();
    t.printStackTrace(new PrintWriter(sw));
    return sw.toString();
}

From source file:com.weibo.api.motan.util.ExceptionUtil.java

/**
 * exceptionstack?server?server/*from   w w w .  j  a  v  a2s . c  o m*/
 *
 * @param e
 */
public static void setMockStackTrace(Throwable e) {
    if (e != null) {
        try {
            e.setStackTrace(REMOTE_MOCK_STACK);
        } catch (Exception e1) {
            LoggerUtil.warn("replace remote exception stack fail!" + e1.getMessage());
        }
    }
}

From source file:cc.sion.core.utils.Exceptions.java

/**
 * StackTrace. ?StackTrace?, ????.//  ww w  .j a  v  a 2  s  . c  o  m
 *
 * ?StackTrace???(logger)?Trace.
 *
 * Cause??, ??CauseStackTrace.
 */
public static <T extends Throwable> T clearStackTrace(T exception) {
    Throwable cause = exception;
    while (cause != null) {
        cause.setStackTrace(EMPTY_STACK_TRACE);
        cause = cause.getCause();
    }
    return exception;// NOSONAR
}

From source file:com.infinities.skyport.timeout.ServiceProviderTimeLimiter.java

private static Exception throwCause(Exception e, boolean combineStackTraces) throws Exception {
    Throwable cause = e.getCause();
    if (cause == null) {
        throw e;/*from  w ww  .  j  a v  a  2 s.  co m*/
    }
    if (combineStackTraces) {
        StackTraceElement[] combined = ObjectArrays.concat(cause.getStackTrace(), e.getStackTrace(),
                StackTraceElement.class);
        cause.setStackTrace(combined);
    }
    if (cause instanceof Exception) {
        throw (Exception) cause;
    }
    if (cause instanceof Error) {
        throw (Error) cause;
    }
    // The cause is a weird kind of Throwable, so throw the outer exception.
    throw e;
}

From source file:Main.java

/**
 * Like {@link #sprintFullStack(Throwable)}, renders a stacktrace as a String for logging,
 * but excludes stack trace elements common with the calling scope, which may
 * be considered irrelevant to application functionality testing
 * @param t an exception/*ww w. j  av a2 s.c om*/
 * @return a string representation of a shorter stack trace
 */
public static String sprintShortStack(final Throwable t) {
    if (t == null) {
        return "";
    }

    StringWriter writer = new StringWriter();
    Throwable local = new Throwable();

    StackTraceElement[] localStack = local.getStackTrace();
    StackTraceElement[] tStack = t.getStackTrace();

    int m = tStack.length - 1, n = localStack.length - 1;
    while (m >= 0 && n >= 0 && tStack[m].equals(localStack[n])) {
        m--;
        n--;
    }

    int framesInCommon = tStack.length - 1 - (m + 1);

    t.setStackTrace(Arrays.copyOf(t.getStackTrace(), m + 1, StackTraceElement[].class));
    t.printStackTrace(new PrintWriter(writer));
    t.setStackTrace(tStack);
    StringBuilder sb = new StringBuilder(writer.toString());
    if (framesInCommon != 0) {
        sb.append("\t... " + framesInCommon + " more");
    }
    return sb.toString();
}

From source file:org.jgentleframework.utils.ExceptionUtils.java

/**
 * Fill the current client-side stack trace into the given exception.
 * <p>// w w w.  ja  v a 2  s.co m
 * The given exception is typically thrown on the server and serialized
 * as-is, with the client wanting it to contain the client-side portion of
 * the stack trace as well. What we can do here is to update the
 * <code>StackTraceElement</code> array with the current client-side stack
 * trace, provided that we run on JDK 1.5+.
 * 
 * @param ex
 *            the exception to update
 * @see java.lang.Throwable#getStackTrace()
 * @see java.lang.Throwable#setStackTrace(StackTraceElement[])
 */
public static void fillInClientStackTraceIfPossible(Throwable ex) {

    if (ex != null) {
        StackTraceElement[] clientStack = new Throwable().getStackTrace();
        Set<Throwable> visitedExceptions = new HashSet<Throwable>();
        Throwable exToUpdate = ex;
        while (exToUpdate != null && !visitedExceptions.contains(exToUpdate)) {
            StackTraceElement[] serverStack = exToUpdate.getStackTrace();
            StackTraceElement[] combinedStack = new StackTraceElement[serverStack.length + clientStack.length];
            System.arraycopy(serverStack, 0, combinedStack, 0, serverStack.length);
            System.arraycopy(clientStack, 0, combinedStack, serverStack.length, clientStack.length);
            exToUpdate.setStackTrace(combinedStack);
            visitedExceptions.add(exToUpdate);
            exToUpdate = exToUpdate.getCause();
        }
    }
}

From source file:org.apache.drill.common.util.DrillExceptionUtil.java

/**
 * Recreate throwable from exception protoBuf which received from remote or local node.
 * if no match constructor found, the base Throwable (Exception or Error) is created as a substitution
 *
 * @param exceptionWrapper the exception protoBuf
 * @return Throwable deserialized from protoBuf
 *///from ww w. ja  v a2 s.co  m
public static Throwable getThrowable(UserBitShared.ExceptionWrapper exceptionWrapper) {
    if (exceptionWrapper == null) {
        return null;
    }
    String className = exceptionWrapper.getExceptionClass();
    if (StringUtils.isBlank(className) || exceptionWrapper.getStackTraceCount() < 1) {
        return null;
    }
    Throwable inner = getThrowable(exceptionWrapper.getCause());
    try {
        Throwable throwable = getInstance(className, exceptionWrapper.getMessage(), inner);
        int size = exceptionWrapper.getStackTraceCount();
        StackTraceElement[] stackTrace = new StackTraceElement[size];
        for (int i = 0; i < size; ++i) {
            UserBitShared.StackTraceElementWrapper w = exceptionWrapper.getStackTrace(i);
            stackTrace[i] = new StackTraceElement(w.getClassName(), w.getMethodName(), w.getFileName(),
                    w.getLineNumber());
        }
        throwable.setStackTrace(stackTrace);
        return throwable;
    } catch (Throwable t) {
        return null;
    }
}

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

/**
 * Removes some internal Mule entries from the stacktrace. Modifies the
 * passed-in throwable stacktrace./*from   w  w  w .j a  va  2s. com*/
 */
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.uiautomation.ios.communication.FailedWebDriverLikeResponse.java

public static Exception createThrowable(JSONObject serialized) {
    try {/*from  w  w  w  .jav  a 2  s.  c om*/
        Class<? extends Exception> c = getException(serialized);

        String msg = serialized.getString("message");

        JSONObject cause = serialized.optJSONObject("cause");
        Throwable causeThrowable = null;
        if (cause != null) {
            causeThrowable = createThrowable(cause);
        }

        Object[] args = new Object[] { msg, (Exception) causeThrowable };
        Class<?>[] argsClass = new Class[] { String.class, Throwable.class };
        Constructor<?> constructor = c.getConstructor(argsClass);
        Throwable exception = (Throwable) constructor.newInstance(args);
        JSONArray stack = serialized.optJSONArray("stacktrace");
        if (stack != null) {
            List<StackTraceElement> stacks = new ArrayList<StackTraceElement>();
            for (int i = 0; i < stack.length(); i++) {
                JSONObject o = stack.getJSONObject(i);
                String fileName = o.getString("fileName");
                String className = o.getString("className");
                String methodName = o.getString("methodName");
                int lineNumber = o.getInt("lineNumber");
                StackTraceElement el = new StackTraceElement(className, methodName, fileName, lineNumber);
                stacks.add(el);
            }
            exception.setStackTrace(stacks.toArray(new StackTraceElement[0]));
        }
        return (Exception) exception;
    } catch (Exception e) {
        throw new IOSAutomationException(
                "error deserializing the exception from the server : " + serialized.toString(), e);
    }

}