Overview

FactoryGen automatically generates Factory classes, eliminating the maintenance that would normally be required. An Ant task is provided which can be hooked into a Maven or Ant build system, or even an Eclipse build.

Getting Started

The JavaDoc is available.

FactoryGen is designed to be run as an Ant task. The following task definition is provided as a starting point:

  <target name="run" description="Runs the task">
    <taskdef
        name="buildFactory"
        classname="ca.quine.factorygen.builder.BuildFactoryTask"
        classpath="lib/jcommons-sourcegen-0.1.jar;lib/jcommons-factorygen-builder-0.5.jar"
    />
    <buildFactory
        outputPackage="ca.quine.factorygen.test"
        outputClass="MarshallerFactory"
        outputDir="src"
        iRegistrantSubclass="ca.quine.factorygen.test.marshaller.IMarshaller"
        importPackage="ca.quine.factorygen.test.marshaller"
        dir="src/ca/quine/factorygen/test/marshaller"
        includes="*.java"
        excludes="IMarshaller.java"
    />
  </target>

Ensure you have jcommons-sourcegen and jcommons-factorygen-builder in the lib directory of the project where the factory is to be created, and that the versions match. Eclipse will underline the task definition in yellow if the ca.quine.factorygen.builder.BuildFactoryTask is not found in the classpath provided.

The outputPackage, outputClass and outputDir attributes tell the factory builder what factory class is being created and where. The iRegistrantSubclass is the fully qualified class name of the interface which extends IRegistrant which the generated factory will cast IRegsitrant objects into before passing them back to the caller.

The last four attributes shown above tell the factory builder which classes are to be added to the factory, and what package the generated factory should import. The includes and excludes attributes are a comma or space separated list of the files which are to be added to the generated factory.

The optional attribute superClass allows the user to "chain" generated factories together by adding or replacing additional IRegistrant instances to a previously generated factory. In this way it is possible to add further behaviour to a factory which is already in binary form or otherwise pre-packaged. The super class must be (or extend) ca.quine.factorygen.Registrar.

After the task is run, a Java source file will be generated that looks like the following.

package ca.quine.jcommons.marshaller;

import ca.quine.jcommons.marshaller.type.*;
import ca.quine.factorygen.Registrar;
import ca.quine.jcommons.marshaller.type.IMarshaller;

public class MarshallerFactory extends Registrar {
        private static MarshallerFactory instance;

        protected MarshallerFactory() {
                super();

                addRegistrant(new ArrayListMarshaller());
                addRegistrant(new ClassMarshaller());
                addRegistrant(new EntryMarshaller());
                addRegistrant(new MapMarshaller());
                addRegistrant(new SimpleMarshaller());
        }

        public static synchronized MarshallerFactory getInstance() {
                if (instance == null) {
                        instance = new MarshallerFactory();
                }

                return instance;
        }

        public IMarshaller getRegistrant(Object key) {
                return (IMarshaller) getRegistrantProtected(key);
        }

}