注释限制非最终字段 [英] Limit annotations to non-final fields

查看:145
本文介绍了注释限制非最终字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java定制注释工作。有什么办法在编译的时候,如果有人试图使用注解上最后一个字段抛出一个错误?

I'm working with custom annotations in Java. Is there any way to throw an error at compile time if someone tries to use the annotation on a final field?

这是我目前的注解:

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

例如,这是完全合法的: @Property(测试)公共字符串str;

For example, this is perfectly legal: @Property("test") public String str;

然而,这是不是: @Property(测试2)公共最后字符串的STR2;

推荐答案

使用的检查框架检查编译时,如果字段有修改最后

use the checker framework to check when compiling, if the field has the modifier final

http://types.cs.washington.edu/checker-framework/

这篇关于注释限制非最终字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