Dienstag, 9. Februar 2010

Design by Contract in Spring Roo

In Spring Roo they use a simple approach for Design by Contract (ok not real DBC only preconditions), looks nice for me here a code snippet from Spring Roo source code:

public MailProtocol(String protocolLabel, String protocol) {
 Assert.notNull(protocolLabel, "Protocol label required");
 Assert.notNull(protocol, "protocol required");
public AbstractInvocableMemberMetadata(String declaredByMetadataId, int modifier, List parameters, List parameterNames, List annotations, String body) {
 Assert.hasText(declaredByMetadataId, "Declared by metadata ID required");

I think the Spring Roo way is a good hint for Design by Contract / Preconditions as tests in Java, which is not support native in the Java language. The Java asserts can be disabled the spring roo Asserts throws RuntimeExceptions...

Other Java Framework for Design by Contract:
Languages, which support real Design by Contract:

    1 Kommentar:

    1. There is a Groovy extension that supports Design by Contract too - GContracts (http://github.com/andresteingress/gcontracts). It uses Groovy's AST transformations to inject class invariants, pre- and postconditions during compile time. It even has support for assertion inheritance.