-
Sub-task
-
Resolution: Unresolved
-
Major
-
2.1.1.Final
-
None
Splitting FORGE-1667 so it's easier to implement step by step
See : NewAnnotationCommand and NewAnnotationCommandTest
This allows to add extra features to the constraint-new-annotation command.
Adding resource bundle message
At the moment, the command create a constraint annotation with no implementation, and a default error message. It would be good to add a resource bundle key for the message :
constraint-new-annotation --named URL
@Documented @Constraint(validatedBy = {}) @Target( { METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER }) @Retention(RUNTIME) @ReportAsSingleViolation public @interface URL { String message() default "{org.mycompany.myproject.constraints.URL.message}"; Class<?>[] groups() default { }; Class<? extends Payload>[] payload() default { }; @Target( { METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER }) @Retention(RUNTIME) @Documented public @interface List { URL[] value(); } }
Then, it creates a message entry in the ValidationMessages.properties file (the file is created under resources directory if it doesn't already exist)
org.mycompany.myproject.constraints.URL.message=invalid URL
We could have attributes to the command to change the target
constraint-new-annotation --named URL --targets METHOD FIELD
This will generate the following code only with @Target METHOD, FIELD
@Documented @Constraint(validatedBy = {}) @Target( { METHOD, FIELD }) @Retention(RUNTIME) @ReportAsSingleViolation public @interface URL { String message() default "{org.mycompany.myproject.constraints.URL.message}"; Class<?>[] groups() default { }; Class<? extends Payload>[] payload() default { }; @Target( { METHOD, FIELD }) @Retention(RUNTIME) @Documented public @interface List { URL[] value(); } }
Constraint with an implementation
The constraint-new-annotation can have a validatedBy and a type attribute, it will then create a constraint annotation, an error message, and a default implementation :
constraint-new-annotation --named URL --validatedBy URLValidator --type String
The constraint will now refer to the implementation class URLValidator.class :
@Documented @Constraint(validatedBy = URLValidator.class) @Target( { METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER }) @Retention(RUNTIME) @ReportAsSingleViolation public @interface URL { String message() default "{org.mycompany.myproject.constraints.URL.message}"; Class<?>[] groups() default { }; Class<? extends Payload>[] payload() default { }; @Target( { METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER }) @Retention(RUNTIME) @Documented public @interface List { URL[] value(); } }
And the default implementation will look like :
public class URLValidator implements ConstraintValidator<URL, String> { @Override public void initialize(URL url) { } @Override public boolean isValid(String value, ConstraintValidatorContext context) { return false; } }
- is related to
-
FORGE-1671 Add nested annotation to a constraint annotation
- Closed
- relates to
-
FORGE-1616 Command constraint-new-annotation to create a new constraint
- Closed