View Javadoc
1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian
3    * Thomschke.
4    * 
5    * All Rights Reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    * http://www.eclipse.org/legal/epl-v10.html
9    * 
10   * Contributors:
11   *     Sebastian Thomschke - initial implementation.
12   *******************************************************************************/
13  package net.sf.oval.constraint;
14  
15  import java.lang.annotation.Documented;
16  import java.lang.annotation.ElementType;
17  import java.lang.annotation.Retention;
18  import java.lang.annotation.RetentionPolicy;
19  import java.lang.annotation.Target;
20  
21  import net.sf.oval.ConstraintTarget;
22  import net.sf.oval.ConstraintViolation;
23  import net.sf.oval.configuration.annotation.Constraint;
24  import net.sf.oval.configuration.annotation.Constraints;
25  import net.sf.oval.constraint.CheckWithCheck.SimpleCheck;
26  
27  /**
28   * Check the value by a method of the same class that takes the value as argument and returns true if valid
29   * and false if invalid.
30   * 
31   * @author Sebastian Thomschke
32   */
33  @Documented
34  @Retention(RetentionPolicy.RUNTIME)
35  @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD, ElementType.TYPE})
36  @Constraint(checkWith = CheckWithCheck.class)
37  public @interface CheckWith
38  {
39  	@Documented
40  	@Retention(RetentionPolicy.RUNTIME)
41  	@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD, ElementType.TYPE})
42  	@Constraints
43  	public @interface List
44  	{
45  		/**
46  		 * The CheckWith constraints.
47  		 */
48  		CheckWith[] value();
49  
50  		/**
51  		 * Formula returning <code>true</code> if this constraint shall be evaluated and
52  		 * <code>false</code> if it shall be ignored for the current validation.
53  		 * <p>
54  		 * <b>Important:</b> The formula must be prefixed with the name of the scripting language that is used.
55  		 * E.g. <code>groovy:_this.amount > 10</code>
56  		 * <p>
57  		 * Available context variables are:<br>
58  		 * <b>_this</b> -&gt; the validated bean<br>
59  		 * <b>_value</b> -&gt; the value to validate (e.g. the field value, parameter value, method return value,
60  		 *    or the validated bean for object level constraints)
61  		 */
62  		String when() default "";
63  	}
64  
65  	/**
66  	 * <p>In case the constraint is declared for an array, collection or map this controls how the constraint is applied to it and it's child objects.
67  	 * 
68  	 * <p><b>Default:</b> ConstraintTarget.CONTAINER
69  	 * 
70  	 * <p><b>Note:</b> This setting is ignored for object types other than array, map and collection.
71  	 */
72  	ConstraintTarget[] appliesTo() default ConstraintTarget.CONTAINER;
73  
74  	/**
75  	 * error code passed to the ConstraintViolation object
76  	 */
77  	String errorCode() default "net.sf.oval.constraint.CheckWith";
78  
79  	/**
80  	 * this constraint will be ignored if the value to check is null
81  	 */
82  	boolean ignoreIfNull() default true;
83  
84  	/**
85  	 * message to be used for the ContraintsViolatedException
86  	 * 
87  	 * @see ConstraintViolation
88  	 */
89  	String message() default "net.sf.oval.constraint.CheckWith.violated";
90  
91  	/**
92  	 * The associated constraint profiles.
93  	 */
94  	String[] profiles() default {};
95  
96  	/**
97  	 * severity passed to the ConstraintViolation object
98  	 */
99  	int severity() default 0;
100 
101 	/**
102 	 * An expression to specify where in the object graph relative from this object the expression
103 	 * should be applied.
104 	 * <p>
105 	 * Examples:
106 	 * <li>"owner" would apply this constraint to the current object's property <code>owner</code>
107 	 * <li>"owner.id" would apply this constraint to the current object's <code>owner</code>'s property <code>id</code>
108 	 * <li>"jxpath:owner/id" would use the JXPath implementation to traverse the object graph to locate the object where this constraint should be applied.
109 	 */
110 	String target() default "";
111 
112 	/**
113 	 * Check class to use for validation. If this class is an inner class
114 	 * it needs be declared as a <b>static</b> class. Otherwise
115 	 * check instantiation will fail.
116 	 * 
117 	 * @see net.sf.oval.constraint.CheckWithCheck.SimpleCheck
118 	 */
119 	Class< ? extends SimpleCheck> value();
120 
121 	/**
122 	 * Formula returning <code>true</code> if this constraint shall be evaluated and
123 	 * <code>false</code> if it shall be ignored for the current validation.
124 	 * <p>
125 	 * <b>Important:</b> The formula must be prefixed with the name of the scripting language that is used.
126 	 * E.g. <code>groovy:_this.amount > 10</code>
127 	 * <p>
128 	 * Available context variables are:<br>
129 	 * <b>_this</b> -&gt; the validated bean<br>
130 	 * <b>_value</b> -&gt; the value to validate (e.g. the field value, parameter value, method return value,
131 	 *    or the validated bean for object level constraints)
132 	 */
133 	String when() default "";
134 }