001    /*******************************************************************************
002     * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian
003     * Thomschke.
004     * 
005     * All Rights Reserved. This program and the accompanying materials
006     * are made available under the terms of the Eclipse Public License v1.0
007     * which accompanies this distribution, and is available at
008     * http://www.eclipse.org/legal/epl-v10.html
009     * 
010     * Contributors:
011     *     Sebastian Thomschke - initial implementation.
012     *******************************************************************************/
013    package net.sf.oval.constraint;
014    
015    import java.lang.annotation.Documented;
016    import java.lang.annotation.ElementType;
017    import java.lang.annotation.Retention;
018    import java.lang.annotation.RetentionPolicy;
019    import java.lang.annotation.Target;
020    
021    import net.sf.oval.ConstraintTarget;
022    import net.sf.oval.ConstraintViolation;
023    import net.sf.oval.configuration.annotation.Constraint;
024    import net.sf.oval.configuration.annotation.Constraints;
025    
026    /**
027     * Check the value by a method of the same class that takes the value as argument and returns true if valid
028     * and false if invalid.
029     * 
030     * @author Sebastian Thomschke
031     */
032    @Documented
033    @Retention(RetentionPolicy.RUNTIME)
034    @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD, ElementType.TYPE})
035    @Constraint(checkWith = ValidateWithMethodCheck.class)
036    public @interface ValidateWithMethod
037    {
038            @Documented
039            @Retention(RetentionPolicy.RUNTIME)
040            @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD, ElementType.TYPE})
041            @Constraints
042            public @interface List
043            {
044                    /**
045                     * The ValidateWithMethod constraints.
046                     */
047                    ValidateWithMethod[] value();
048    
049                    /**
050                     * Formula returning <code>true</code> if this constraint shall be evaluated and
051                     * <code>false</code> if it shall be ignored for the current validation.
052                     * <p>
053                     * <b>Important:</b> The formula must be prefixed with the name of the scripting language that is used.
054                     * E.g. <code>groovy:_this.amount > 10</code>
055                     * <p>
056                     * Available context variables are:<br>
057                     * <b>_this</b> -&gt; the validated bean<br>
058                     * <b>_value</b> -&gt; the value to validate (e.g. the field value, parameter value, method return value,
059                     *    or the validated bean for object level constraints)
060                     */
061                    String when() default "";
062            }
063    
064            /**
065             * <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.
066             * 
067             * <p><b>Default:</b> ConstraintTarget.CONTAINER
068             * 
069             * <p><b>Note:</b> This setting is ignored for object types other than array, map and collection.
070             */
071            ConstraintTarget[] appliesTo() default ConstraintTarget.CONTAINER;
072    
073            /**
074             * error code passed to the ConstraintViolation object
075             */
076            String errorCode() default "net.sf.oval.constraint.ValidateWithMethod";
077    
078            /**
079             * this constraint will be ignored if the value to check is null
080             */
081            boolean ignoreIfNull() default true;
082    
083            /**
084             * message to be used for the ContraintsViolatedException
085             * 
086             * @see ConstraintViolation
087             */
088            String message() default "net.sf.oval.constraint.ValidateWithMethod.violated";
089    
090            /**
091             * name a the single parameter method to use for validation
092             */
093            String methodName();
094    
095            /**
096             * type of the method parameter
097             */
098            Class< ? > parameterType();
099    
100            /**
101             * The associated constraint profiles.
102             */
103            String[] profiles() default {};
104    
105            /**
106             * severity passed to the ConstraintViolation object
107             */
108            int severity() default 0;
109    
110            /**
111             * An expression to specify where in the object graph relative from this object the expression
112             * should be applied.
113             * <p>
114             * Examples:
115             * <li>"owner" would apply this constraint to the current object's property <code>owner</code>
116             * <li>"owner.id" would apply this constraint to the current object's <code>owner</code>'s property <code>id</code>
117             * <li>"jxpath:owner/id" would use the JXPath implementation to traverse the object graph to locate the object where this constraint should be applied.
118             */
119            String target() default "";
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    }