Author: ggregory
Date: Tue May 13 02:54:03 2014
New Revision: 1594116
URL: http://svn.apache.org/r1594116
Log:
[LOG4J2-634] ThrowableProxy ctor throws an exception when using suppressed
exceptions. Refactor and fix code out of ThrowableProxy into Throwables. Tests
and main code will use this util class. I have local code that uses the new
code in Throwables but I want to commit in smaller chunks.
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/helpers/Throwables.java
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/helpers/Throwables.java
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/helpers/Throwables.java?rev=1594116&r1=1594115&r2=1594116&view=diff
==============================================================================
---
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/helpers/Throwables.java
(original)
+++
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/helpers/Throwables.java
Tue May 13 02:54:03 2014
@@ -22,22 +22,91 @@ import java.io.LineNumberReader;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
+import org.apache.logging.log4j.status.StatusLogger;
+
/**
* Helps with Throwable objects.
*/
public final class Throwables {
- private Throwables() {
+ private static final Method ADD_SUPPRESSED;
+
+ private static final Method GET_SUPPRESSED;
+
+ static {
+ Method getSuppressed = null, addSuppressed = null;
+ final Method[] methods = Throwable.class.getMethods();
+ for (final Method method : methods) {
+ if (method.getName().equals("getSuppressed")) {
+ getSuppressed = method;
+ } else if (method.getName().equals("addSuppressed")) {
+ addSuppressed = method;
+ }
+ }
+ GET_SUPPRESSED = getSuppressed;
+ ADD_SUPPRESSED = addSuppressed;
+ }
+
+ /**
+ * Has no effect on Java 6 and below.
+ *
+ * @param throwable a Throwable
+ * @param suppressedThrowable a suppressed Throwable
+ * @see Throwable#addSuppressed(Throwable)
+ * @deprecated If compiling on Java 7 and above use {@link
Throwable#addSuppressed(Throwable)}. Marked as deprecated because Java 6 is
+ * deprecated.
+ */
+ @Deprecated
+ public static void addSuppressed(Throwable throwable, Throwable
suppressedThrowable) {
+ if (ADD_SUPPRESSED != null) {
+ try {
+ ADD_SUPPRESSED.invoke(throwable, suppressedThrowable);
+ } catch (IllegalAccessException e) {
+ // Only happens on Java >= 7 if this class has a bug.
+ StatusLogger.getLogger().error(e);
+ } catch (IllegalArgumentException e) {
+ // Only happens on Java >= 7 if this class has a bug.
+ StatusLogger.getLogger().error(e);
+ } catch (InvocationTargetException e) {
+ // Only happens on Java >= 7 if this class has a bug.
+ StatusLogger.getLogger().error(e);
+ }
+ }
+
+ }
+
+ /**
+ * Has no effect on Java 6 and below.
+ *
+ * @param throwable a Throwable
+ * @return see Java 7's {@link Throwable#getSuppressed()}
+ * @see Throwable#getSuppressed()
+ * @deprecated If compiling on Java 7 and above use {@link
Throwable#getSuppressed()}. Marked as deprecated because Java 6 is
+ * deprecated.
+ */
+ @Deprecated
+ public static Throwable[] getSuppressed(Throwable throwable) {
+ if (GET_SUPPRESSED != null) {
+ try {
+ return (Throwable[]) GET_SUPPRESSED.invoke(throwable);
+ } catch (final Exception e) {
+ // Only happens on Java >= 7 if this class has a bug.
+ StatusLogger.getLogger().error(e);
+ return null;
+ }
+ }
+ return null;
}
/**
* Converts a Throwable stack trace into a List of Strings
- *
- * @param throwable
- * the Throwable
+ *
+ * @param throwable the Throwable
* @return a List of Strings
*/
public static List<String> toStringList(final Throwable throwable) {
@@ -68,4 +137,7 @@ public final class Throwables {
return lines;
}
+ private Throwables() {
+ }
+
}