我正在創建一個注釋,我需要匹配根本原因然后采取行動。這三個類作為完全限定名稱(如 javax.persistence.PersistenceException)在陣列中傳遞。當我在接受型別別以匹配 rootCause 的“instanceOf”中使用這些類時,它不起作用。我收到錯誤“class0 無法決議為型別”。我還嘗試了 class0.getClass() 來查找型別。任何建議將不勝感激。
public boolean findRootCauseOfException(Throwable throwable, String[] value) {
Objects.requireNonNull(throwable);
Throwable rootCause = throwable;
Object class0 = null;
Object class1 = null;
Object class2 = null;
try {
class0 = Class.forName(value[0]);
class1 = Class.forName(value[1]);
class2 = Class.forName(value[2]);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
while (rootCause != null) {
if (rootCause instanceof class0
|| rootCause instanceof class1
|| rootCause instanceof class2 ) {
return true;
}
rootCause = rootCause.getCause();
}
return false;
}
uj5u.com熱心網友回復:
instanceof僅適用于文字類,不適用于Class物件。
Class.isInstance
您必須在類上呼叫該isInstance方法。Class不要與instanceOf.
class0.isInstance(rootCause)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/412585.html
標籤:
