Turning one annotation into many annotations with AspectJ

I found a pattern in my JPA mappings that I would like to codify. The following is a simple example:

@OneToMany(fetch=FetchType.EAGER) @Sort(type=SortType.NATURAL) private SortedSet<Item> items; 

I would like to create a single SortedOneToMany annotation that I can apply to the set above:

 public @interface SortedOneToMany { FetchType fetch() default EAGER; SortType sort() default NATURAL; Class comparator() default void.class; } 

I wrote the following aspect to โ€œattachโ€ 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 do not know how I can access the values โ€‹โ€‹of the SortedOneToMany annotation parameters and use them when defining OneToMany and Sort annotations. There may be times when I want to change one of the default values:

 @SortedOneToMany(sort=SortType.COMPARATOR,comparator=ItemComparator.class) private SortedSet<Item> items; 

So, how can I pass annotation values โ€‹โ€‹from SortedOneToMany to Sort annotation?

+7
source share
1 answer

I received this response from Andy Clement on the mailing list for aspect users:

Hi,

I'm afraid you canโ€™t do this with AspectJ right now, you canโ€™t get past some of the agreed information to the new annotation. I can, perhaps imagine some hypothetical syntax:

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

which would seem to achieve what you want.

Maybe raise a promotion request: https://bugs.eclipse.org/bugs/enter_bug.cgi?product=AspectJ

Sorry, I have no news.

amuses Andy

I created a ticket for this problem if someone wants to follow the progress: https://bugs.eclipse.org/bugs/show_bug.cgi?id=345515

+1
source

All Articles