com.bluecatcode.common.exceptions.Exceptions Maven / Gradle / Ivy
package com.bluecatcode.common.exceptions;
import com.bluecatcode.common.contract.errors.RequireViolation;
import com.bluecatcode.common.functions.CheckedFunction;
import com.google.common.base.Function;
import javax.annotation.Nonnull;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import static com.bluecatcode.common.contract.Preconditions.require;
public class Exceptions {
private Exceptions() {
throw new UnsupportedOperationException();
}
public static WrappedException wrap(@Nonnull Exception cause) {
return WrappedException.wrap(cause);
}
public static E wrap(@Nonnull Exception cause, Class type) {
return exception(type, parameters(Throwable.class), arguments(cause));
}
public static Function uncheckedException() {
return e -> e instanceof RuntimeException ? (RuntimeException) e : new UncheckedException(e);
}
public static Function unwrapToUncheckedException() {
return e -> uncheckedException().apply(e.unwrap());
}
public static E exception(Class throwableType) {
return throwable(throwableType);
}
public static E throwable(Class throwableType) {
return throwable(throwableType, parameters(), arguments());
}
public static E exception(Class throwableType,
CheckedFunction, Constructor, ReflectiveOperationException> constructorSupplier,
CheckedFunction, E, ReflectiveOperationException> instanceSupplier) {
return throwable(throwableType, constructorSupplier, instanceSupplier);
}
public static E throwable(Class throwableType,
CheckedFunction, Constructor, ReflectiveOperationException> constructorSupplier,
CheckedFunction, E, ReflectiveOperationException> instanceSupplier) {
require(throwableType != null, "Expected non-null throwableType");
require(constructorSupplier != null, "Expected non-null constructorSupplier");
require(instanceSupplier != null, "Expected non-null instanceSupplier");
require(!Modifier.isAbstract(throwableType.getModifiers()),
"Expected non-abstract throwable type, got: '%s'", throwableType.getCanonicalName());
final Constructor constructor;
try {
constructor = constructorSupplier.apply(throwableType);
require(constructor != null, "Expected constructor supplier to return non-null reference");
} catch (ReflectiveOperationException e) {
throw new RequireViolation("Expected a throwable with (String) constructor", e);
}
try {
E instance = instanceSupplier.apply(constructor);
require(instance != null, "Expected instance supplier to return non-null reference");
return instance;
} catch (ReflectiveOperationException e) {
throw new RequireViolation("Expected an instantiable throwable.", e);
}
}
public static CheckedFunction, Constructor, ReflectiveOperationException> parameters(Class> parameterType) {
return type -> type.getConstructor(parameterType);
}
public static CheckedFunction, Constructor, ReflectiveOperationException> parameters(Class>... parameterTypes) {
return type -> type.getConstructor(parameterTypes);
}
public static CheckedFunction, E, ReflectiveOperationException> arguments(Object arg) {
return constructor -> constructor.newInstance(arg);
}
public static CheckedFunction, E, ReflectiveOperationException> arguments(Object... args) {
return constructor -> constructor.newInstance(args);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy