• Post Reply Bookmark Topic Watch Topic
  • New Topic

Annotation used to validate String  RSS feed

 
Ted Scofield
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

first off, I will try to demonstrate what I would like to achieve, since I think it will be much better than describing it. Consider the following interface:



Next, consider the following line of code



What I would like to achieve is that @Validate would check the String following the annotation by using the isValid method from the interface InputValidator and throw a compile time error if the result would be false. Since I'm pretty new to annotations, I'm not even sure if this is possible. So, is it possible and if so, could you please point me in the direction I could find more about how to create such an annotation?

Thank you.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a link to the sun tutorial on annotations. It explains how to create your own.
http://download.oracle.com/javase/tutorial/java/javaOO/annotations.html

Hunter
 
Ted Scofield
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hunter,

I've seen that, along with some more examples I found on Google, but I still don't get the idea of how to creates something as described above...
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What specifically is giving you a problem? Writing the annotation itself? Processing the annotation at runtime?
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ted Scofield wrote:What I would like to achieve is that @Validate would check the String following the annotation by using the isValid method from the interface InputValidator

Which implementing class do you want to use? An interface has no body, so its isValid method cannot return anything.

and throw a compile time error if the result would be false.

Yeah... not going to happen. The compiler knows of a select group of system annotations (@Deprecated, @Override, @SuppressWarnings -- these are in java.lang for a reason), and you can't just create a new annotation and expect the compiler to know how to handle it. IntelliJ supports the @NotNull annotation but it uses its own compiler. So for you to add compiler support for your own annotation, you'll need to write your own compiler.

That said, you still can't validate a runtime value during compile time. The argument can come from any number of sources; your call to setSomething may include a value set several classes away.

Just validate during runtime and throw an IllegalArgumentException when the value is invalid.
 
Ted Scofield
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob, thanks for explaining it. Doing it the way it's been done a million times before, without unnecessary confusion (in this case, annotations) seems like a pretty cool idea.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parameter validations through annotations are perfectly reasonable, at run time (modulo a possible performance penalty, depending on how you implement it).
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't say you couldn't do it with annotations; if the retention policy of the annotation is RetentionPolicy.RUNTIME you can use reflection to get the annotations; Method.getParameterAnnotations seems to be the method to use. But as David said, it will likely have an impact on performance.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!