证明枚举课程是枚举类,当他们通过或存储时

发布于 2025-02-03 04:45:22 字数 1048 浏览 2 评论 0原文

如果我传递到一种方法或存储在变量中,那么我知道的所有类都是枚举的集合(理想保证,但我不介意铸造),是否可以让编译器知道它们是枚举类,为了使用values()enumset.allof()等的方法?即,我正在寻找无法直接参考枚举类的名称的情况。

我相信这是不可能的,但想验证。

一个示例可能看起来像这样:

enumClasses.stream()
        .map(eclass -> EnumSet.allOf(eclass))
        ... more here...

但是我看不出一种在枚举(作为变量或参数)声明中证明它只是枚举。

示例:我尝试过的某些情况,这些情况无法使用class;? lt;?

    List<? extends Class<? extends Enum<?>>> enums = List.of(MyEnum.class); 
    enums.forEach(eclass -> EnumSet.allOf(eclass)); // error here.

    Class<? extends Enum<?>> enumClass = MyEnum.class;
    EnumSet.allOf(enumClass); // error here.
    enumClass.values(); // error here.

了Enum

static <E extends Enum<E>> EnumSet myValues(Class<E> iEnumClass) {
    return EnumSet.allOf(iEnumClass);
}

扩展 >该方法(除非我直接使用类名来调用该方法,例如myValues(myenum.class)

If I pass to a method, or store in a variable, a collection of classes that I know are all Enums (ideally guaranteed, but I don't mind casting), is there a way to let the compiler know they are enum classes, in order to use methods like values(), EnumSet.allOf(), etc? I.e., I'm looking at cases where I cannot refer to the names of the Enum classes directly.

I believe this is not possible, but wanted to verify.

An example might look something like this:

enumClasses.stream()
        .map(eclass -> EnumSet.allOf(eclass))
        ... more here...

but I don't see a way to prove in the declaration of enumClasses (as variable or parameter) that it's only enums.

Examples: Some cases I tried that did not work using Class<? extends Enum<?>>>

    List<? extends Class<? extends Enum<?>>> enums = List.of(MyEnum.class); 
    enums.forEach(eclass -> EnumSet.allOf(eclass)); // error here.

or

    Class<? extends Enum<?>> enumClass = MyEnum.class;
    EnumSet.allOf(enumClass); // error here.
    enumClass.values(); // error here.

I also tried creating this helper-method signature:

static <E extends Enum<E>> EnumSet myValues(Class<E> iEnumClass) {
    return EnumSet.allOf(iEnumClass);
}

and the method compiles fine, but I have the same problems as above when I try to call the method (unless I call that method directly with the class-name, like myValues(MyEnum.class))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恋竹姑娘 2025-02-10 04:45:22

我在a 链接问题 class> class> class称为getEnumconstants()上有一种方法,

(如果不是枚举,它只是返回null,但在我的情况下,我知道它是。isenum()(如果需要)

List<Class<?>> enumClasses; // or List<Class<? extends MyInterface>>
enumClasses.stream()
    .map(Class::getEnumConstants)
    .flatMap(Arrays::stream)
    ...

。正如我在标题中提出的那样,但它确实解决了我的用例,这是为了收集几个相同面积的枚举的值。

I discovered in a linked question that there's a method on Class called getEnumConstants()

(It simply returns null if it's not an enum, but in my case I know that it is. isEnum() is also provided if needed.)

List<Class<?>> enumClasses; // or List<Class<? extends MyInterface>>
enumClasses.stream()
    .map(Class::getEnumConstants)
    .flatMap(Arrays::stream)
    ...

This doesn't directly solve the question as I posed it in the title, but it does solve the use-case I had, which was to collect the values of several same-interface enums.

锦爱 2025-02-10 04:45:22

您存在的问题是在两个enumset.allof()上都存在的&lt; e扩展了enum&lt; e&gt;&gt;(以及enumset类本身)和您的myValues()方法。基本上,您需要一个“类的列表,每个类别的enum本身”,并且无法在Java中表达出来。

使用class&lt;?扩展了enum&lt;?&gt;&gt;,您正在放弃按本身继承enum的要求,因此可以将一些无效的值放在其中。例如,某人可以将enum.class本身(代表enum类本身)放置在其中。或者,有人可以用枚举常数进行枚举,以实现自定义方法。这样的枚举常数将是枚举的匿名子类的一个实例。因此,在此上运行.getClass()将为您提供class,它是enum的子类,但它不是它的实际类您想要的枚举。

如果您想安全地保留枚举类的列表,则可能必须定义自己的自定义类listofenumclasses或类似的东西。 add()方法将是通用的,并在类型变量上执行关系,例如

public <E extends Enum<E>> void add(Class<E> clazz) { }

内部您仍然可以将其视为list&lt; class&lt;?扩展Enum&lt;?&gt;&gt;,但您不会将此变量暴露于外部。要将其中一个元素传递到enumset.allof()内部之类的内容,您可能必须将其施放到原始类型中才能关闭仿制药,例如:enumset.allof(class(class) )eclass)

The problem you are having is with the bound <E extends Enum<E>>, which exists on both EnumSet.allOf() (as well as the EnumSet class itself) and your myValues() method. Basically, you need a "list of classes, each of which inherits Enum of itself", and there is no way to express that in Java.

With Class<? extends Enum<?>>, you are giving up the requirement that the class inherit Enum of itself, so it's possible to put some invalid values in there. For example, someone could put Enum.class itself (the class object representing the Enum class itself) in there. Or, someone could take an enum with an enum constant that implements custom methods; such an enum constant would be an instance of an anonymous subclass of the enum. So running .getClass() on this would give you a Class which is a subclass of Enum, but it is not the actual class of the enum that you want.

If you want to hold a list of enum classes safely, you would probably have to define your own custom class ListOfEnumClasses or something like that. The add() method would be generic and enforce the relationship on the type variable, like

public <E extends Enum<E>> void add(Class<E> clazz) { }

Internally you might still hold it as a List<Class<? extends Enum<?>>, but you would not expose this variable to the outside. To pass one of the elements to something like EnumSet.allOf() internally, you would probably have to cast it to a raw type to turn off generics, like: EnumSet.allOf((Class) eclass)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文