[英]Is it possible to create a custom annotation with a group of annotations in Java?
I use lombok and JPA in my system. 我在我的系统中使用了lombok和JPA。 So for the entity classes, all of them looks like:
所以对于实体类,它们看起来像:
@Getter
@Setter
@Entity
@NoArgsConstructor
@AllArgsConstructor
public class XxxEntity {
...
}
So my question can I create a custom annotation to grouping these all these annotations? 所以我的问题是,我可以创建一个自定义注释来分组这些所有这些注释吗?
so it may look like: 所以它可能看起来像:
@CustomAnnotation
public class XxxEntity {
...
}
And when I use the @CustomAnnotation, it will apply all the above annotation to that class. 当我使用@CustomAnnotation时,它会将所有上述注释应用于该类。
Is this possible? 这可能吗?
Thanks. 谢谢。
No, it's not possible. 不,这是不可能的。 You can lump your annotations together using a new annotation, but the annotation processor won't understand it.
您可以使用新注释将注释混合在一起,但注释处理器将无法理解它。 The
@Caching
annotation from Spring is just an example of an annotation known by the annotation processor . Spring中的
@Caching
注释只是注释处理器已知注释的一个示例。 If you create an own annotation by copying @Caching
, I'd bet you'll see that it does not work. 如果你通过复制
@Caching
创建一个自己的注释,我敢打赌你会发现它不起作用。
In theory, it could work as an annotation processor can read any annotation and look inside. 从理论上讲,它可以作为注释处理器读取任何注释并查看内部。 However, it's totally unclear how an unknown annotation should be processed.
但是,完全不清楚如何处理未知的注释。 Putting other annotations inside has no standardized meaning.
在其中放入其他注释没有标准化的含义。
Especially, this does not work for Lombok. 特别是,这对龙目岛不起作用。 There are a few issues requesting this, eg, here , but it's pretty complicated.
有一些问题需要这个,例如, 这里 ,但它非常复杂。
The ultimate solution would be an annotation pre-processor understanding compound annotations and replacing them by their constituents before ordinary annotation processors run. 最终的解决方案是注释预处理器理解复合注释,并在普通注释处理器运行之前用它们的成分替换它们。 But there's no such thing and AFAIK no plans for it.
但是没有这样的事情和AFAIK没有计划。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.