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

Antonio Goncalves (JIRA) issues at jboss.org
Sat Feb 7 12:46:48 EST 2015


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

Antonio Goncalves updated FORGE-1617:
-------------------------------------
    Description: 
Some constraints need to have fields (attributes). For example, the {{URL}} constraint needs three fields : a protocol, a host, and a port :

{code}
constraint-add-field --named protocol --type String --targetConstraint URL
constraint-add-field --named host --type String --targetConstraint URL
constraint-add-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}

  was:
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}



> Command 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
>              Labels: Starter
>             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-add-field --named protocol --type String --targetConstraint URL
> constraint-add-field --named host --type String --targetConstraint URL
> constraint-add-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 was sent by Atlassian JIRA
(v6.3.11#6341)


More information about the forge-issues mailing list