com.ashridgetech.jamda.code
Class ClassGenerator

java.lang.Object
  |
  +--com.ashridgetech.jamda.code.ClassGenerator
Direct Known Subclasses:
UmlTypeGenerator

public class ClassGenerator
extends java.lang.Object

Java code generator to generate a Java class. Each instance may only be used to generate one class.

Author:
Paul Boocock version 1.0

Nested Class Summary
 class ClassGenerator.FieldGenerator
          Generates a field variable in the code.
 class ClassGenerator.MethodGenerator
          Generates a method in the code.
 class ClassGenerator.PropertyDetails
          Holds references to the generator objects for the field, getter and setter generated for a property.
 
Field Summary
static int ABSTRACT
          Copy of Modifier.ABSTRACT for convenience
static int FINAL
          Copy of Modifier.FINAL for convenience
static int PACKAGE
          Convenience value for PACKAGE, which is zero
static int PRIVATE
          Copy of Modifier.PRIVATE for convenience
static int PROTECTED
          Copy of Modifier.PROTECTED for convenience
static int PUBLIC
          Copy of Modifier.PUBLIC for convenience
static boolean READ_ONLY
          Used with #addProperty(String,String,String,boolean,boolean) to indicate a read-only property
static boolean READ_WRITE
          Used with #addProperty(String,String,String,boolean,boolean) to indicate a read-write property
static int STATIC
          Copy of Modifier.STATIC for convenience
static int SYNCHRONIZED
          Copy of Modifier.SYNCHRONIZED for convenience
static int TRANSIENT
          Copy of Modifier.TRANSIENT for convenience
static int VOLATILE
          Copy of Modifier.VOLATILE for convenience
 
Constructor Summary
ClassGenerator()
          Construct a ClassGenerator for a Java class.
