相关疑难解决方法(0)

用例实现注释

什么是实现注释的有效用例?

在设计主要基于注释的配置系统时,我偶尔需要创建实现代码生成或编程配置注释的类.

替代方案涉及将注释中包含的数据镜像到DTO中,这似乎是一种开销.

这是一个例子:

public enum IDType {
    LOCAL,
    URI,
    RESOURCE;
}

@Documented
@Target( { METHOD, FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface Id {
    /**
     * @return
     */
    IDType value() default IDType.LOCAL;
}
Run Code Online (Sandbox Code Playgroud)

随着实施

public class IdImpl implements Id{

    private final IDType idType;

    public IdImpl(IDType idType){
        this.idType = idType;
    }

    @Override
    public IDType value() {
        return idType;
    }

    @Override
    public Class<? extends Annotation> annotationType() {
        return Id.class;
    }

}
Run Code Online (Sandbox Code Playgroud)

我得到了编译器警告,但它似乎是许多用例的有效工具.

以上示例的警告是

注释类型Id不应用作IdImpl的超接口

编辑:

我刚从Guice找到这个例子:

bind(CreditCardProcessor.class)
    .annotatedWith(Names.named("Checkout"))
    .to(CheckoutCreditCardProcessor.class);
Run Code Online (Sandbox Code Playgroud)

从名称中 …

java annotations

37
推荐指数
1
解决办法
1万
查看次数

标签 统计

annotations ×1

java ×1