• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Java Annotation help needed

 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am trying to learn Java Annotations.
This is an article from Sun website that explains using Annotations to add Validity Constraints to JavaBeans Properties.
The article explains the existing validation techniques available (viz. XML Schema, Property Descriptor etc.) and lists all their respective merits and demerits, eventually making the case for Annotations.
In short, what I expected Annotations to do in this case is to shorten the code and make it far more readable allowing the developer to focus on "what" rather than how.
That was until I tried the very 1st example that tries to constrain the length of a field to a maximum of 20:
Custom Annotation code (MaxLength):


and the test class code is :


However, when I run this code, it simply does not work.
Meaning when an argument of "dfsfjvdvjfdvjhbvdjhvbdjvbdvjbdfvhjbcdjkvndvjn" (say, string of length more than 20) is supplied at run time, the regular output is obtained even after adding the annotation.
Can anyone help me please?
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Annotations don't do anything by themselves. They are markup. Its up to you to find classes and methods marked with your annotation via the Reflection API and do the validation or whatever it is your annotation's purpose might be.
 
aditee sharma
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply.
If we have to write the code for validation, then how are Annotations an advantage over the PropertyDescriptor as explained in the article?
Having to write the code, they actually seem confusing to me, because while the validation code is inside one class, the Annotation itself is defined in another one.So, it seems all we achieved by Annotations is shifting from a xml file to a java file.
Having worked the xml way for years now, can you please give me a good reason to switch to Annotations?
Thanks in anticipation.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Having worked the xml way for years now, can you please give me a good reason to switch to Annotations?



No. But I can tell you that some people prefer it. Others don't. I like annotations when used sparingly. For example, one of my favorite web application frameworks is Stripes. I can do something like this:



So by going to this one action class I can see what the url pattern might be and I can see what validation is taking place for the User object. I know that user.username and user.password are both required. Now consider having this information in an XML file. Would it still work? Sure. But I don't care to jump from java to XML just to figure this information out. Now consider something like this:



Taken from here. That is utterly horrid and I'd never want to write something like that in Java code.

My point is annotations, for me, are good when used correctly, bad when used inappropriately. I know this might not really answer all your concerns but its kind of something you have to decide for yourself.
 
aditee sharma
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for taking so much effort to drive the point home.
So, can one generalize that while Annotations may be beneficial for simple purposes , they may not serve for more complex ones ?
 
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well it's up to each individual developer but I agree with Gregg 100%. Annotations should aid the understanding of the code flow, not make it unintelligible
 
There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic