[forge-issues] [JBoss JIRA] (FORGE-1617) Command constraint-new-field to add a new field to an existing constraint

Antonio Goncalves (JIRA) issues at jboss.org
Sun Mar 2 11:38:47 EST 2014


     [ https://issues.jboss.org/browse/FORGE-1617?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Antonio Goncalves updated FORGE-1617:
-------------------------------------

    Component/s: Java EE
                     (was: Scaffold)

    
> Command constraint-new-field to add a new field to an existing constraint
> -------------------------------------------------------------------------
>
>                 Key: FORGE-1617
>                 URL: https://issues.jboss.org/browse/FORGE-1617
>             Project: Forge
>          Issue Type: Sub-task
>          Components: Java EE
>    Affects Versions: 2.1.1.Final
>            Reporter: Antonio Goncalves
>             Fix For: 2.x Future
>
>
> Some constraints need to have fields (attributes). For example, the {{URL}} constraint needs three fields : a protocol, a host, and a port :
> {code}
> constraint-new-field --named protocol --type String --targetConstraint URL
> constraint-new-field --named host --type String --targetConstraint URL
> constraint-new-field --named port --type String --targetConstraint URL
> {code}
> This will add the three attributes to the existing constraint {{URL}} : 
> {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 { };
> 	String protocol() default "";
> 	String host() default "";
> 	int port() default -1;
> 	@Target( { METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
> 	@Retention(RUNTIME)
> 	@Documented
> 	public @interface List {
> 		URL[] value();
> 	}
> }
> {code}
> But it will also add the three attributes to the implementation : attributes and {{initialize}} method : 
> {code}
> public class URLValidator implements ConstraintValidator<URL, String> {
>   private String protocol;
>   private String host;
>   private int port;
>   @Override
>   public void initialize(URL url) {
>     this.protocol = url.protocol();
>     this.host = url.host();
>     this.port = url.port();
>   }
>   @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