2013-11-29 26 views
2

我正在使用Java中的自定义注释。如果有人试图在最终字段上使用注释,有什么办法可以在编译时抛出错误?将注释限制为非最终字段

这是我目前的注解:

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

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

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

回答

1

使用检查框架要检查编译时,如果该字段有修饰符final

https://checkerframework.org/

+0

@mernst感谢你更新我的答案和真棒工具! –