Multiple Annotations of the same type?

Z

zakmck73

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
 
J

Joshua Cranmer

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
{
...
}

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 said:
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 {
[ ... ]
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top