This discussion is archived
0 Replies Latest reply: Jul 21, 2010 1:21 PM by 843793 RSS

Annotation Validations and Method Behavior.

843793 Newbie
Currently Being Moderated

Let me clearly explain the situation first,

I created an annotation (targeted at methods) whose only field of is a String, which identifies some validations (preconditions) that must be checked before the execution of the actual code of the method.

When you create a method “decorated” with this annotation it looks like this

public void myAnnotatedMethod(){
     //First lines
     //In this lines call another method to validate preconditions as described by the annotation

     //Other lines
     //In this lines perform methods operations if preconditions are fulfilled

Please note that the field “validate” on the annotation is not the validation itself, it just identifies what kind of validation should be done, then, in the first lines, I call a method which uses reflection to extract the annotation from the calling method (if you were to run it with the method from above it, would extract the annotation from the method “myAnnotatedMethod”) and then it performs the actual validation according to what is specified in the field “validate”.

This may look complicated, but it works, and gives me some flexibility regarding who and how should perform the validation of the preconditions, the only one “issue” here is that the first lines are always the same lines no matter what validation I'm doing, the consequence is that I put the same two lines at the beginning of every method which needs a precondition check .

My goal with this "weird" design was that the annotation alone could describe all the validations to be made and no other validation-related code would appear in the method. So my question is, could the annotation itself constrain the annotated method to perform the initial validation every time the annotated methods gets called?, so that I don't have write those first lines ever again.

Is this possible?, and if it is not, what can I do to avoid writing those two lines at the beginning of my methods?.

Thanks in advance