Java Annotations



Email    print   
April 1, 2005 —  (Page 1 of 2)
Java 5’s “annotation” feature is a welcome addition to the language. Unfortunately, the implementation is worse than it needs to be, largely, I believe, because of a lack of openness in the Java Community Process.

Annotations replace dubious tagging idioms in your code. The classic example of this problem is the get/set idiom required originally by the JavaBeans spec and now endemic. Nobody ever intended for these getters and setters to be called by anything other than a UI-building tool. They were just a way of annotating the fact that an object exposed a property to an outside tool (which, in this case, uses the information to build a “property sheet”).

These particular getter/setter methods exist solely to provide information for the external tool. They should not be called from normal code. Another example of a flawed tagging mechanism is the Serializable interface, which tells the compiler (and JVM) to treat the annotated object specially. Serializable has no methods, so implementing it is nonsensical on its face.

An official annotation mechanism lets you leave notes for the compiler or runtime system without resorting to dubious idioms. For example, you could use private @property Color backgroundColor; to indicate that a layout tool should put a Background Color property into its property sheet. No getters and setters are required. By the same token, a persistence framework could tag a field with @persistent instead of using get/set. Other applications abound: You could precede a class definition with an @remote annotation to indicate that it would be used remotely via RMI. No need for a Remote interface or magic exceptions; the compiler provides the infrastructure for you. @precondition and @postcondition annotations can tell a compiler to automatically build regression tests for you. You get the idea.

Though annotations are wonderful, the mechanisms used to implement them in Java are not. For example, the syntax used to describe an annotation derives, inappropriately, from that of an interface definition. You might define an annotation that documents a known bug as follows: @Bug{id=1234, synopsis=“description of behavior”}




Pages 1 2 


Share this link: http://sdt.bz/28514
 
Most Read Latest News Blog Resources

Add comment


Name*
Email*  
Country     


  • Comment
Loading




close
NEXT ARTICLE
Look what 2011 washed in: The return of Java
Oracle's stewardship has led to real progress for the venerable coding language Read More...
 
 
 
 
News on Monday
more>>
SharePoint Tech Report
more>>


   

 
 

Download Current Issue
FEBRUARY 2012 PDF ISSUE

Need Back Issues?
DOWNLOAD HERE

Want to subscribe?


 
blogs tab
Are you at risk for burnout?
Burnout is a severe problem and it can strike at any time. Here's how to tell if you are nearing the edge.
02/09/2012 02:16 PM EST

Agility, mom, and apple pie
If we're to evaluate the state-of-the-art in software development, we should start with the values espoused in the Agile Manifesto.
02/07/2012 11:57 AM EST

RIM woos developers with free tablet
How do you get more apps ported to the BlackBerry PlayBook? By giving every developer a free tablet, of course!
02/04/2012 01:57 PM EST

GitHire: Use Headhunters to Find Your Perfect Programmer
Are you a hiring manager tired of scouring the job boards? Check out this new service that will find 5 people interested in your jobs.
02/03/2012 12:17 PM EST

Facebook claims hacker cred
Facebook's SEC S-1 filing form includes a short essay on the Hacker Way by Mark Zuckerberg himself.
02/02/2012 08:26 AM EST

Ryan Dahl steps down
Ryan Dahl, creator of Node.js, steps back from his position as gatekeeper for the project.
02/01/2012 04:58 PM EST

 
Events calendar tab
2/13/2012 to 2/16/2012
Santa Clara
TechWeb

2/26/2012 to 2/29/2012
San Francisco
BZ Media

2/27/2012 to 3/2/2012
San Francisco
RSA

3/4/2012 to 3/7/2012
Las Vegas
IBM Tivoli

3/5/2012 to 3/9/2012
San Francisco
TechWeb