Skip to content

Commit

Permalink
dbeaver/pro#3643 Utils refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
serge-rider committed Nov 13, 2024
1 parent b5c1f30 commit 1e3731a
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions modules/org.jkiss.utils/src/org/jkiss/utils/CommonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,33 @@ public static Throwable getRootCause(@NotNull Throwable ex) {
return rootCause;
}

@Nullable
public static <T extends Exception> T getCauseOfType(@NotNull Throwable ex, Class<T> causeClass) {
Throwable rootCause = ex;
for (; ; ) {
if (causeClass.isInstance(rootCause)) {
return causeClass.cast(rootCause);
}
if (rootCause.getCause() != null) {
rootCause = rootCause.getCause();
} else if (rootCause instanceof InvocationTargetException ite && ite.getTargetException() != null) {
rootCause = ite.getTargetException();
} else {
break;
}
}
return null;
}

public static boolean hasCause(Throwable ex, Class<? extends Throwable> causeClass) {
for (Throwable e = ex; e != null; e = e.getCause()) {
if (causeClass.isAssignableFrom(e.getClass())) {
return true;
}
}
return false;
}

public static boolean equalObjects(@Nullable Object o1, @Nullable Object o2) {
if (o1 == o2) {
return true;
Expand Down Expand Up @@ -1136,4 +1163,5 @@ public static String getAllExceptionMessages(Throwable e) {
}
return String.join(":\n", result);
}

}

0 comments on commit 1e3731a

Please sign in to comment.