我有一个用于方法或字段的注释,定义如下:
@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:注释类型对此类声明不适用"的错误提示 - 实际上这正是我所期望的。
public record MyRecord(String customerId,
String companyName,
@NotColumn String description
public MyRecord {
}
}
@AllArgsConstructor
的例子(https://github.com/rzwitserloot/lombok/blob/master/test/transform/resource/messages-delombok/ConstructorsOnRecord.java.messages)来解决你的问题。具体实现细节需要在该处跟进。 - Naman