org.hamcrest.generator
Class SugarGenerator

java.lang.Object
  extended by org.hamcrest.generator.SugarGenerator
All Implemented Interfaces:
java.io.Closeable, SugarConfiguration

public class SugarGenerator
extends java.lang.Object
implements java.io.Closeable, SugarConfiguration

API for syntactic sugar and wrapper code generation framework.

Generally, Matcher factory methods are defined all over the place, which makes it really hard to remember which methods need to be imported and doesn't allow you to easily browse a list of all of them.

This generates one uber matcher factory that delegates to all the respective classes.

Additionally, this allows the uber-classes to do things like wrap matchers in adapters (e.g. to make them EasyMock friendly) or even generate wrappers in other languages (such as JRuby or Jython).

You need to add at least one writer and at least one factory method for this to be any help.

Usage

 SugarGenerator sugarGenerator = new SugarGenerator();
 try {
   // Write out sugar as standard Hamcrest factories.
   sugarGenerator.addWriter(
     new HamcrestFactoryWriter("com.some.package", "MyMatcherClass", new FileWriter(...)));
   // Also write out sugar as EasyMock compatible factories.
   sugarGenerator.addWriter(
     new EasyMockFactoryWriter("com.some.package", "MyEasyMatcherClass", new FileWriter(...)));
   // Add a load of Matcher factories to the generated sugar. The factory methods
   // are read via reflection.
   sugarGenerator.addFactoryMethods(new ReflectiveFactoryReader(IsIn.class));
   sugarGenerator.addFactoryMethods(new ReflectiveFactoryReader(IsSame.class));
   sugarGenerator.addFactoryMethods(new ReflectiveFactoryReader(IsNot.class));
   // Generate everything!
   sugarGenerator.generate();
 } finally {
   // Clean up... close all streams.
   sugarGenerator.close();
 }

Author:
Joe Walnes
See Also:
FactoryWriter, HamcrestFactoryWriter, ReflectiveFactoryReader

Constructor Summary
SugarGenerator()
           
 
Method Summary
 void addFactoryMethod(FactoryMethod method)
          Add a FactoryMethod that will be generated in the sugar.
 void addFactoryMethods(java.lang.Iterable<FactoryMethod> methods)
          Convient way to add multiple FactoryMethods.
 void addWriter(FactoryWriter factoryWriter)
          Add a writer of factories.
 void close()
          Close all underlying streams.
 void generate()
          Generate all the factory methods using all the writers.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SugarGenerator

public SugarGenerator()
Method Detail

addWriter

public void addWriter(FactoryWriter factoryWriter)
Add a writer of factories.

Specified by:
addWriter in interface SugarConfiguration

addFactoryMethod

public void addFactoryMethod(FactoryMethod method)
Add a FactoryMethod that will be generated in the sugar.

Specified by:
addFactoryMethod in interface SugarConfiguration
See Also:
ReflectiveFactoryReader, FactoryMethod

addFactoryMethods

public void addFactoryMethods(java.lang.Iterable<FactoryMethod> methods)
Convient way to add multiple FactoryMethods.

Specified by:
addFactoryMethods in interface SugarConfiguration
See Also:
addFactoryMethod(FactoryMethod)

generate

public void generate()
              throws java.io.IOException
Generate all the factory methods using all the writers. This should always happen AFTER adding factory methods and writers.

Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Close all underlying streams. Calling this means you don't have to explicitly keep track of all the File streams and close them.

Specified by:
close in interface java.io.Closeable
Throws:
java.io.IOException