GContracts


Andre Steingress
  @sternegross/@gcontracts
LINZ, AUSTRIA
www.ejug.at
ABOUT
GCONTRACTS.
GCONTRACTS

DESIGN BY CONTRACT (tm) FOR GROOVY
 Groovy 1.7+ (was 1.6 loooong time ago)

BSD LICENSE


https://github.com/andresteingress/gcontracts
JUMP START.
LET‘S REVIEW THIS
       ACCOUNT CLASS.



Googled, found at javacoffeebreak.com
PREREQUISITES.
@Grab(
	

 group='org.gcontracts',
	

 module='gcontracts-core',
	

 version='[1.2.3,)'
)


                  no additional dependencies...
import org.gcontracts.annotations.*
3 ANNOTATIONS.
Commonalities
• special assertion types
 • specifying boolean expressions
• closure instances as annotation
  parameters


  @MyAnnotation({ param?.size() > 0 })
@Requires

• precondition

• a method‘s bouncer
@Ensures


• postcondition
• a method‘s mother
@Invariant

• class-invariant

• an object‘s god.
LET‘S REVIEW THIS
       ACCOUNT CLASS.



Googled, found at javacoffeebreak.com
INTERNALS.
@Ensures({ count() == old.count + 1 })


                          { count() == old.count + 1 }



               { result, old -> assert count() == old.count + 1 }
compile-time




                    class $_gc_closure1 extends Closure {
                        def doCall(...) {
                           // ...
                           assert count() == old.count + 1
                           // ...
                        }
                    }

                       @Requires($_gc_213123123123.class)
JVM Arguments: -ea -da

                      if ($GCONTRACTS_ENABLED) { ... }


                   org.gcontracts.ViolationTracker.init()



           $_gc_result = Putable$_gc_closure1.newInstance(this,
run-time




           this).call(element, key)


           if (org.gcontracts.ViolationTracker.violationsOccured())


               org.gcontracts.ViolationTracker.rethrowFirst()
Officially Supported since Groovy 1.8!
Works since Groovy 1.6

http://pniederw.wordpress.com/2010/03/04/3/
MORE FEATURES.
LABELING
INTERFACE CONTRACTS
INTERFACE CONTRACTS
INHERITANCE

✓INTERFACE CONTRACTS
✓ABSTRACT CLASSES
✓CONCRETE CLASSES
✓MIXED
INHERITANCE


✓ACROSS COMPILATION UNITS
MICRO CONTRACTS



       REUSABLE CONTRACT PART
MICRO CONTRACTS
Grails Integration



•   Container extends object construction time

•   @Service stereotype causes CI validation in
    @PostConstruct
GroovyDoc Integration
https://github.com/andresteingress/gcontracts/wiki
ABOUT
CONTRACTS.
MR.
„DESIGN BY CONTRACT“
WHY CONTRACTS?
SPOCK, JUNIT, et. al. ARE

„THE CHINAMAN IS
NOT THE ISSUE HERE,
     DUDE.“
External API Changes           New Programmers
         Changing Requirements
 Assumptions         Additional Libraries
      New Use-Cases                   More Clients
  New Knowledge    Code Conventions
External API Changes           New Programmers
         Changing Requirements
 Assumptions         Additional Libraries
      New Use-Cases                   More Clients
 New Knowledge     Code Conventions
WHAT DID YOU PROGRAM LAST WEEK?

CAN YOU RECALL YOUR THOUGHTS, MODELS?

WHY SHOULD ANYONE ELSE KNOW ABOUT THEM?
BUILD YOUR PLAYGROUND.
http://about.me/asteingress
Thank you!
"Design by Contract" is a trademark of Interactive Software Engineering.

GR8Conf 2011: GContracts