230 lines
7.6 KiB
Java
230 lines
7.6 KiB
Java
package rx.exceptions;
|
|
|
|
import java.io.PrintStream;
|
|
import java.io.PrintWriter;
|
|
import java.util.ArrayList;
|
|
import java.util.Collection;
|
|
import java.util.Collections;
|
|
import java.util.HashSet;
|
|
import java.util.Iterator;
|
|
import java.util.LinkedHashSet;
|
|
import java.util.List;
|
|
public final class CompositeException extends RuntimeException {
|
|
private static final long serialVersionUID = 3026362227162912146L;
|
|
private Throwable cause;
|
|
private final List<Throwable> exceptions;
|
|
private final String message;
|
|
|
|
public static final class a extends RuntimeException {
|
|
private static final long serialVersionUID = 3875212506787802066L;
|
|
|
|
@Override // java.lang.Throwable
|
|
public String getMessage() {
|
|
return "Chain of Causes for CompositeException In Order Received =>";
|
|
}
|
|
}
|
|
|
|
public static abstract class b {
|
|
public abstract Object a();
|
|
|
|
public abstract void b(Object obj);
|
|
}
|
|
|
|
public static final class c extends b {
|
|
public final PrintStream a;
|
|
|
|
public c(PrintStream printStream) {
|
|
this.a = printStream;
|
|
}
|
|
|
|
@Override // rx.exceptions.CompositeException.b
|
|
public Object a() {
|
|
return this.a;
|
|
}
|
|
|
|
@Override // rx.exceptions.CompositeException.b
|
|
public void b(Object obj) {
|
|
this.a.println(obj);
|
|
}
|
|
}
|
|
|
|
public static final class d extends b {
|
|
public final PrintWriter a;
|
|
|
|
public d(PrintWriter printWriter) {
|
|
this.a = printWriter;
|
|
}
|
|
|
|
@Override // rx.exceptions.CompositeException.b
|
|
public Object a() {
|
|
return this.a;
|
|
}
|
|
|
|
@Override // rx.exceptions.CompositeException.b
|
|
public void b(Object obj) {
|
|
this.a.println(obj);
|
|
}
|
|
}
|
|
|
|
public CompositeException(Collection<? extends Throwable> collection) {
|
|
LinkedHashSet linkedHashSet = new LinkedHashSet();
|
|
ArrayList arrayList = new ArrayList();
|
|
if (collection != null) {
|
|
for (Throwable th : collection) {
|
|
if (th instanceof CompositeException) {
|
|
linkedHashSet.addAll(((CompositeException) th).exceptions);
|
|
} else if (th != null) {
|
|
linkedHashSet.add(th);
|
|
} else {
|
|
linkedHashSet.add(new NullPointerException());
|
|
}
|
|
}
|
|
} else {
|
|
linkedHashSet.add(new NullPointerException());
|
|
}
|
|
arrayList.addAll(linkedHashSet);
|
|
List<Throwable> unmodifiableList = Collections.unmodifiableList(arrayList);
|
|
this.exceptions = unmodifiableList;
|
|
this.message = unmodifiableList.size() + " exceptions occurred. ";
|
|
}
|
|
|
|
public CompositeException(Throwable... thArr) {
|
|
LinkedHashSet linkedHashSet = new LinkedHashSet();
|
|
ArrayList arrayList = new ArrayList();
|
|
for (Throwable th : thArr) {
|
|
if (th instanceof CompositeException) {
|
|
linkedHashSet.addAll(((CompositeException) th).exceptions);
|
|
} else if (th != null) {
|
|
linkedHashSet.add(th);
|
|
} else {
|
|
linkedHashSet.add(new NullPointerException());
|
|
}
|
|
}
|
|
arrayList.addAll(linkedHashSet);
|
|
List<Throwable> unmodifiableList = Collections.unmodifiableList(arrayList);
|
|
this.exceptions = unmodifiableList;
|
|
this.message = unmodifiableList.size() + " exceptions occurred. ";
|
|
}
|
|
|
|
public final void a(StringBuilder sb, Throwable th, String str) {
|
|
sb.append(str);
|
|
sb.append(th);
|
|
sb.append('\n');
|
|
StackTraceElement[] stackTrace = th.getStackTrace();
|
|
for (StackTraceElement stackTraceElement : stackTrace) {
|
|
sb.append("\t\tat ");
|
|
sb.append(stackTraceElement);
|
|
sb.append('\n');
|
|
}
|
|
if (th.getCause() != null) {
|
|
sb.append("\tCaused by: ");
|
|
a(sb, th.getCause(), "");
|
|
}
|
|
}
|
|
|
|
public List<Throwable> b() {
|
|
return this.exceptions;
|
|
}
|
|
|
|
public final void c(b bVar) {
|
|
StringBuilder sb = new StringBuilder(128);
|
|
sb.append(this);
|
|
sb.append('\n');
|
|
StackTraceElement[] stackTrace = getStackTrace();
|
|
for (StackTraceElement stackTraceElement : stackTrace) {
|
|
sb.append("\tat ");
|
|
sb.append(stackTraceElement);
|
|
sb.append('\n');
|
|
}
|
|
int i = 1;
|
|
for (Throwable th : this.exceptions) {
|
|
sb.append(" ComposedException ");
|
|
sb.append(i);
|
|
sb.append(" :\n");
|
|
a(sb, th, "\t");
|
|
i++;
|
|
}
|
|
synchronized (bVar.a()) {
|
|
bVar.b(sb.toString());
|
|
}
|
|
}
|
|
|
|
@Override // java.lang.Throwable
|
|
public synchronized Throwable getCause() {
|
|
if (this.cause == null) {
|
|
a aVar = new a();
|
|
HashSet hashSet = new HashSet();
|
|
Iterator<Throwable> it = this.exceptions.iterator();
|
|
Throwable th = aVar;
|
|
while (it.hasNext()) {
|
|
Throwable next = it.next();
|
|
if (!hashSet.contains(next)) {
|
|
hashSet.add(next);
|
|
ArrayList arrayList = new ArrayList();
|
|
Throwable cause = next.getCause();
|
|
if (cause != null) {
|
|
if (cause != next) {
|
|
while (true) {
|
|
arrayList.add(cause);
|
|
Throwable cause2 = cause.getCause();
|
|
if (cause2 == null) {
|
|
break;
|
|
} else if (cause2 == cause) {
|
|
break;
|
|
} else {
|
|
cause = cause.getCause();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Iterator it2 = arrayList.iterator();
|
|
while (it2.hasNext()) {
|
|
Throwable th2 = (Throwable) it2.next();
|
|
if (hashSet.contains(th2)) {
|
|
next = new RuntimeException("Duplicate found in causal chain so cropping to prevent loop ...");
|
|
} else {
|
|
hashSet.add(th2);
|
|
}
|
|
}
|
|
try {
|
|
th.initCause(next);
|
|
} catch (Throwable unused) {
|
|
}
|
|
Throwable cause3 = th.getCause();
|
|
if (!(cause3 == null || cause3 == th)) {
|
|
while (true) {
|
|
Throwable cause4 = cause3.getCause();
|
|
if (cause4 == null || cause4 == cause3) {
|
|
break;
|
|
}
|
|
cause3 = cause3.getCause();
|
|
}
|
|
th = cause3;
|
|
}
|
|
}
|
|
}
|
|
this.cause = aVar;
|
|
}
|
|
return this.cause;
|
|
}
|
|
|
|
@Override // java.lang.Throwable
|
|
public String getMessage() {
|
|
return this.message;
|
|
}
|
|
|
|
@Override // java.lang.Throwable
|
|
public void printStackTrace() {
|
|
printStackTrace(System.err);
|
|
}
|
|
|
|
@Override // java.lang.Throwable
|
|
public void printStackTrace(PrintStream printStream) {
|
|
c(new c(printStream));
|
|
}
|
|
|
|
@Override // java.lang.Throwable
|
|
public void printStackTrace(PrintWriter printWriter) {
|
|
c(new d(printWriter));
|
|
}
|
|
}
|