Skip to content

WritingContractsWithJavaScript

Sebastian Hoß edited this page Oct 5, 2013 · 2 revisions

See the JavaScript operators documentation

To reference the enclosing instance of the annotated business class, use the constant _this. To reference the result of the method invocation use the constant _return. The following class shows an example:


class JavaScriptInsuranceCompany {

    @Contract(
            preconditions = {
                    @Clause(value = "damage > 0", message = "Reported damage must be positive!",
                            exception = IllegalStateException.class),
                    @Clause(value = "damage <= _this.maximumReportableDamage", message = "We won't pay that!",
                            exception = IllegalStateException.class) },
            postconditions = {
                    @Clause(value = "_return >= 0", message = "We won't take any more!"),
                    @Clause(value = "_return <= _this.remainingMoney", message = "We can't pay that much!") })
    public double calculateCover(final double damage) {
        // some calculations
    }

    public double getMaximumReportableDamage() {
        // some value
    }

    public double getRemainingMoney() {
        // some value
    }

}