This week's book giveaway is in the Agile forum.
We're giving away four copies of Head First Agile and have Andrew Stellman & Jennifer Greene on-line!
See this thread for details.
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

When to use @annotation instead of marker interface declarations  RSS feed

 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I say a passing reference that said that whereas for a decade, we have used marker "interface" declarations, its better to use @annotations.

I've googled, and don't see any clear reason why one would be preferable to the other.

The declaration and use of a standard Java interface is clear and well understood.

I'm sure that I can figure out who to define and use my own annotations, but I'm not seeing any clear reason to do so, and thus don't see how its "better" in sufficient ways to justify the learning curve.

Is there a clear case to be made?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Learning curve? It's almost exactly the same as processing a marker interface, and the ability to pass parameters can be valuable depending on your use case.

If you don't need any parameters there's no compelling reason to switch, but the learning curve shouldn't be the reason.
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote: the learning curve shouldn't be the reason.

I disagree. I fully grok how to use marker interfaces. The tutorials and examples that fall readily to hand via Google don't even address how to use an @Annotation as a marker.

A marker interface has no parameters, I assume a marker @annotation has none either. But since nothing talks about them, I don't know.

I assume that I have to use reflection or something similar to see of the class has the desired @annotation. With an interface, a simple "instanceof" comparison is all you need.
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Farrell wrote:A marker interface has no parameters, I assume a marker @annotation has none either. But since nothing talks about them, I don't know.


It's pretty much the same. With a marker interface, you have to declare a blank interface. With "marker annotations", you have to declare a blank annotation class.

To mark a class with a marker interface, you have to implement the interface. To mark a variable, method, or class, with an annotation, you have to use a special annotation syntax.

Pat Farrell wrote:
I assume that I have to use reflection or something similar to see of the class has the desired @annotation. With an interface, a simple "instanceof" comparison is all you need.


This is where annotations kinda falls over. With a marker interface, you can use instanceof. With annotations, you have to get the annotation, using reflection, with the annotation Class that you are looking for. Like the others, its easy once you get use to it -- but can never get as easy as instanceof.

Henry
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Having said that, annotations is about marking, just like marker interfaces, but has many advantages....

1. It can mark variables, methods, and/or classes.

2. It can mark any class specifically, or via inheritance. A marker interface will mark all subclasses of the marked class.

3. You can add data to the mark. In other words, an annotation that is not blank has value -- in that you are marking with more than just type.

Henry
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Annotations also aren't subclassable, which is a PITA sometimes.

Pat Farrell wrote:
David Newton wrote: the learning curve shouldn't be the reason.

I disagree.

I found there to be essentially zero learning curve for annotations. I don't really see what the issue is, or what's complicated about them, or why making one without any arguments is difficult (there's an example right on the Sun^H^H^HOracle annotation tutorial. YMMV. Using isAnnotationPresent didn't seem difficult to me, but I be misunderstanding something.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once again, I must break a long post into smaller ones, to avoid JForum "pants down" errors. Here we go...
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding to the points already made:

Henry Wong wrote:2. It can mark any class specifically, or via inheritance. A marker interface will mark all subclasses of the marked class.

This can be either an advantage or a disadvantage. Annotations are not inherited by default - isAnnotationPresent() tells you if the annotation is present on that particular class, but not if it's present on a superclass or superinterface. So if you, as the implementor of whatever special functionality the annotation is intended to confer, want the annotation to behave as if it's inherited - you have to check isAnnotationPresent on not just this class, but every superclass, and every superinterface.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, if there's a possibility of picking up the same annotation from more than one of these sources, and if the annotation contains any additional data fields, then you may need to decide on how to resolve potential conflicting data from these multiple sources. For superclasses, this isgenerally easy - the closest superclass (most recent ancestor) wins. But if interfaces may also include annotations, it gets tricky - which interface is more important, and can they ever prevail over the data contained in a class annotation? (Simplest answer - any conflict involving an inherited interface should result in an immediate error, forcing the client programmer to re-declare the annotation in the current class, with whatever field values are desired. But your mileage may vary.)
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I would say that annotations are a bit harder to learn to use than marker interfaces are. But they are also more powerful and flexible.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, they have at least one more advantage not previously listed: with an annotation, you're much less likely to need to argue with other programmers about whether marker interfaces are a good idea. Your opponents will instead be divided into two camps: those who understand what you are doing with the annotation, and agree with it (having already bought into the annotations-are-better-than-marker-interfaces camp), and those who aren't sure what you're doing, don't really like it, but are afraid to speak up about it.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Done.)
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm... just a test, trying to repost the whole thing at once:

