@Ensure annotation

back to index

Action

(Available since v1.1)
Design-By-Contract annotation that declares the postconditions of a method. The postconditions are expressed using any Java-compliant (JSR 223) scripting engine, and must be true after the body of the method is executed. If the postconditions fail, a ConstraintViolationException will be thrown.

The script can be specified either in the same location or can be placed in an external location. The script receives the following variables :

The script must return true if the data is valid, or false otherwise

Note that in order to execute design by contract annotations automatically, you must enable AspectJ in your project, as described here.

Javadoc Ref :  Javadoc reference@Ensure

 Input Types 1
Object
 Output Types
Same as input
   

1Automatic conversion will take place if the provided input does not match any of the input classes, as described in the conversions part of the manual.

Arguments

language

Language of the script. Default is "JavaScript".

script
Script code
scriptResource
Name of an external resource containing the script code
resourceEncoding
If the script is specified externally, this attribute specifies the character encoding used in the resource. Default is iso-8859-1
exceptionPolicy

Specifies what happens if the script throws an exception. Can be one of the following values (all of them belonging to the Javadoc referenceExceptionPolicy enumeration)

  • ASSUME_ VALID : The value is assumed to be valid
  • ASSUME_INVALID : The value is assumed to be invalid
  • PROPAGATE : The exception is propagated to the user code as a Javadoc reference HandlerException
scriptFunction

Specifies which function from the script to call. If not specified, the script will be executed using "immediate mode". Otherwise, the specified function will be invoked, and will be passed the following parameters (in order):

  • Validation environment
  • Property name
  • Object instance
  • Data value to be validated

The function must return a true/false result.

 

Example

@Ensure("__result > 0 ")
public int sum(@MinInclusive(3) int a, @MinInclusive(3) int b) {
   return a +b;
}