ClassGenerator(boolean isInterface)
          Construct a ClassGenerator The instance will have the class modifiers (see setClassModifiers(int) set to PUBLIC.
 
Method Summary
 ClassGenerator.MethodGenerator addConstructor()
          Obtain a ClassGenerator.MethodGenerator in order to create a constructor.
 void addExtends(java.lang.Class clazz)
          Add a class or interface extended by this one, adding an import statement for it.
 void addExtends(java.lang.Class clazz, boolean addImport)
          Add a class or interface extended by this one.
 void addExtends(java.lang.String extendsName)
          Add the name of a class or interface extended by this one.
 ClassGenerator.FieldGenerator addField(java.lang.String typeName, java.lang.String name)
          Obtain a ClassGenerator.FieldGenerator in order to create a private non-final instance field.
 ClassGenerator.FieldGenerator addField(java.lang.String typeName, java.lang.String name, int modifiers)
          Obtain a ClassGenerator.FieldGenerator in order to create a field.
 void addImplements(java.lang.Class clazz)
          Add a class or interface implemented by this one, and add an import for it.
 void addImplements(java.lang.Class clazz, boolean addImport)
          Add a class or interface implemented by this one.
 void addImplements(java.lang.String implementsName)
          Add the name of a class or interface implemented by this one.
 void addImport(java.lang.String importName)
          Add an imported name, unchanged, to the generated class.
 void addImportClass(java.lang.Class clazz)
          Add an imported class to the generated class.
 void addImportPackage(java.lang.String packageName)
          Add an imported package to the generated class.
 ClassGenerator.MethodGenerator addMethod(java.lang.String name)
          Obtain a ClassGenerator.MethodGenerator in order to create a method.
 ClassGenerator.PropertyDetails addProperty(java.lang.String typeName, java.lang.String name, java.lang.String description)
          Add the field variable and getter/setter methods to generate a standard read-write JavaBean property with a field variable.
 ClassGenerator.PropertyDetails addProperty(java.lang.String typeName, java.lang.String name, java.lang.String description, boolean isReadWrite, boolean isAbstract)
          Add the field variable and getter/setter methods to generate a standard JavaBean property.
 void checkReadyToGenerate()
          Check that this ClassGenerator has the minimum settings needed to generate a class file.
protected  void classDecl()
          Generate the class or interface declaration.
protected  void classDoc()
          Generate the class Javadoc.
protected  void constructors()
          Generate the class constructors.
protected  void doGenerate()
          Write the generated code to the ClassWriter.
protected  void endClass()
          End the class definition.
protected  void fields()
          Generate the class fields.
protected  void fileHeader()
          Generate the file header.
 void generate(java.io.Writer writer)
          Write the generated class code to the output ClassWriter.
 java.lang.String getClassDoc()
          Get the documentation for the class being generated.
 int getClassModifiers()
          Get the class modifiers
 java.lang.String getClassName()
          Get the name of the class being generated.
 java.util.List getExtendsNames()
          Get the names of the classes or interfaces extended by this one, as they will appear in the class declaration.
 java.lang.String getFileName()
          Get the name of the source file being generated.
 java.util.List getImplementsNames()
          Get the names of the interfaces implemented by this class, as they will appear in the class declaration.
 java.util.List getImportNames()
          Get the names of the imported packages or classes, as they will appear in the generated import statements.
 java.lang.String getPackageName()
          Get the name of the package of the class being generated.
 ClassWriter getWriter()
          Get the ClassWriter to which this generator writes its code.
protected  void imports()
          Generate the import declarations.
 boolean isInterface()
          Indicate whether this ClassGenerator is for a Java interface.
protected  org.apache.log4j.Logger logger()
           
protected  ClassWriter makeClassWriter(java.io.Writer writer)
          Create a ClassWriter to which the generated code is written.
protected  void methods()
          Generate the class methods.
protected  void packageDecl()
          Generate the package declaration.
 void setClassDoc(java.lang.String doc)
          Set the documentation for the class being generated..
 void setClassModifiers(int classModifiers)
          Set the classModifiers
 void setClassName(java.lang.String name)
          Set the name of the class being generated.
 void setPackageName(java.lang.String name)
          Set the name of the package to which the class being generated belongs.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

READ_ONLY

public static final boolean READ_ONLY
Used with #addProperty(String,String,String,boolean,boolean) to indicate a read-only property

See Also:
Constant Field Values

READ_WRITE

public static final boolean READ_WRITE
Used with #addProperty(String,String,String,boolean,boolean) to indicate a read-write property

See Also:
Constant Field Values

ABSTRACT

public static final int ABSTRACT
Copy of Modifier.ABSTRACT for convenience

See Also:
Constant Field Values

FINAL

public static final int FINAL
Copy of Modifier.FINAL for convenience

See Also:
Constant Field Values

PRIVATE

public static final int PRIVATE
Copy of Modifier.PRIVATE for convenience

See Also:
Constant Field Values

PACKAGE

public static final int PACKAGE
Convenience value for PACKAGE, which is zero

See Also:
Constant Field Values

PROTECTED

public static final int PROTECTED
Copy of Modifier.PROTECTED for convenience

See Also:
Constant Field Values

PUBLIC

public static final int PUBLIC
Copy of Modifier.PUBLIC for convenience

See Also:
Constant Field Values

STATIC

public static final int STATIC
Copy of Modifier.STATIC for convenience

See Also:
Constant Field Values

SYNCHRONIZED

public static final int SYNCHRONIZED
Copy of Modifier.SYNCHRONIZED for convenience

See Also:
Constant Field Values

TRANSIENT

public static final int TRANSIENT
Copy of Modifier.TRANSIENT for convenience

See Also:
Constant Field Values

VOLATILE

public static final int VOLATILE
Copy of Modifier.VOLATILE for convenience

See Also:
Constant Field Values
Constructor Detail

ClassGenerator

public ClassGenerator()
Construct a ClassGenerator for a Java class. The instance will have isInterface() false, and the class modifiers (see setClassModifiers(int) set to PUBLIC.


ClassGenerator

public ClassGenerator(boolean isInterface)
Construct a ClassGenerator The instance will have the class modifiers (see setClassModifiers(int) set to PUBLIC.

Parameters:
isInterface - true if this instance is to generate a Java interface
Method Detail

isInterface

public boolean isInterface()
Indicate whether this ClassGenerator is for a Java interface.

Returns:
true if it is generating an interface
See Also:
ClassGenerator( boolean )

getWriter

public ClassWriter getWriter()
Get the ClassWriter to which this generator writes its code. Will return null before the start of the generate(java.io.Writer) method.

Returns:
the ClassWriter

generate

public void generate(java.io.Writer writer)
              throws java.io.IOException
Write the generated class code to the output ClassWriter. This method may only be called once. Calls doGenerate().

Parameters:
writer - the destination for the generated code
Throws:
java.lang.IllegalStateException - if this method has already been called
java.io.IOException

checkReadyToGenerate

public void checkReadyToGenerate()
Check that this ClassGenerator has the minimum settings needed to generate a class file.

Throws:
java.lang.IllegalStateException - if the instance is not ready to generate

makeClassWriter

protected ClassWriter makeClassWriter(java.io.Writer writer)
                               throws java.io.IOException
Create a ClassWriter to which the generated code is written. May be overridden by subclasses.

Parameters:
writer - the underlying Writer
Returns:
the new ClassWriter
java.io.IOException

doGenerate

protected void doGenerate()
                   throws java.io.IOException
Write the generated code to the ClassWriter. May be overridden by subclasses.

java.io.IOException

getClassModifiers

public int getClassModifiers()
Get the class modifiers

Returns:
the OR'ed values of the Java reflection modifier constants, as in Modifier.

setClassModifiers

public void setClassModifiers(int classModifiers)
Set the classModifiers

Parameters:
classModifiers - the new classModifiers, using the OR'ed values of the Java reflection modifier constants, as in Modifier.

getClassName

public java.lang.String getClassName()
Get the name of the class being generated.

Returns:
the class name

setClassName

public void setClassName(java.lang.String name)
Set the name of the class being generated.

Parameters:
name - the class name - may not be null

getPackageName

public java.lang.String getPackageName()
Get the name of the package of the class being generated.

Returns:
the package name, or null if the class does not belong to a package

setPackageName

public void setPackageName(java.lang.String name)
Set the name of the package to which the class being generated belongs.

Parameters:
name - the package name - may be null to show the class does not belong to a package

getFileName

public java.lang.String getFileName()
Get the name of the source file being generated.

Returns:
the file name

getClassDoc

public java.lang.String getClassDoc()
Get the documentation for the class being generated.

Returns:
the documentation

setClassDoc

public void setClassDoc(java.lang.String doc)
Set the documentation for the class being generated..

Parameters:
doc - the documentation

getImportNames

public java.util.List getImportNames()
Get the names of the imported packages or classes, as they will appear in the generated import statements. names from the java packages appear first.

Returns:
a collection of String or other objects whose string values are the import names

addImport

public void addImport(java.lang.String importName)
Add an imported name, unchanged, to the generated class. Names may be added more than once, but they will only appear once in the final list.

Parameters:
importName - the fully qualified name of the import - not null

addImportClass

public void addImportClass(java.lang.Class clazz)
Add an imported class to the generated class. This implementation calls addImport(String).

Parameters:
clazz - the imported class - not null

addImportPackage

public void addImportPackage(java.lang.String packageName)
Add an imported package to the generated class. The ".*" is added to the end if it is not present. This implementation calls addImport(String).

Parameters:
packageName - the fully qualified name of the imported package - not null

getExtendsNames

public java.util.List getExtendsNames()
Get the names of the classes or interfaces extended by this one, as they will appear in the class declaration.

Returns:
a collection of String or other objects whose string values are the extends names

addExtends

public void addExtends(java.lang.String extendsName)
Add the name of a class or interface extended by this one.

Parameters:
extendsName - the name of the class or interface, as it will appear in the class declaration - not null or empty

addExtends

public void addExtends(java.lang.Class clazz)
Add a class or interface extended by this one, adding an import statement for it.

Parameters:
clazz - the extended class - not null

addExtends

public void addExtends(java.lang.Class clazz,
                       boolean addImport)
Add a class or interface extended by this one.

Parameters:
clazz - the extended class - not null
addImport - if true, ensure there is an import for clazz

getImplementsNames

public java.util.List getImplementsNames()
Get the names of the interfaces implemented by this class, as they will appear in the class declaration.

Returns:
a collection of String or other objects whose string values are the implements names

addImplements

public void addImplements(java.lang.String implementsName)
Add the name of a class or interface implemented by this one.

Parameters:
implementsName - the name of the class or interface, as it will appear in the class declaration - not null or empty

addImplements

public void addImplements(java.lang.Class clazz)
Add a class or interface implemented by this one, and add an import for it.

Parameters:
clazz - the implemented class - not null

addImplements

public void addImplements(java.lang.Class clazz,
                          boolean addImport)
Add a class or interface implemented by this one.

Parameters:
clazz - the implemented class - not null
addImport - if true, ensure there is an import for clazz

addMethod

public ClassGenerator.MethodGenerator addMethod(java.lang.String name)
Obtain a ClassGenerator.MethodGenerator in order to create a method. This ClassGenerator stores the returned generator and uses it in its methods() method. Further methods can be called on the MethodGenerator to set the characteristics of the generated method. This method may not be called until the class name has been set with setClassName(java.lang.String).

Parameters:
name - the name of the method - must not be null, must not be equal to the class name returned by getClassName().
Returns:
the MethodGenerator

addConstructor

public ClassGenerator.MethodGenerator addConstructor()
Obtain a ClassGenerator.MethodGenerator in order to create a constructor. This ClassGenerator stores the returned generator and uses it in its constructors() method. Further methods can be called on the MethodGenerator to set the characteristics of the generated constructor. This method may not be called until the class name has been set with setClassName(java.lang.String).

Returns:
the MethodGenerator

addProperty

public ClassGenerator.PropertyDetails addProperty(java.lang.String typeName,
                                                  java.lang.String name,
                                                  java.lang.String description,
                                                  boolean isReadWrite,
                                                  boolean isAbstract)
Add the field variable and getter/setter methods to generate a standard JavaBean property.

Parameters:
typeName - the name of the type of the property - not null
name - the name of the property
description - the description of the property . If this is null or empty, name is used as the description.
isReadWrite - true for getter and setter, false for getter only. The constants READ_WRITE and READ_ONLY should be used with this parameter.
isAbstract - true if the methods are to be abstract - no field is generated
Returns:
the details of the property generated, in case further adjustments to the field or methods are needed. This return object may normally be ignored

addProperty

public ClassGenerator.PropertyDetails addProperty(java.lang.String typeName,
                                                  java.lang.String name,
                                                  java.lang.String description)
Add the field variable and getter/setter methods to generate a standard read-write JavaBean property with a field variable.

Parameters:
typeName - the name of the type of the property
name - the name of the property
description - the description of the property
Returns:
see addProperty(String,String,String,boolean,boolean)
See Also:
addProperty(String,String,String,boolean,boolean)

addField

public ClassGenerator.FieldGenerator addField(java.lang.String typeName,
                                              java.lang.String name,
                                              int modifiers)
Obtain a ClassGenerator.FieldGenerator in order to create a field. This ClassGenerator stores the returned generator and uses it in its fields() method. Further methods can be called on the FieldGenerator to set the characteristics of the generated field.

Parameters:
typeName - the type of the field
name - the name of the method
Returns:
the FieldGenerator
Throws:
java.lang.IllegalStateException - if the parent ClassGenerator is an interface (see isInterface() ) and modifiersmodifiers is not PUBLIC, STATIC, FINAL.

addField

public ClassGenerator.FieldGenerator addField(java.lang.String typeName,
                                              java.lang.String name)
Obtain a ClassGenerator.FieldGenerator in order to create a private non-final instance field.

Parameters:
typeName - the type of the field
name - the name of the method
Returns:
the FieldGenerator

fileHeader

protected void fileHeader()
Generate the file header.


packageDecl

protected void packageDecl()
Generate the package declaration.


imports

protected void imports()
Generate the import declarations.


classDecl

protected void classDecl()
Generate the class or interface declaration.


classDoc

protected void classDoc()
Generate the class Javadoc. XXX - needs to split up multi-line comments, or long lines


fields

protected void fields()
Generate the class fields.


methods

protected void methods()
Generate the class methods.


constructors

protected void constructors()
Generate the class constructors.


endClass

protected void endClass()
End the class definition.


logger

protected org.apache.log4j.Logger logger()

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


Copyright © 2003 Ashridge Technologies Ltd. All Rights Reserved.