[deleted after the fact - but it worked, finally]
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, NOW it works.

Well, I tried it earlier many times, and it didn't start working until I broke it into smaller posts. Now, at the end, JForum accepts my longer post, just to mock me. Oh well.
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike, could you please edit your post next time instead of posting 8 posts within a timespan of 10 minutes?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not a length-of-post issue, it's a JavaRanch issue. Witness the folks that post 500 lines of code.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marker interfaces and annotations are used for the same purpose: to add metadata about some source code element (class, method, variable, etc.).

Annotations were added to Java 5 for just this purpose. You could argue that marker interfaces are a kind of hack - interfaces are not primarily intended to add metadata to your program. You're misusing interfaces to add metadata to the program. For that reason using annotations is cleaner.

Also you can use annotations not only on classes, but also on methods, variables etc.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An interface is supposed to guarantee external behavior (more or less); I've never been convinced that a marker interface is in direct contradiction to that, except that the actual *behavior* is performed by an external force. Part of that is because of the way Java was designed. For me the exercise of determining if it's abusive is rendered moot by it being the only practical way to do it :/
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Mike, could you please edit your post next time instead of posting 8 posts within a timespan of 10 minutes?


Well, I've removed the text of the last post, when it finally did work. But as for "instead of posting 8 posts within a timespan of 10 minutes" - no, that's not really a viable option for me. I tried posting it as a single post, many times. It didn't work until I broke it up. This has happened numerous times before, as well. I continued to attempt posting the full text several more times even after I'd started breaking it up - only the last attempt worked. I included that success to document what actually happened this time, even though that final success did not match the usual pattern I've observed.

I can't delete the old posts, just edit them. After all the trouble I've had, I'm not going to edit eight posts to leave behind empty shells. I'm happy to do it with one post, though.

David Newton wrote:It's not a length-of-post issue, it's a JavaRanch issue. Witness the folks that post 500 lines of code.

I agree it's a JavaRanch issue, but for me, it also seems to be a length issue. It used to be a frequently recurring problem; it went away for maybe half a year, and just recently it's resumed as a recurring problem. I have consistently observed that, when JavaRanch does act up, I get much, much, much more chance of posting successfully if break up the post into smaller posts. And I'm well aware that longer posts are possible, when the forum software is not acting up. But during periods of trouble, I can either not post at all, or I can break up the posts. I choose the latter.

From past experiments, I've determined that this behavior affects my posts from home, but not from work. It does not seem to depend on which browser I use, or which computer. I'm guessing it's something to do with either the ISP or the modem. (Hmmm... I haven't tried using a direct connection instead of wireless, when the problem occurs. Need to try that...) I have no idea why the problem occurs, or why it follows the particular symptoms I observe. Or why I seem to be the only person who sees this particular set of symptoms. But that doesn't change the fact that I have observed it, many times, and that I have seen a very strong correlation between the length of my posts and their chance of erroring out. It's not an absolute rule; there are exceptions on either side. But there's a strong correlation.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:for me, it also seems to be a length issue.

TMI
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:
Mike Simmons wrote:for me, it also seems to be a length issue.

TMI


LOL.

John.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!