使用 AspectJ 将一个注释变成多个注释
我在 JPA 映射中发现了一种模式,我想对其进行编码。一个简单的示例如下:
@OneToMany(fetch=FetchType.EAGER)
@Sort(type=SortType.NATURAL)
private SortedSet<Item> items;
我想创建一个名为 SortedOneToMany 的单个注释,我可以将其应用于上面的集合:
public @interface SortedOneToMany {
FetchType fetch() default EAGER;
SortType sort() default NATURAL;
Class comparator() default void.class;
}
我编写了以下方面,以便在看到我的注释时“附加”JPA 注释:
public aspect SortedOneToManyAspect {
declare @field: @SortedOneToMany * * : @OneToMany(fetch=FetchType.EAGER);
declare @field: @SortedOneToMany * * : @Sort(type=SortType.NATURAL);
}
但我不知道如何我可以访问 SortedOneToMany 注释参数的值并在定义 OneToMany 和 Sort 注释时使用它们吗?在某些情况下,我可能想更改默认值之一,如下所示:
@SortedOneToMany(sort=SortType.COMPARATOR,comparator=ItemComparator.class)
private SortedSet<Item> items;
那么如何将注释值从 SortedOneToMany 传递到 Sort 注释?
I have discovered a pattern in my JPA mappings that I would like to codify. A simple example follows:
@OneToMany(fetch=FetchType.EAGER)
@Sort(type=SortType.NATURAL)
private SortedSet<Item> items;
I would like to create a single annotation called SortedOneToMany that I can apply to the above set:
public @interface SortedOneToMany {
FetchType fetch() default EAGER;
SortType sort() default NATURAL;
Class comparator() default void.class;
}
I have written the following aspect to "attach" the JPA annotations whenever it sees my annotation:
public aspect SortedOneToManyAspect {
declare @field: @SortedOneToMany * * : @OneToMany(fetch=FetchType.EAGER);
declare @field: @SortedOneToMany * * : @Sort(type=SortType.NATURAL);
}
But I don't know how can I access the values of the SortedOneToMany annotation parameters and use them when defining the OneToMany and Sort annotations. There may be cases where I want to change one of the default values like so:
@SortedOneToMany(sort=SortType.COMPARATOR,comparator=ItemComparator.class)
private SortedSet<Item> items;
So how can I pass the annotation values from SortedOneToMany to the Sort annotation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在aspectj-users 邮件列表上收到了 Andy Clement 的回答:
我为该问题创建了一张票,以防有人想要跟踪进度:https: //bugs.eclipse.org/bugs/show_bug.cgi?id=345515
I received this answer from Andy Clement on the aspectj-users mailing list:
I created a ticket for the issue in case anyone wants to follow the progress: https://bugs.eclipse.org/bugs/show_bug.cgi?id=345515