[forge-issues] [JBoss JIRA] (FORGE-1667) Improve the constraint-new-annotation command
Antonio Goncalves (JIRA)
issues at jboss.org
Tue Mar 11 17:25:10 EDT 2014
[ https://issues.jboss.org/browse/FORGE-1667?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Antonio Goncalves updated FORGE-1667:
-------------------------------------
Description:
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.
h2. 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 :
{code}
constraint-new-annotation --named URL
{code}
{code}
@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();
}
}
{code}
Then, it creates a message entry in the {{ValidationMessages.properties}} file (the file is created under {{resources}} directory if it doesn't already exist)
{code}
org.mycompany.myproject.constraints.URL.message=invalid URL
{code}
We could have attributes to the command to change the target
{code}
constraint-new-annotation --named URL --targets METHOD FIELD
{code}
This will generate the following code only with @Target METHOD, FIELD
{code}
@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();
}
}
{code}
h2. 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* :
{code}
constraint-new-annotation --named URL --validatedBy URLValidator --type String
{code}
The constraint will now refer to the implementation class {{URLValidator.class}} :
{code}
@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();
}
}
{code}
And the default implementation will look like :
{code}
public class URLValidator implements ConstraintValidator<URL, String> {
@Override
public void initialize(URL url) {
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
return false;
}
}
{code}
was:
This allows the developer to create a constraint (with or without implementation) with a default error message (as a resource bundle).
h2. Constraint with no implementation
The following command will create a *constraint annotation* with no implementation, and a default *error message* :
{code}
constraint-new-annotation --named URL
{code}
This command first creates the constraint annotation (notice validatedBy = {})
{code}
@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();
}
}
{code}
Then, it creates a message entry in the {{ValidationMessages.properties}} file (the file is created under {{resources}} directory if it doesn't already exist)
{code}
org.mycompany.myproject.constraints.URL.message=invalid URL
{code}
We could have attributes to the command to change the target
{code}
constraint-new-annotation --named URL --targets METHOD FIELD
{code}
This will generate the following code only with @Target METHOD, FIELD
{code}
@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();
}
}
{code}
h2. 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* :
{code}
constraint-new-annotation --named URL --validatedBy URLValidator --type String
{code}
The constraint will now refer to the implementation class {{URLValidator.class}} :
{code}
@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();
}
}
{code}
And the default implementation will look like :
{code}
public class URLValidator implements ConstraintValidator<URL, String> {
@Override
public void initialize(URL url) {
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
return false;
}
}
{code}
> Improve the constraint-new-annotation command
> ----------------------------------------------
>
> Key: FORGE-1667
> URL: https://issues.jboss.org/browse/FORGE-1667
> Project: Forge
> Issue Type: Sub-task
> Components: Java EE
> Affects Versions: 2.1.1.Final
> Reporter: Antonio Goncalves
> Fix For: 2.x Future
>
>
> 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.
> h2. 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 :
> {code}
> constraint-new-annotation --named URL
> {code}
> {code}
> @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();
> }
> }
> {code}
> Then, it creates a message entry in the {{ValidationMessages.properties}} file (the file is created under {{resources}} directory if it doesn't already exist)
> {code}
> org.mycompany.myproject.constraints.URL.message=invalid URL
> {code}
> We could have attributes to the command to change the target
> {code}
> constraint-new-annotation --named URL --targets METHOD FIELD
> {code}
> This will generate the following code only with @Target METHOD, FIELD
> {code}
> @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();
> }
> }
> {code}
>
> h2. 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* :
> {code}
> constraint-new-annotation --named URL --validatedBy URLValidator --type String
> {code}
> The constraint will now refer to the implementation class {{URLValidator.class}} :
> {code}
> @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();
> }
> }
> {code}
> And the default implementation will look like :
> {code}
> public class URLValidator implements ConstraintValidator<URL, String> {
> @Override
> public void initialize(URL url) {
> }
> @Override
> public boolean isValid(String value, ConstraintValidatorContext context) {
> return false;
> }
> }
> {code}
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira
More information about the forge-issues
mailing list