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 that the value is not a reference to the validated object itself.<br>
028     * This is e.g. useful to avoid circular references.<br>
029     * 
030     * @author Sebastian Thomschke
031     */
032    @Documented
033    @Retention(RetentionPolicy.RUNTIME)
034    @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
035    @Constraint(checkWith = NoSelfReferenceCheck.class)
036    public @interface NoSelfReference
037    {
038            @Documented
039            @Retention(RetentionPolicy.RUNTIME)
040            @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
041            @Constraints
042            public @interface List
043            {
044                    /**
045                     * The NoSelfReference constraints.
046                     */
047                    NoSelfReference[] 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.VALUES
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.VALUES;
072    
073            /**
074             * error code passed to the ConstraintViolation object
075             */
076            String errorCode() default "net.sf.oval.constraint.NoSelfReference";
077    
078            /**
079             * message to be used for the ContraintsViolatedException
080             * 
081             * @see ConstraintViolation
082             */
083            String message() default "net.sf.oval.constraint.NoSelfReference.violated";
084    
085            /**
086             * The associated constraint profiles.
087             */
088            String[] profiles() default {};
089    
090            /**
091             * severity passed to the ConstraintViolation object
092             */
093            int severity() default 0;
094    
095            /**
096             * An expression to specify where in the object graph relative from this object the expression
097             * should be applied.
098             * <p>
099             * Examples:
100             * <li>"owner" would apply this constraint to the current object's property <code>owner</code>
101             * <li>"owner.id" would apply this constraint to the current object's <code>owner</code>'s property <code>id</code>
102             * <li>"jxpath:owner/id" would use the JXPath implementation to traverse the object graph to locate the object where this constraint should be applied.
103             */
104            String target() default "";
105    
106            /**
107             * Formula returning <code>true</code> if this constraint shall be evaluated and
108             * <code>false</code> if it shall be ignored for the current validation.
109             * <p>
110             * <b>Important:</b> The formula must be prefixed with the name of the scripting language that is used.
111             * E.g. <code>groovy:_this.amount > 10</code>
112             * <p>
113             * Available context variables are:<br>
114             * <b>_this</b> -&gt; the validated bean<br>
115             * <b>_value</b> -&gt; the value to validate (e.g. the field value, parameter value, method return value,
116             *    or the validated bean for object level constraints)
117             */
118            String when() default "";
119    }