简体   繁体   English

如何使用 Class 调用 EnumSet#(all|none)Of<!--?--> ?

[英]How to invoke EnumSet#(all|none)Of with a Class<?>?

I need to invoke EnumSet#nonOf or EnumSet#allOf with an unknown type of class.我需要使用未知类型的 class 调用EnumSet#nonOfEnumSet# allOf。

    @Override
    public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
        final EnumSetOfAll annotation = parameterContext.getParameter().getAnnotation(EnumSetOfAll.class);
        final Class<?> value = annotation.value();
        if (!value.isEnum()) {
            throw new ParameterResolutionException("value(" + value + ") is not an enum");
        }

        // How can I return the result of EnumSet#(all|none)Of invoked with value?

    }

First, change the type of your annotation's value() to Class<? extends Enum>首先,将注释的value()类型更改为Class<? extends Enum> Class<? extends Enum> . Class<? extends Enum>

Now you can do:现在你可以这样做:

final Class<? extends Enum> value = annotation.value();

Now this can be passed to allOf and noneOf :现在这可以传递给allOfnoneOf

EnumSet<?> set = EnumSet.allOf(value);

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

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