简体   繁体   中英

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.

    @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> Class<? extends Enum> .

Now you can do:

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

Now this can be passed to allOf and noneOf :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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