我有一个用于定义方法或字段的注释,如下所示:
@Retention(RetentionPolicy.RUNTIME)
@Target(value = {ElementType.METHOD, ElementType.FIELD})
public @interface NotColumn {
}
Run Code Online (Sandbox Code Playgroud)
我想阻止用户在记录上使用此注释,因为在该上下文中使用此注释没有意义。看来这样做不应该编译,因为我没有指定ElementType.PARAMETER为有效的@Target.
不过,以下编译良好:
public record MyRecord(String customerId,
String companyName,
@NotColumn String description
}
Run Code Online (Sandbox Code Playgroud)
但是这种带有紧凑构造函数的形式无法使用“ java:注释类型不适用于这种声明”进行编译 - 这实际上是我所期望的。
public record MyRecord(String customerId,
String companyName,
@NotColumn String description
public MyRecord {
}
}
Run Code Online (Sandbox Code Playgroud)