【问题标题】:Limit annotations to non-final fields将注释限制为非最终字段
【发布时间】:2013-11-29 03:55:03
【问题描述】:

我正在使用 Java 中的自定义注释。如果有人试图在 final 字段上使用注解,有什么方法可以在编译时抛出错误?

这是我当前的注释:

@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Property {
    public String value();
}

例如,这是完全合法的:@Property("test") public String str;

然而,这不是:@Property("test2") public final String str2;

【问题讨论】:

    标签: java reflection annotations field final


    【解决方案1】:

    编译时使用Checker Framework检查,字段是否有修饰符final

    https://checkerframework.org/

    【讨论】:

    • @mernst 感谢您更新我的答案和很棒的工具!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多