Class Builder

Class Builder©2002 is a tool for generating java source files from simple class descriptions.

Developed by Michael Werner after tools used by the Demeter Group, free for non-commercial use.

Download and installation:

Download the jar file and include it in the CLASSPATH. Download Now

Usage:

To use Class Builder, you first prepare a cb file, which contains essential information such as class names and attributes. Here is a sample cb file:

//* test for cb2 */

Identifier = //A class named Identifier will be built

<name> String. //Identifier will have a field named "name" of type String.
Graph = //A class named Graph will be built
<nodes>* Node //Graph will have a vector field named "nodes" to contain multiple Node objects
<edges>* Edge
<attributes># Attribute. //Graph will have a hashtable field named "attributes" to contain multiple Attribute objects

//It will be necessary to define Attribute.toString() to make the hashing work
Point =
<x> int
<y> int.

Node : Point = //Node will inherit from Point
<name> Identifier.

Edge =
<from> Node
<to> Node.

Attribute =
<property> Identifier
<value> Identifier.

Running Class Builder

Run from the command prompt. If Cb2.jar is on the classpath, simply type:

java Cb2 <input-file> <output-file>

Example: java Cb2 test.cb test.java

to read from test.cb and write to test.java

If Cb2.jar is not on the classpath, you need to call java with the -classpath option, as in:

java -cp c:\Java\Lib\Cb2.jar Cb2 test.cb test.java

where the file Cb2.jar is in folder C:\Java\Lib

There are also optional flags that determine what code will be generated. The current flags are:

Flags:

usage: java Cb2 <input-file> <output-file> [flags]

Example: java Cb2 test.cb test.java equals visitors nosetters

The Visitor Design Pattern

The Visitor Design Pattern is described in the famous "Gang-of-Four" book: Design Patterns - Elements of Reusable Object-Oriented Software.

It is particularly useful, when there is a tree of objects, such as the abstract syntax tree generated by a parser. Much work needs to be done at each node of the tree, such as consistency checking, optimization, code generation, etc. The work is accomplished by concrete visitor objects, which traverse the tree, doing tasks at each node, depending on the type of node it is. There are different visitors for each task, i.e. a CheckingVisitor, an OptimizingVisitor, a GenerateVisitor, etc. So the work done depends on two things: (1) the type of visitor; and (2) the type of the node being visited. This is called "double dispatch". Class Builder supports the Visitor Pattern by optionally generating code, which provides for a depth-first traversal of an object tree, and also generating an abstract Visitor class with before and after methods for each type of node. The programmer then creates concrete subclasses of Visitor, and launches them on thier traversal.

Class Builder, itself was programmed using the Visitor Pattern.

For more information about the Visitor Pattern see:

Antonio García The Visitor Design Pattern

Jeremy Blosser Java Tip 98: Reflect on the Visitor design pattern

Mark Grand Overview of Design Patterns

Related Tools

JavaCC and JJTree