Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

Multiple Annotations of the same type?

Thread view: 
zakmck73@yahoo.it - 10 Aug 2007 11:36 GMT
Hi all,

I have a question about Java annotations. I find weird I cannot
annotate something multiple times using the same annotation type. For
instance I cannot do this:

@TODO ( type = "urgent", "Fix the bugs!" )
@TODO ( type = "low priority", "Check the comments" )
public class Foo
{
...
}

I wish that on the reflection side I had something like
getAnnotationsByType ( Class clazz ).
To make another example, if I use Hibernate annotations, I cannot
define multiple mappings from two different databases to the same
class.

Would someone comment on this strange restriction? Any chance things
will be improved in future? Does anyone know a workaround? I have seen
examples like this:http://java.sun.com/javase/6/docs/api/javax/xml/
bind/annotation/XmlElements.html
Any better idea?

Thanks in advance for any help
Joshua Cranmer - 10 Aug 2007 23:19 GMT
> Hi all,
>
[quoted text clipped - 8 lines]
> ...
> }

AFAIK, there is no technical reason why that cannot be true.

> I wish that on the reflection side I had something like
> getAnnotationsByType ( Class clazz ).

You have getAnnotation(Class<T> annotationType) [see below]

> Any chance things will be improved in future?

There's a chance; I don't think it's high on anyone's list of things to
do, though.

> Does anyone know a workaround?

When I ran into this problem, I created a new supra-annotation:

public @interface Option {
 [...]
}

public @interface Options {
  Option[] annotations();
}

And used at as such:

@Options({
   @Option( [...] ),
   @Option( [...] )
})
public class Foobar {
    [ ... ]
}

Signature

Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.