简体   繁体   English

Java泛型,实施枚举参数

[英]Java Generics, Enforcing an Enum Parameters

I'm playing around with building a factory method for generating RuntimeExceptions. 我正在玩构建生成RuntimeExceptions的工厂方法。

The idea would be to throw an exception by performing the following snippet: 想法是通过执行以下代码段抛出异常:

throw ExceptionFactory.build(CustomException.class, CustomException.NOT_FOUND);

The first parameter to the build method is the exception class, however the second parameter would reference an Enum that's defined within the CustomException class for loading up additional details when building the exception. 构建方法的第一个参数是异常类,但是第二个参数将引用在CustomException类中定义的Enum,以便在构建异常时加载其他详细信息。

Example: 例:

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..

  }

}

The question I have is with the ExceptionFactory.build(), how do I specify the parameters for the build() method such that the second parameter must be specific to the CustomException class? 我的问题是ExceptionFactory.build(),我如何指定build()方法的参数,以便第二个参数必须特定于CustomException类?

If this approach sounds crazy, how could it be improved? 如果这种方法听起来很疯狂,怎么可能改进? The goal is to have a generic factory method for building exceptions that have details already pre loaded. 目标是使用通用工厂方法来构建已预先加载详细信息的异常。 What I want is to avoid is something like this.. 我想要避免的是这样的事情..

ExceptionFactory.build(CustomException.class, "Some string...")

The idea is the description needs to be defined in the CustomException and not just anything when throwing the error. 想法是需要在CustomException中定义描述,而不是在抛出错误时定义任何内容。 So how to enforce?? 那么如何执行?

public class ExceptionFactory {

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

}

You could use a marker interface: 您可以使用标记界面:

interface ExceptionTypeEnum<T extends RuntimeException> {}

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

public static <T extends RuntimeException> T build(T e, ExceptionTypeEnum<T> type) {

Instead of using an ExceptionFactory something something, one way of implementing this would be to use factory methods in your exception types. 实现这一点的一种方法是在异常类型中使用工厂方法,而不是使用ExceptionFactory For example: 例如:

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

This makes sure that any CustomException users of your code get comes from one of your factory methods that populate the exception message with the information you chose. 这可以确保代码的任何CustomException用户都来自您使用您选择的信息填充异常消息的工厂方法之一。 You can also add whatever arguments you want to the static factory methods, so different exception causes can require different parameters in the factory method. 您还可以向静态工厂方法添加所需的任何参数,因此不同的异常原因可能需要在工厂方法中使用不同的参数。

I'm not sure I actually see much value in what you propose to do. 我不确定我真的看到你打算做什么很有价值。 It's not clear to me how a complicated factory mechanism for generating exceptions improves on instantiating them directly where and when they are needed. 我不清楚生成异常的复杂工厂机制如何在需要的地方和时间直接实例化它们。 Nevertheless, you can approach this issue by making your enums more functional. 不过,您可以通过使枚举更具功能来解决此问题。

For example, 例如,

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);

ExceptionFactory.build() need only require an argument of type ExceptionEnum . ExceptionFactory.build()只需要一个ExceptionEnum类型的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM