Author: rgoers
Date: Wed Dec 28 17:06:09 2011
New Revision: 1225236
URL: http://svn.apache.org/viewvc?rev=1225236&view=rev
Log:
Add Java 7 methods to ThrowableProxy
Modified:
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java
Modified:
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java
URL:
http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java?rev=1225236&r1=1225235&r2=1225236&view=diff
==============================================================================
---
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java
(original)
+++
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java
Wed Dec 28 17:06:09 2011
@@ -42,8 +42,11 @@ public class ThrowableProxy extends Thro
private static final org.apache.logging.log4j.Logger logger =
StatusLogger.getLogger();
+ private static Method getSuppressed;
+
private final Throwable throwable;
private final ThrowableProxy cause;
+ private final ThrowableProxy[] suppressed;
private int commonElementCount;
private final StackTracePackageElement[] callerPackageData;
@@ -51,6 +54,7 @@ public class ThrowableProxy extends Thro
static {
setupCallerCheck();
+ versionCheck();
}
/**
@@ -64,6 +68,7 @@ public class ThrowableProxy extends Thro
callerPackageData = resolvePackageData(stack, map, null,
throwable.getStackTrace());
this.cause = (throwable.getCause() == null) ? null :
new ThrowableProxy(throwable, stack, map, throwable.getCause());
+ suppressed = getSuppressed(throwable);
}
/**
@@ -79,6 +84,7 @@ public class ThrowableProxy extends Thro
callerPackageData = resolvePackageData(stack, map,
parent.getStackTrace(), cause.getStackTrace());
this.cause = (throwable.getCause() == null) ? null :
new ThrowableProxy(parent, stack, map, throwable.getCause());
+ suppressed = getSuppressed(throwable);
}
@@ -102,6 +108,22 @@ public class ThrowableProxy extends Thro
return cause;
}
+ /**
+ * Added in Java 7.
+ * @param exception A Throwable that was suppressed.
+ */
+ public void addSuppressed(Throwable exception) {
+ throw new UnsupportedOperationException("Cannot add suppressed
exceptions to a ThrowableProxy");
+ }
+
+ /**
+ * Added in Java 7.
+ * @return Any suppressed exceptions.
+ */
+ public Throwable[] getSuppressed() {
+ return suppressed;
+ }
+
@Override
public Throwable initCause(Throwable throwable) {
throw new IllegalStateException("Cannot set the cause on a
ThrowableProxy");
@@ -372,6 +394,15 @@ public class ThrowableProxy extends Thro
return clazz;
}
+ private static void versionCheck() {
+ Method[] methods = Throwable.class.getMethods();
+ for (Method method : methods) {
+ if (method.getName().equals("getSuppressed")) {
+ getSuppressed = method;
+ }
+ }
+ }
+
/**
* Determine if Reflection.getCallerClass is available.
*/
@@ -404,6 +435,24 @@ public class ThrowableProxy extends Thro
}
}
+ private ThrowableProxy[] getSuppressed(Throwable throwable) {
+ ThrowableProxy[] supp = null;
+ if (getSuppressed != null) {
+ try {
+ Throwable[] array = (Throwable[])
getSuppressed.invoke(throwable, null);
+ supp = new ThrowableProxy[array.length];
+ int i = 0;
+ for (Throwable t : array) {
+ supp[i] = new ThrowableProxy(t);
+ ++i;
+ }
+ } catch (Exception ex) {
+ //
+ }
+ }
+ return supp;
+ }
+
private class CacheEntry {
StackTracePackageElement element;
ClassLoader loader;