我有一个方法getInstanceOfCause(),该方法接受一个异常类和一个Throwable,遍历Throwable的原因及其原因,并返回与作为第一个参数传递的类匹配的cause的实例。看起来像这样:
public class ExceptionUtil {
public static <T> T getInstanceOfCause(Class<? extends Throwable> expected, Throwable exc) {
return (expected.isInstance(exc)) ? (T) exc : getInstanceOfCause(expected, exc.getCause());
}
}
让我们假设期望的类型确实在“原因链”中,并且该调用不会导致NPE。我可以这样使用:
MyException myException = ExceptionUtil.<MyException>getInstanceOfCause(MyException.class, exception);
这很尴尬,因为我必须两次指定类型。有什么方法可以重写方法签名,以便我可以像下面这样使用它,同时仍确保在编译时该类型是Throwable的子类?
MyException myException = ExceptionUtil.getInstanceOfCause(MyException.class, exception);
或
MyException myException = ExceptionUtil.<MyException>getInstanceOfCause(exception);
答案 0 :(得分:1)
请注意,可以从您当前的方法签名中推断出T
。一个问题是您可以这样称呼它:
Foo foo = ExceptionUtil.getInstanceOfCause(MyException.class, exception);
这没有道理。
我想您要保证返回值类型和第一个参数的类类型相同?
您可以使用通用参数T
:
public static <T extends Throwable> T getInstanceOfCause(Class<T> expected, Throwable exc) {
return (expected.isInstance(exc)) ? (T) exc : getInstanceOfCause(expected, exc.getCause());
}
请注意如何将T
约束为Throwable
,并在Class<T> expected
和返回值类型中使用它。这样可以保证返回值的类型与传入的类的类型相同。