/** * Set the {@link StackTraceElement} for the given {@link Throwable}, using the {@link Class} and method name. */ public static <T extends Throwable> T unknownStackTrace(T cause, Class<?> clazz, String method) { cause.setStackTrace(new StackTraceElement[] { new StackTraceElement(clazz.getName(), method, null, -1)}); return cause; }
/** * Set the {@link StackTraceElement} for the given {@link Throwable}, using the {@link Class} and method name. */ public static <T extends Throwable> T unknownStackTrace(T cause, Class<?> clazz, String method) { cause.setStackTrace(new StackTraceElement[] { new StackTraceElement(clazz.getName(), method, null, -1)}); return cause; }
private NetworkBehavior(Random random) { this.random = random; failureException = new MockRetrofitIOException(); failureException.setStackTrace(new StackTraceElement[0]); }
/** * @param <T> the Throwable type * @param t the throwable whose stacktrace we want to remove * @param clazz the caller class * @param method the caller method * @return the input throwable with removed stacktrace */ public static <T extends Throwable> T unknownStackTrace(T t, Class<?> clazz, String method) { t.setStackTrace(new StackTraceElement[]{new StackTraceElement(clazz.getName(), method, null, -1)}); return t; } }
private static void filter(Throwable th) { StackTraceElement[] stackTrace = th.getStackTrace(); int cutIndex = -1; int length = stackTrace.length; for (int i = 0; i < length; i++) { StackTraceElement stackTraceElement = stackTrace[i]; if (stackTraceElement.getClassName().startsWith(JADX_API_PACKAGE)) { cutIndex = i; } else if (cutIndex > 0) { cutIndex = i; break; } } if (cutIndex > 0 && cutIndex < length) { th.setStackTrace(Arrays.copyOfRange(stackTrace, 0, cutIndex)); } }
private static Exception throwCause(Exception e, boolean combineStackTraces) throws Exception { Throwable cause = e.getCause(); if (cause == null) { throw e; } 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; }
@Override public Object recreate() throws Throwable { if (exception != null) { // fix issue#619 try { // get Throwable class Class clazz = exception.getClass(); while (!clazz.getName().equals(Throwable.class.getName())) { clazz = clazz.getSuperclass(); } // get stackTrace value Field stackTraceField = clazz.getDeclaredField("stackTrace"); stackTraceField.setAccessible(true); Object stackTrace = stackTraceField.get(exception); if (stackTrace == null) { exception.setStackTrace(new StackTraceElement[0]); } } catch (Exception e) { // ignore } throw exception; } return result; }
@Override public Object recreate() throws Throwable { if (exception != null) { // fix issue#619 try { // get Throwable class Class clazz = exception.getClass(); while (!clazz.getName().equals(Throwable.class.getName())) { clazz = clazz.getSuperclass(); } // get stackTrace value Field stackTraceField = clazz.getDeclaredField("stackTrace"); stackTraceField.setAccessible(true); Object stackTrace = stackTraceField.get(exception); if (stackTrace == null) { exception.setStackTrace(new StackTraceElement[0]); } } catch (Exception e) { // ignore } throw exception; } return result; }
/** * Fill the current client-side stack trace into the given exception. * <p>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} array with the current client-side stack * trace, provided that we run on JDK 1.4+. * @param ex the exception to update * @see Throwable#getStackTrace() * @see Throwable#setStackTrace(StackTraceElement[]) */ public static void fillInClientStackTraceIfPossible(Throwable ex) { if (ex != null) { StackTraceElement[] clientStack = new Throwable().getStackTrace(); Set<Throwable> visitedExceptions = new HashSet<>(); 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(); } } }
static Throwable hideRecursiveCall(Throwable throwable, int current, Class<?> targetType) { try { StackTraceElement[] stack = throwable.getStackTrace(); int skip = 0; StackTraceElement next; do { next = stack[stack.length - current - ++skip]; } while (!next.getClassName().equals(targetType.getName())); int top = stack.length - current - skip; StackTraceElement[] cleared = new StackTraceElement[stack.length - skip]; System.arraycopy(stack, 0, cleared, 0, top); System.arraycopy(stack, top + skip, cleared, top, current); throwable.setStackTrace(cleared); return throwable; } catch (RuntimeException ignored) { // This should not happen unless someone instrumented or manipulated exception stack traces. return throwable; } }
/** * @param thread a thread * @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(); }
private static Exception throwCause(Exception e, boolean combineStackTraces) throws Exception { Throwable cause = e.getCause(); if (cause == null) { throw e; } 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; }
/** * 覆盖给定exception的stack信息,server端产生业务异常时调用此类屏蔽掉server端的异常栈。 * * @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()); } } } }
public void filter(Throwable throwable) { if (!config.cleansStackTrace()) { return; } StackTraceElement[] filtered = filter.filter(throwable.getStackTrace(), true); throwable.setStackTrace(filtered); } }
/** * 清除StackTrace. 假设StackTrace已生成, 但把它打印出来也有不小的消耗. * * 如果不能控制StackTrace的生成,也不能控制它的打印端(如logger),可用此方法暴力清除Trace. * * 但Cause链依然不能清除, 只能清除每一个Cause的StackTrace. */ public static <T extends Throwable> T clearStackTrace(@NotNull T throwable) { Throwable cause = throwable; while (cause != null) { cause.setStackTrace(EMPTY_STACK_TRACE); cause = cause.getCause(); } return throwable; } }
/** * Appends the stack trace of the current thread to the one in the given <code>{@link Throwable}</code>. * * @param t the given {@code Throwable}. * @param methodToStartFrom the name of the method used as the starting point of the current thread's stack trace. */ public static void appendStackTraceInCurrentThreadToThrowable(Throwable t, String methodToStartFrom) { List<StackTraceElement> stackTrace = newArrayList(t.getStackTrace()); stackTrace.addAll(stackTraceInCurrentThread(methodToStartFrom)); t.setStackTrace(stackTrace.toArray(new StackTraceElement[stackTrace.size()])); }
public void filter(Throwable throwable) { if (!config.cleansStackTrace()) { return; } StackTraceElement[] filtered = filter.filter(throwable.getStackTrace(), true); throwable.setStackTrace(filtered); } }
System.arraycopy(callStack, index, result, cachedIndex, callStack.length - index); clone.setStackTrace(result); return new CacheOperationInvoker.ThrowableWrapper(clone);
@Override public Callable<?> handle(Object instance, Method origin, Object[] arguments) throws Throwable { MockMethodInterceptor interceptor = interceptors.get(instance); if (interceptor == null) { return null; } RealMethod realMethod; if (instance instanceof Serializable) { realMethod = new SerializableRealMethodCall(identifier, origin, instance, arguments); } else { realMethod = new RealMethodCall(selfCallInfo, origin, instance, arguments); } Throwable t = new Throwable(); t.setStackTrace(skipInlineMethodElement(t.getStackTrace())); return new ReturnValueWrapper(interceptor.doIntercept(instance, origin, arguments, realMethod, new LocationImpl(t))); }
@Test public void shouldLogThrowableDetailsAlongwithMessage() throws Exception { Throwable throwable = new RuntimeException("oops"); throwable.setStackTrace(new StackTraceElement[]{new StackTraceElement("class", "method", "field", 20)}); pluginLoggingService.error(pluginID(1), "LoggingClass", "error", throwable); assertMessageInLog(pluginLog(1), "ERROR", "LoggingClass", "error", "java\\.lang\\.RuntimeException:\\soops[\\s\\S]*at\\sclass\\.method\\(field:20\\)[\\s\\S]*$"); }