使用 AspectJ 将一个注释转换为多个注释 [英] Turning one annotation into many annotations with AspectJ

查看:20
本文介绍了使用 AspectJ 将一个注释转换为多个注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 JPA 映射中发现了一个我想要编纂的模式.一个简单的例子如下:

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;

我想创建一个名为 SortedOneToMany 的注释,我可以将其应用于上述集合:

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;
}

我编写了以下方面以在 JPA 看到我的注释时附加"它:

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);
}

但我不知道如何访问 SortedOneToMany 注释参数的值并在定义 OneToMany 和 Sort 注释时使用它们.在某些情况下,我可能想像这样更改默认值之一:

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;

那么如何将 SortedOneToMany 中的注释值传递给 Sort 注释?

So how can I pass the annotation values from SortedOneToMany to the Sort annotation?

推荐答案

我在 aspectj-users 邮件列表上收到了 Andy Clement 的答复:

I received this answer from Andy Clement on the aspectj-users mailing list:

恐怕你现在不能用 AspectJ 做到这一点,你不能通过与新注释匹配的信息.我也许可以想象一些假设的语法:

I'm afraid you can't do that with AspectJ right now, you can't pass a piece of the matched information to the new annotation. I can perhaps imagine some hypothetical syntax:

声明@field:@SortedOneToMany(sort=SortType.COMPARATOR,comparator={1}) * * :@Sort(type=SortType.COMPARATOR,comparator={1});

declare @field: @SortedOneToMany(sort=SortType.COMPARATOR,comparator={1}) * * : @Sort(type=SortType.COMPARATOR,comparator={1});

这似乎可以实现您想要的.

which would seem to achieve what you want.

也许会提出一个增强请求:https://bugs.eclipse.org/bugs/enter_bug.cgi?product=AspectJ

Maybe raise an enhancement request for it: https://bugs.eclipse.org/bugs/enter_bug.cgi?product=AspectJ

抱歉,我没有更好的消息.

sorry I don't have better news.

干杯安迪

我为这个问题创建了一张票,以防有人想跟踪进度:https://bugs.eclipse.org/bugs/show_bug.cgi?id=345515

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

这篇关于使用 AspectJ 将一个注释转换为多个注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