Sign in to follow this  

[java] Annotation Types: Good, Bad, or Indifferent?

This topic is 3840 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been reading a lot lately about Annotations and Annotation Types in Java. While I can see where they can come in handy, I also have a concern. It seems to me that Annotation Types are basically glorified structs. As a result, they don't really improve encapsulation and other object-oriented paradigms -- in fact, they may hurt them. Have any of you used Annotation Types in your programming? If so, what are your thoughts? Are my concerns legitimate? - Rob

Share this post


Link to post
Share on other sites
I've never seen them used as structs. The most common use I've seen is to "tag" classes and interfaces with metadata. This is sort of like the Serializable interface, but the annotations have the added advantage of being able to hold information. Ones like @Override and @Deprecated are good examples of "tagging" annotations (some people might call them "markers").
Another thing I've seen them used is in some web frameworks. I think the newest version of Hibernate uses them to define how DAOs are mapped to tables in a database. The advantage here is that you don't have tons of XML files defining your configuration. The disadvantage is that you have to recompile if you make changes and that also puts a crimp on trying to distribute custom variations of your code (which isn't as much as a problem with XML config files).

Share this post


Link to post
Share on other sites
Just like any other methodology, they can if used properly. They can also hinder development.

There's also development environments/toolkits/paradigms which are based solely on annotations.

There is no right or wrong here. In some cases, they can come in handy.

Java's implementation, while somewhat simplified from other such systems, allows for incredibly powerful meta and contract based programming.

Similar to templates which define an abstract term of concepts, these are another facility to achieve the same.

Unfortunately, there is no "look at this source to see what I mean" example beyond basic syntax. There are plenty of large projects which use meta-programming to define aspects of business logic, component and container contracts, data flows, or even provide much higher level AOP (aspect oriented programming).

Annotations are one of building blocks of such paradigms. They do unfortunately lack some distinction. Unlike OOP, which is clearly defined, the very abstract nature of meta-programming makes it much harder to define.

Just like with everything else - there are situations where these tools fit like glove fits the hand. And then there's abuse.

Middle-ground would be common example of @Test for unit tests. It can be easily done without annotations, but they add some syntactic sugar.

I'd go into some elaborate explanation of how meta-programming can be used, especially in Java with its reflection and class-loading mechanisms, but it would take too long.

If all you need is the answer to the question, then the answer is: depends. If you're interested into the topic, look for Java-related AOP, component/container design, IOC, meta-programming, and related topics. But it's a large, somewhat non-programmer topic (in the same way UML is unrelate to the actual source code). As always, a very powerful tool, but can be lethal for those who don't understand the big picture.

Share this post


Link to post
Share on other sites

This topic is 3840 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this