Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

    }


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
547 views
Welcome To Ask or Share your Answers For Others

1 Answer

First, change the type of your annotation's value() to 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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share
...