validate constructor arguments or method parameters with annotations, and let them throw an exceptio
- by marius
I am validating constructor and method arguments, as I want to the software, especially the model part of it, to fail fast.
As a result, constructor code often looks like this
public MyModelClass(String arg1, String arg2, OtherModelClass otherModelInstance) {
if(arg1 == null) {
throw new IllegalArgumentsException("arg1 must not be null");
}
// further validation of constraints...
// actual constructor code...
}
Is there a way to do that with an annotation driven approach? Something like:
public MyModelClass(@NotNull(raise=IllegalArgumentException.class, message="arg1 must not be null") String arg1, @NotNull(raise=IllegalArgumentException.class) String arg2, OtherModelClass otherModelInstance) {
// actual constructor code...
}
In my eyes this would make the actual code a lot more readable.
In understand that there are annotations in order to support IDE validation (like the existing @NotNull annotation).
Thank you very much for your help.