package net.sourceforge.tuned; public final class ExceptionUtilities { public static Throwable getRootCause(Throwable t) { while (t.getCause() != null) { t = t.getCause(); } return t; } @SuppressWarnings("unchecked") public static T findCause(Throwable t, Class type) { while (t != null) { if (type.isInstance(t)) return (T) t; t = t.getCause(); } return null; } public static String getRootCauseMessage(Throwable t) { return getMessage(getRootCause(t)); } public static String getMessage(Throwable t) { String message = t.getMessage(); if (message == null || message.isEmpty()) { message = t.toString(); } return message; } @SuppressWarnings("unchecked") public static T wrap(Throwable t, Class type) { if (type.isInstance(t)) { return (T) t; } try { return type.getConstructor(Throwable.class).newInstance(t); } catch (Exception e) { throw new IllegalArgumentException(e); } } public static RuntimeException asRuntimeException(Throwable t) { return wrap(t, RuntimeException.class); } /** * Dummy constructor to prevent instantiation. */ private ExceptionUtilities() { throw new UnsupportedOperationException(); } }