本文整理了Java中java.lang.Throwable.setStackTrace()
方法的一些代码示例,展示了Throwable.setStackTrace()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Throwable.setStackTrace()
方法的具体详情如下:
包路径:java.lang.Throwable
类名称:Throwable
方法名:setStackTrace
[英]Sets the array of stack trace elements. Each StackTraceElementrepresents an entry in the call stack. A copy of the specified array is stored in this Throwable. will be returned by getStackTrace() and printed by printStackTrace().
[中]设置堆栈跟踪元素的数组。每个StackTraceeleCenter在调用堆栈中显示一个条目。指定数组的副本存储在此可丢弃文件中。将由getStackTrace()返回,并由printStackTrace()打印。
代码示例来源:origin: netty/netty
/**
* 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;
}
代码示例来源:origin: redisson/redisson
/**
* 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;
}
代码示例来源:origin: square/retrofit
private NetworkBehavior(Random random) {
this.random = random;
failureException = new MockRetrofitIOException();
failureException.setStackTrace(new StackTraceElement[0]);
}
代码示例来源:origin: AsyncHttpClient/async-http-client
/**
* @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;
}
}
代码示例来源:origin: skylot/jadx
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));
}
}
代码示例来源:origin: google/guava
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;
}
代码示例来源:origin: apache/incubator-dubbo
@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;
}
代码示例来源:origin: apache/incubator-dubbo
@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;
}
代码示例来源:origin: spring-projects/spring-framework
/**
* 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();
}
}
}
代码示例来源:origin: org.mockito/mockito-core
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;
}
}
代码示例来源:origin: Alluxio/alluxio
/**
* @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();
}
代码示例来源:origin: prestodb/presto
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;
}
代码示例来源:origin: weibocom/motan
/**
* 覆盖给定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());
}
}
}
}
代码示例来源:origin: org.mockito/mockito-core
public void filter(Throwable throwable) {
if (!config.cleansStackTrace()) {
return;
}
StackTraceElement[] filtered = filter.filter(throwable.getStackTrace(), true);
throwable.setStackTrace(filtered);
}
}
代码示例来源:origin: springside/springside4
/**
* 清除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;
}
}
代码示例来源:origin: org.assertj/assertj-core
/**
* 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()]));
}
代码示例来源:origin: google/j2objc
public void filter(Throwable throwable) {
if (!config.cleansStackTrace()) {
return;
}
StackTraceElement[] filtered = filter.filter(throwable.getStackTrace(), true);
throwable.setStackTrace(filtered);
}
}
代码示例来源:origin: spring-projects/spring-framework
System.arraycopy(callStack, index, result, cachedIndex, callStack.length - index);
clone.setStackTrace(result);
return new CacheOperationInvoker.ThrowableWrapper(clone);
代码示例来源:origin: org.mockito/mockito-core
@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)));
}
代码示例来源:origin: gocd/gocd
@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]*$");
}
内容来源于网络,如有侵权,请联系作者删除!