打开一个注释与AspectJ的许多注解 [英] Turning one annotation into many annotations with AspectJ

查看:174
本文介绍了打开一个注释与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注释参数值和定义一对多时使用它们和排序注释。有可能,我想改变,像这样的默认值中的一个情况:

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的排序注释?

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

推荐答案

我收到这个答案来自安迪·克莱门特在AspectJ的用户邮件列表:

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(排序= SortType.COMPARATOR,比较= {1})*:
  @Sort(类型= SortType.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.

欢呼
  刘德华

cheers Andy

我创建了万一有人想跟着进步的问题一票: 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天全站免登陆