苍穹-ExceptionUtil

package kd.bos.xdb.exception;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Constructor;
import kd.bos.bundle.Resources;
import kd.bos.xdb.exception.XdbException;


public final class ExceptionUtil {
public static String getStackTrace(String msg) {
return getStackTrace((Throwable)(new XdbException(msg)));
}

public static String getStackTrace(Throwable e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
return sw.toString();
}

public static RuntimeException asRuntimeException(Throwable e) {
return e instanceof RuntimeException?(RuntimeException)e:new RuntimeException(e);
}




public static RuntimeException asRuntimeException(String msg, Throwable e) {
return new RuntimeException(msg, e);
}

public static XdbException wrap(String msg) {
return new XdbException(msg);
}

public static XdbException wrap(Throwable e) {
return e instanceof XdbException?(XdbException)e:new XdbException(e);
}




public static XdbException wrap(String msg, Throwable e) {
return new XdbException(msg, e);
}


public static <T extends Exception> T as(Class<T> type, Throwable e) {
if(type.isAssignableFrom(e.getClass())) {
return (Exception)e;
} else {
Constructor[] cs = type.getConstructors(); Constructor[] arg2 = cs; int arg3 = cs.length;
for(int arg4 = 0; arg4 < arg3; ++arg4) { Constructor c = arg2[arg4];
if(c.getParameterCount() == 1 && Throwable.class.isAssignableFrom(c.getParameterTypes()[0])) {
try {
return (Exception)c.newInstance(new Object[]{e});
} catch (Exception arg7) {
throw wrap(e);
} }
}

throw new XdbException(Resources.get("bos-xdb", "ExceptionUtil_0", "异常类{0}无适合的构造方法。", new Object[]{type}));
}
}

public static <T extends Exception> T as(Class<T> type, String msg, Throwable e) {
if(type.isAssignableFrom(e.getClass())) {
return (Exception)e;
} else {
Constructor[] cs = type.getConstructors(); Constructor[] arg3 = cs; int arg4 = cs.length;
for(int arg5 = 0; arg5 < arg4; ++arg5) { Constructor c = arg3[arg5];
if(c.getParameterCount() == 2 && String.class.isAssignableFrom(c.getParameterTypes()[0]) && Throwable.class.isAssignableFrom(c.getParameterTypes()[1])) {

try {
return (Exception)c.newInstance(new Object[]{msg, e});
} catch (Exception arg8) {
throw wrap(e);
} }
}

throw new XdbException(Resources.get("bos-xdb", "ExceptionUtil_0", "异常类{0}无适合的构造方法。", new Object[]{type}));
}
}
}

posted @ 2021-12-07 11:15  DuoMi  阅读(50)  评论(0)    收藏  举报