0

I understand, in Java we have parameters validation solution. I believe JAX-RS has various annotations both for validation and data extraction. My question is, if I want to implement my own parameter validation class for a standalone Java application, how would I make sure that a method is executed only when its parameters have been validated? I am using Reflection to spot parameters with @LowerCaseCheck and then performing validation on it, but not sure where to place this validation code.

public void print(@LowerCaseCheck String lowerCaseString) {
  ....
}

3 Answers 3

2

You need to change the byte code of the method to perform the check (or call a method which performs the check) The simplest way to do this might be to use an Aspect orientated library like AspectJ.

Sign up to request clarification or add additional context in comments.

1 Comment

@Abidi Unless you change the byte code, it is just a compiler time check or a comment at best. If you want to run something, you need byte code to do it.
0

Look at gag for an example of a library that does what you're looking for. It uses the asm bytecode manipulation library to insert validation checks at the start of annotated methods.

Comments

0

Cant'you use Bean Validation (JSR-303) to solve your problem ? the @Pattern(regexp) annotation seems to do just what you need.

public void print(@Pattern(regexp = "^[a-z]*$") String lowerCaseString) {
  ....
}

1 Comment

That's right but how does JSR-303 does it? Can this be coded without dealing with bytecode?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.