Design by Contract based on annotations. It allows easy usage of contracts without any need for extra JVM parameters, JVM agents, special classloaders or compilers. Instead it leverages dependency injection to capture method calls which are annotation with a contract. The project is highly flexible and can be extended to support new contract annotations and other contract clause definitions (see below).
Contracts can only be specified on methods. The following example shows how to do it:
public class ContractExample {
@Contract( preconditions = { @Clause(value = "damage > 0", message = "Reported damage must be positive!", exception = IllegalStateException.class), @Clause("damage <= 5000") }, postconditions = { @Clause("return >= 0"), @Clause(value = "return <= 2000", exception = NotEnoughMoneyException.class) }) public double calculateCover(double damage) { // .. some calculations }
}
The above example shows a method with two pre- and postconditions. The first precondition sets the optional message and exception fields, which are used in case the contract clause was violated. In case the exception message isn’t set, a default no-argument constructor is used, otherwise a constructor with a single String
parameter is searched. See the integration matrix below for a framework/language usage guide.
This library offers several implementations for a variety of other libraries/frameworks. The following configuration matrix shows what is currently supported. Anything missing? Create an issue!:
CDI | Guice | Spring | |
---|---|---|---|
JavaScript | ✓ | ✓ | ✓ |
JEXL | ✓ | ✓ | ✓ |
JUEL | ✓ | ✓ | ✓ |
MVEL | ✓ | ✓ | ✓ |
OGNL | ✓ | ✓ | ✓ |
SpEL | ✓ | ✓ | ✓ |
UEL | ✓ | ✓ | ✓ |
This program is free software. It comes without any warranty, to the extent permitted by applicable law. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2, as published by Sam Hocevar. See LICENSE or http://www.wtfpl.net/ for more details.
This projects uses Java, Maven and Git as the main development tools. To build the project yourself just download & install at least Maven 3.0 and call mvn install inside the project folder. Maven should then proceed to clean, build, package and install this project.
This project follows the semantic versioning guidelines.
Not what you are looking for? Try one of the alternatives:
- https://code.google.com/p/cofoja/
- http://jcontracts.sourceforge.net/
- http://www.polyglotprogramming.com/contract4j
- http://jcontractor.sourceforge.net/
- http://c4j.sourceforge.net/
- http://c4j-team.github.io/C4J/
- http://oval.sourceforge.net/
- http://springcontracts.sourceforge.net/
- http://modernjass.sourceforge.net/
- https://code.google.com/p/javadbc/
- http://chex4j.sourceforge.net/
- https://code.google.com/p/java-on-contracts/