简体   繁体   English

Java 枚举实现接口作为自定义注释参数

[英]Java enum implementing interface as custom annotation parameter

I'm creating an custom annotation with parameters.我正在创建带有参数的自定义注释。 Parameters I want to use are enums that implement an interface.我想使用的参数是实现接口的枚举。 There are few enums that are candidates to b used as this annotation's parameter.很少有枚举可以用作此注释的参数。 Here are some code examples:以下是一些代码示例:

Interface:界面:

public interface MyInterface {
    String getSomething();
    int getMore();
}

Enum example:枚举示例:

public enum MyEnum implements MyInterface {

    VAL1("some1", 1),
    VAL2("2val", 22);

    private String something;
    private int more;

    private MyEnum(String something, int more) {
        this.something = something;
        this.more = more;
    }

    public String getSomething() {
        return something;
    }

    public int getMore() {
        return more;
    }
}

I want to implement an annotation with parameter that can be retrieved as multiple MyInterface values.我想使用可以作为多个 MyInterface 值检索的参数来实现注释。 Let's say annotation looks like this:假设注释看起来像这样:

@MyAnnotation(MyEnum.class)

or或者

@MyAnnotation(MyEnum.values())

So I can further retrieve it and pass it to a method that accepts Collection parameter.所以我可以进一步检索它并将它传递给接受 Collection 参数的方法。

How do I implement an annotation with a parameter so I can retrieve all values of enum as MyInterface type?如何使用参数实现注释,以便我可以将枚举的所有值检索为 MyInterface 类型?

I was trying to do it like this:我试图这样做:

public @interface MyAnnotation {
    Class<? extends Enum<?> & MyInterface> myValues();
}

So I could do:所以我可以这样做:

Collection<MyInterface> desired = Arrays.asList(... .getAnnotation().myValues().enumValues());

but this syntax is invalid.但此语法无效。

You can't use union types in an annotation member, so you'll have to give up on that idea.您不能在注释成员中使用联合类型,因此您将不得不放弃这个想法。

But frame challenge: why do you even care that the implementer has to be an enum?但是框架挑战:为什么你甚至关心实现者必须是一个枚举?

What I assume you care about is that something can give you a finite set of implementations.我假设您关心的是某些东西可以为您提供一组有限的实现。 An enum is one way to achieve that, but why be overly restrictive if someone wants to give you a list of values some other way?枚举是实现这一目标的一种方式,但如果有人想以其他方式为您提供值列表,为什么要过度限制呢?

I'd add a new interface which can provide a collection of values.我会添加一个可以提供值集合的新接口。

interface MyInterfaceValueSource {
    List<MyInterface> values();
}

The enum gets a very simple implementation that just calls MyEnum.values() .枚举有一个非常简单的实现,只调用MyEnum.values()

class MyEnumValueSource implements MyInterfaceValueSource {
    @Override
    public List<MyInterface> values() {
        return Arrays.asList(MyEnum.values());
    }
}

Your annotation can then be altered to accept one of those然后可以更改您的注释以接受其中之一

@interface MyAnnotation {
    Class<? extends MyInterfaceValueSource> value();
}

and example of its usage would be它的用法示例是

@MyAnnotation(MyEnumValueSource.class)

Now if someone wants to provide a list of values but not implement it as an enum, they have the flexibility to do that.现在,如果有人想提供一个值列表而不是将其实现为枚举,他们可以灵活地做到这一点。

class ExampleValueSource implements MyInterfaceValueSource {
    @Override
    public List<MyInterface> values() {
        return Arrays.asList(
            new MyInterfaceOne(), new MyInterfaceTwo()
        );
    }
}

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

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