Skip to content

WritingContractsWithOGNL

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

See the OGNL language guide

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 OgnlInsuranceCompany {

    @Contract(
            preconditions = {
                    @Clause(value = "#damage > 0", message = "Reported damage must be positive!",
                            exception = IllegalStateException.class),
                    @Clause(value = "#damage <= 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 <= 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
    }

}