[英]How to create abstract generic class that extends RuntimeException
I want to create some generic abstract class:我想创建一些通用摘要 class:
public abstract class SomeDomainValidationException<E extends Enum<E>> extends ValidationException {
private final E hint;
public SomeDomainValidationException(String message, E hint) {
super(message);
this.hint = hint;
}
}
where:在哪里:
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public class ValidationException extends RuntimeException {
public ValidationException(String message) {
super(message);
}
}
I want to group different types of exceptions in my code, eg I want to create different types of exceptions like:我想在我的代码中对不同类型的异常进行分组,例如我想创建不同类型的异常,例如:
WarehouseMatchingException
extends SomeDomainValidationException<WarehouseWrongDatesEnum>
but of course I can't because generic class may not extend 'java.lang.Throwable'但我当然不能因为通用 class 可能不会扩展 'java.lang.Throwable'
How to create generic class that represents different types of exceptions included in some enums like WarehouseWrongDatesEnum
?如何创建通用的 class 来表示某些枚举(如
WarehouseWrongDatesEnum
)中包含的不同类型的异常?
You could use some interfaces to achieve something like this but this feels a little hacky.您可以使用一些接口来实现类似的目的,但这感觉有点老套。 I'll post it anyway to let you decide for yourself.
无论如何我都会发布它让你自己决定。
First, we'll create 3 interfaces: one to return the "raw" hin, one to return the "generic" hint and one to do a cast:首先,我们将创建 3 个接口:一个返回“原始”hin,一个返回“通用”提示,一个进行转换:
//return the raw hint - this is an internal interface
interface RawxceptionHint {
Object getRawHint();
}
//return the generic hint - this is a public interface
interface ExceptionHint<T>{
T getHint();
}
//adapter to cast the raw hint to the generic one
interface ExceptionHintAdapter<T> extends ExceptionHint<T>, RawxceptionHint {
default T getHint() {
return (T)getRawHint();
}
}
Now you can implement your exceptions:现在你可以实现你的异常:
public abstract class SomeDomainValidationException extends ValidationException implements RawxceptionHint {
private final Object hint;
public SomeDomainValidationException(String message, Object hint) {
super(message);
this.hint = hint;
}
public Object getRawHint() {
return hint;
}
}
class WarehouseMatchingException
extends SomeDomainValidationException
implements ExceptionHintAdapter<WarehouseWrongDatesEnum> {
WarehouseMatchingException (String message, WarehouseWrongDatesEnum hint) {
super(message, hint);
}
}
Now you can access the hint via Object getRawHint()
or WarehouseWrongDatesEnum getHint()
.现在您可以通过
Object getRawHint()
或WarehouseWrongDatesEnum getHint()
访问提示。 If you want to restrict access to getRawHint()
you could cast the exception to ExceptionHint<WarehouseWrongDatesEnum>
.如果您想限制对
getRawHint()
的访问,您可以将异常转换为ExceptionHint<WarehouseWrongDatesEnum>
。
Note : as stated initially this feels somewhat hacky and I'm not sure this would even provide any benefit.注意:如最初所述,这感觉有点老套,我不确定这是否会带来任何好处。 Maybe there's a better solution that I can't think of right now and which supports what you need/want.
也许有一个我现在想不到的更好的解决方案,它支持你需要/想要的东西。 However, not using that generic "middle class"
SomeDomainValidationException
and putting the hint and its methods directly into WarehouseMatchingException
would be the safer and less hacky option.但是,不使用那个通用的“中间类”
SomeDomainValidationException
并将提示及其方法直接放入WarehouseMatchingException
将是更安全且更简单的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.