【发布时间】:2021-04-13 12:30:00
【问题描述】:
我有一个注释用于这样定义的方法或字段:
@Retention(RetentionPolicy.RUNTIME)
@Target(value = {ElementType.METHOD, ElementType.FIELD})
public @interface NotColumn {
}
我想阻止用户在记录上使用它,因为在该上下文中使用此注释没有意义。似乎这样做不应该编译,因为我没有将ElementType.PARAMETER 指定为有效的@Target。
以下编译正常:
public record MyRecord(String customerId,
String companyName,
@NotColumn String description
}
但是这种带有紧凑构造函数的表单无法使用“java: annotation type not applicable to this kind of declaration”进行编译——这实际上是我所期望的。
public record MyRecord(String customerId,
String companyName,
@NotColumn String description
public MyRecord {
}
}
【问题讨论】:
-
如果阻止您的意思是不让用户成功编译代码,那么您可以按照
@AllArgsConstructorfrom lombok 的示例为您的用例。实施细节需要在那里跟进。 -
@Naman 谢谢,但我的库没有依赖项。添加警告日志对我来说很容易,我会记录下来。
标签: java annotations java-record java-16