views:

1987

answers:

7

I don't understand why there is no inheritance in Java annotations, just as Java classes. I think it would be very useful.

For example: I want to know if a given annotation is a validator. With inheritance, I could reflexively navigate through superclasses to know if this annotation extends a ValidatorAnnotation. Otherwise, how can I achieve this?

So, can anyone give me a reason why this design decision?

A: 

the same problem I have. No, you can't. I did 'disciplined' myself to write properties in annotations to respect some standards, so outside when you get annotation you can 'sniff' what kind od annotation it is by properties it has.

ante.sabo
A: 

One thing I could think of is the possibility to have multiple annotations. So you could add validator and a more specific annotation at the same place. But I could be mistaken :)

Janusz
+2  A: 

Check this: http://www.programminglearn.com/359/java-inherited-annotations-tutorial

EDIT: PDF article referred to in the comments

http://www.fusionsoft-online.com/download/static/tutorials/annotation%5Ftutorial%5Feng.pdf

Suraj Chandran
Please add your information into your post. If the site is down, we know the answer though.
furtelwart
What I meant was that there is a link to a pdf there. I just meant to be helpful. I can't copy the pdf here? Can I :)
Suraj Chandran
So now can you please remove the down vote? It's really annoying
Suraj Chandran
I upvoted you because you didn't deserve a downvote. Interesting link, but did not resolve my question :(
Sinuhe
thy lord, thanks:)
Suraj Chandran
you could give at least a short summary of the interesting parts of the pdf
Janusz
Wow, this is cool....Again someone down-voted me!!!I guess, I am ugly :)
Suraj Chandran
Anyways, I am posting the direct link of the pdf here : http://www.fusionsoft-online.com/download/static/tutorials/annotation_tutorial_eng.pdf Its just a 5 page one. You can read if you are interested. Also from next time on, I will put a little make-up and come, so you can't see my binary ugliness :)
Suraj Chandran
Could you provide a summary of the PDF? A URL isn't much of an answer.
Michael Donohue
@michael, If you are really that interested, I dont understand why you can't read a 100 words pdf. Guess what? No I won't!!
Suraj Chandran
@Suraj: Bare URLs seldom score well as answers.
Michael Myers
@michael @ mymers Sorry for my previous post. I just had a very bad day. A bug with no trace and logs. Hope you understand
Suraj Chandran
A: 

Never thought about that but... seems that you're right, there is no problem with annotations inheritance facility (at least I don't see the problem with it).

About your example with 'validator' annotation - you can exploit 'meta-annotation' approach then. I.e. you apply particular meta-annotation to the whole annotation interface.

denis.zhdanov
+7  A: 

Extensible annotations would effectively add the burden of specifying and maintaing another type system. And this would be a fairly unique type system, so you could not simply apply an OO type paradigm.

Think through all the issues when you introduce polymorphism and inheritance to an annotation (e.g. what happens when sub-annotation changes meta-annotation specs such as retention?)

And all this added complexity for what use-case?

You want to know if a given annotation belongs to a category?

Try this:

@Target(ElementType.ANNOTATION_TYPE)
public @interface Category {
 String category();
}

@Category(category="validator")
public @interface MyFooBarValidator {

}

As you can see, you can easily group and categorize annotations without undue pain using the provided facilities.

So, KISS is the reason for not introducing a meta-type type system to the Java language.

[p.s. edit]

I used the String simply for demonstration and in view of an open ended meta annotation. For your own given project, you obviously can use an enum of category types and specify multiple categories ("multiple inheritance") to a given annotation. Do note that the values are entirely bogus and for demonstration purposes only:

@Target(ElementType.ANNOTATION_TYPE)
public @interface Category {
 AnnotationCategory[] category();
}
public enum AnnotationCategory {
 GENERAL,
 SEMANTICS,
 VALIDATION,
 ETC
}

@Category(category={AnnotationCategory.GENERAL, AnnotationCategory.SEMANTICS})
public @interface FooBarAnnotation {

}


A: 

In a sense you already have it with Annotations - meta Annotations. If you annotate an annotation with meta information, that is in many ways equivalent to extending an additional interface. Annotations are interfaces, so polymorphism doesn't really come into play, and since they are static in nature, there can be no runtime dynamic dispatching.

In your validator example, you could just on the annotation get the annotated type and see if it has a validator meta-annotation.

The only use case I could see that inheritance would help is if you wanted to be able to get the annotation by super type, but that would add a whole bunch of complexity, because a given method or type may have two such annotations on it, meaning that an array would have to be returned instead of just a single object.

So I think the ultimate answer is that the use cases are esoteric and complicate more standard use cases making it not worth it.

Yishai
+6  A: 

About the reason why it wasn't designed that way you can find the answer in the JSR 175 Design FAQ, where it says:

Why don’t you support annotation subtyping (where one annotation type extends another)?

It complicates the annotation type system, and makes it much more difficult to write “Specific Tools”.

“Specific Tools” — Programs that query known annotation types of arbitrary external programs. Stub generators, for example, fall into this category. These programs will read annotated classes without loading them into the virtual machine, but will load annotation interfaces.

So, yes I guess, the reason is it just KISS. Anyway, it seems this issue (along with many others) are being looked into as part of JSR 308, and you can even find an alternative compiler with this functionality already developed by Mathias Ricken.

I hope that answers your question...

pedromarce
Well, maybe I'm stupid, but I think it's a pity can't extend annotations just for "keep it simple". At least, Java designers didn't think the same about class inheritance :P
Sinuhe