com.turbospaces.logging.ClassUtils Maven / Gradle / Ivy
The newest version!
package com.turbospaces.logging;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class ClassUtils {
private static final Logger logger = LoggerFactory.getLogger(ClassUtils.class);
private ClassUtils() {
}
public static Optional> parseThrowableClass(String classStr) {
try {
Class clazz = Class.forName(classStr.trim());
if (Throwable.class.isAssignableFrom(clazz)) {
return Optional.of((Class extends Throwable>) clazz);
}
logger.error("Class is not Throwable: {}", classStr);
} catch (Exception ex) {
logger.error("Class can't be parsed: {}", classStr, ex);
}
return Optional.empty();
}
public static List> parseThrowableClasses(String classesStr, String separator) {
List> exceptions = new ArrayList<>();
if (!classesStr.trim().isEmpty()) {
for (String exClass : classesStr.split(separator)) {
ClassUtils.parseThrowableClass(exClass).ifPresent(exceptions::add);
}
}
return exceptions;
}
}