Java泛型,强制使用枚举参数

Java泛型,强制使用枚举参数,java,generics,enums,Java,Generics,Enums,我正在尝试构建一个工厂方法来生成运行时异常 可以通过执行以下代码段来引发异常: throw ExceptionFactory.build(CustomException.class, CustomException.NOT_FOUND); 构建方法的第一个参数是exception类,但是第二个参数将引用CustomException类中定义的枚举,以便在构建异常时加载其他详细信息 例如: public class CustomException extends RuntimeException

我正在尝试构建一个工厂方法来生成运行时异常

可以通过执行以下代码段来引发异常:

throw ExceptionFactory.build(CustomException.class, CustomException.NOT_FOUND);
构建方法的第一个参数是exception类,但是第二个参数将引用CustomException类中定义的枚举,以便在构建异常时加载其他详细信息

例如:

public class CustomException extends RuntimeException {

  public static final ExceptionType NOT_FOUND = ExceptionType.NOT_FOUND;



  //constructors, getters, setters, etc..



  private enum ExceptionType {

    NOT_FOUND(Status.NOT_FOUND, "These aren't the droids you're looking for!");

    private Status status;
    private String description;

    private ExceptionType(Status status, String description){
      this.status = status;
      this.description = description;
    }

    //other methods here..

  }

}
我的问题是关于ExceptionFactory.build(),如何为build()方法指定参数,以便第二个参数必须特定于CustomException类

如果这种方法听起来很疯狂,那么如何改进呢?我们的目标是拥有一个通用的工厂方法来构建已经预加载了详细信息的异常。我想避免的是这样的事情

ExceptionFactory.build(CustomException.class, "Some string...")
其思想是需要在CustomException中定义描述,而不仅仅是在抛出错误时定义任何内容。那么如何执行

public class ExceptionFactory {

  public static <T extends RuntimeException> T build(T e, ???){
    //more logic here...
  }

}
公共类例外工厂{
公共静态T构建(TE,?){
//更多的逻辑在这里。。。
}
}

您可以使用标记界面:

interface ExceptionTypeEnum<T extends RuntimeException> {}

private enum ExceptionType implements ExceptionTypeEnum<CustomException> {
    ...
}

public static <T extends RuntimeException> T build(T e, ExceptionTypeEnum<T> type) {
接口异常类型枚举{}
私有枚举ExceptionType实现ExceptionTypeEnum{
...
}
公共静态T生成(TE,ExceptionTypeEnum类型){

实现这一点的一种方法是在异常类型中使用工厂方法,而不是使用异常工厂。例如:

public class CustomException extends RuntimeException {
   private CustomException(String description) {...}
   public static CustomException notFound() {
     return new CustomException("not found");
   }
   ....
}

这将确保代码的任何
CustomException
用户都来自于使用所选信息填充异常消息的工厂方法之一。您还可以向静态工厂方法添加任何参数,因此不同的异常原因可能需要工厂方法中的不同参数。

我不确定您的建议是否真的有多大价值。我不清楚生成异常的复杂工厂机制如何在需要时在何处直接实例化异常。不过,您可以通过使枚举更具功能性来解决此问题

比如说,

public interface ExceptionEnum {
    public Class<? extends RuntimeException> getExceptionClass();
}

public class CustomException extends RuntimeException {

    enum Details implements ExceptionEnum {
        GOOD, BAD, UGLY;

        public Class<? extends RuntimeException> getExceptionClass() {
            return CustomException.class;
        }
    };

    // ...
}

// ...

throw ExceptionFactory.build(CustomException.GOOD);
公共接口例外{

public ClassIs是不是有几种不同的自定义异常类型,每种类型都有这种枚举功能?我认为这不太可能实现。您可以使用类似于生成器的语法,但是,Java类型参数语言没有任何方法来描述类型的属性,而不是enum?@byte crunch包含type.SLaks的想法可能会奏效,但我不认为这或者你最初的想法会把你带到你真正想去的任何地方。你可能会简化设计,最终变成类似于
throw footexception.NOT_FOUND.build()
。枚举包含生成异常所需的所有信息。或者只
抛出footexception.notFound()
@bayou.io,这是一个很好的观点,我认为你的建议是一个更干净的方法。如果你知道你想要实例化的类,并且知道传递给它的适当参数,我不知道
如何抛出ExceptionFactory.build(FooException.class,FooException.NOT\u FOUND)
是对仅仅
抛出新的FooException的改进(未找到异常)