简体   繁体   English

如何创建扩展 RuntimeException 的抽象泛型 class

[英]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.

相关问题 如何使用扩展类 <E extends Comparable<E> &gt; - How to extend Generic abstract Class with <E extends Comparable<E>> 测试从通用抽象 class 扩展的 class - Testing a class that extends from generic abstract class 扩展抽象类的解组类 - Unmarshaling class that extends abstract generic class 创建一个扩展AsyncTask的抽象类 - Create an abstract class that extends AsyncTask 如何声明扩展泛型的类 - How to declare class that extends generic that extends generic 如何创建可扩展类并实现接口的通用Java类? - how to create generic java class that extends class and implements interface? Java泛型。 如何正确扩展扩展通用抽象参数类型的类的参数类型的类 - Java Generics. How to properly extend parameter typed class that extends Generic abstract parameter typed class 如何从可扩展的类创建实例并实现通用接口? - how to create an instance from a class that extends comparable and implement generic interface? 如何创建仅允许特定 class 扩展/实现它的泛型类型 - How to create a generic type that only allow a specific class to extends/implements it 如何创建泛型 <T> 类,以便T扩展Collections <String> ? - How to create a generic <T> class so that T extends Collections<String>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM