devutility.internal.lang.ExceptionUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of devutility.internal Show documentation
Show all versions of devutility.internal Show documentation
Some utilities for Java development
package devutility.internal.lang;
import java.util.ArrayList;
import java.util.List;
import devutility.internal.base.SystemUtils;
public class ExceptionUtils {
public static String toString(Exception exception) {
return toString(exception, SystemUtils.lineSeparator());
}
public static String toString(Exception exception, String separator) {
StringBuilder result = new StringBuilder();
List list = toList(exception);
for (int i = 0; i < list.size(); i++) {
result.append(list.get(i));
if (i < list.size() - 1) {
result.append(separator);
}
}
return result.toString();
}
public static List toList(Exception exception) {
List list = new ArrayList<>();
if (exception == null) {
return list;
}
list.add(String.format("%s: %s", exception.getClass().getName(), exception.getMessage()));
StackTraceElement[] stackTraceElements = exception.getStackTrace();
if (stackTraceElements == null || stackTraceElements.length == 0) {
return list;
}
for (StackTraceElement stackTraceElement : stackTraceElements) {
list.add(String.format(" at %s", stackTraceElement.toString()));
}
return list;
}
}