|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--com.ashridgetech.jamda.code.ClassGenerator
Java code generator to generate a Java class. Each instance may only be used to generate one class.
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 |
public static final boolean READ_ONLY
public static final boolean READ_WRITE
public static final int ABSTRACT
Modifier.ABSTRACT
for convenience
public static final int FINAL
Modifier.FINAL
for convenience
public static final int PRIVATE
Modifier.PRIVATE
for convenience
public static final int PACKAGE
public static final int PROTECTED
Modifier.PROTECTED
for convenience
public static final int PUBLIC
Modifier.PUBLIC
for convenience
public static final int STATIC
Modifier.STATIC
for convenience
public static final int SYNCHRONIZED
Modifier.SYNCHRONIZED
for convenience
public static final int TRANSIENT
Modifier.TRANSIENT
for convenience
public static final int VOLATILE
Modifier.VOLATILE
for convenience
Constructor Detail |
public ClassGenerator()
ClassGenerator
for a Java class
. The instance will have isInterface()
false,
and the class modifiers (see setClassModifiers(int)
set to PUBLIC
.
public ClassGenerator(boolean isInterface)
ClassGenerator
The instance will have the class modifiers
(see setClassModifiers(int)
set to PUBLIC
.
isInterface
- true if this instance is to generate a Java interface
Method Detail |
public boolean isInterface()
ClassGenerator
is for
a Java interface
.
interface
ClassGenerator( boolean )
public ClassWriter getWriter()
ClassWriter
to which this generator writes its code.
Will return null before the start of the generate(java.io.Writer)
method.
ClassWriter
public void generate(java.io.Writer writer) throws java.io.IOException
ClassWriter
. This method may only be called once.
Calls doGenerate()
.
writer
- the destination for the generated code
java.lang.IllegalStateException
- if this method has already been called
java.io.IOException
public void checkReadyToGenerate()
ClassGenerator
has the minimum settings
needed to generate a class file.
java.lang.IllegalStateException
- if the instance is not ready to generateprotected ClassWriter makeClassWriter(java.io.Writer writer) throws java.io.IOException
ClassWriter
to which the generated code is written.
May be overridden by subclasses.
writer
- the underlying Writer
ClassWriter
java.io.IOException
protected void doGenerate() throws java.io.IOException
ClassWriter
.
May be overridden by subclasses.
java.io.IOException
public int getClassModifiers()
Modifier
.public void setClassModifiers(int classModifiers)
classModifiers
- the new classModifiers, using
the OR'ed values of the Java reflection modifier
constants, as in Modifier
.public java.lang.String getClassName()
public void setClassName(java.lang.String name)
name
- the class name - may not be nullpublic java.lang.String getPackageName()
public void setPackageName(java.lang.String name)
name
- the package name - may be null
to show the class does not belong to a packagepublic java.lang.String getFileName()
public java.lang.String getClassDoc()
public void setClassDoc(java.lang.String doc)
doc
- the documentationpublic java.util.List getImportNames()
public void addImport(java.lang.String importName)
importName
- the fully qualified
name of the import - not nullpublic void addImportClass(java.lang.Class clazz)
addImport(String)
.
clazz
- the imported class - not nullpublic void addImportPackage(java.lang.String packageName)
addImport(String)
.
packageName
- the fully qualified
name of the imported package - not nullpublic java.util.List getExtendsNames()
public void addExtends(java.lang.String extendsName)
extendsName
- the name of the class or interface, as it will appear
in the class declaration - not null or emptypublic void addExtends(java.lang.Class clazz)
clazz
- the extended class - not nullpublic void addExtends(java.lang.Class clazz, boolean addImport)
clazz
- the extended class - not nulladdImport
- if true, ensure there is an import for clazz
public java.util.List getImplementsNames()
public void addImplements(java.lang.String implementsName)
implementsName
- the name of the class or interface, as it will appear
in the class declaration - not null or emptypublic void addImplements(java.lang.Class clazz)
clazz
- the implemented class - not nullpublic void addImplements(java.lang.Class clazz, boolean addImport)
clazz
- the implemented class - not nulladdImport
- if true, ensure there is an import for clazz
public ClassGenerator.MethodGenerator addMethod(java.lang.String name)
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)
.
name
- the name of the method - must not be null, must not be equal to
the class name returned by getClassName()
.
MethodGenerator
public ClassGenerator.MethodGenerator addConstructor()
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)
.
MethodGenerator
public ClassGenerator.PropertyDetails addProperty(java.lang.String typeName, java.lang.String name, java.lang.String description, boolean isReadWrite, boolean isAbstract)
typeName
- the name of the type of the property - not nullname
- the name of the propertydescription
- 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
public ClassGenerator.PropertyDetails addProperty(java.lang.String typeName, java.lang.String name, java.lang.String description)
typeName
- the name of the type of the propertyname
- the name of the propertydescription
- the description of the property
addProperty(String,String,String,boolean,boolean)
addProperty(String,String,String,boolean,boolean)
public ClassGenerator.FieldGenerator addField(java.lang.String typeName, java.lang.String name, int modifiers)
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.
typeName
- the type of the fieldname
- the name of the method
FieldGenerator
java.lang.IllegalStateException
- if the parent ClassGenerator
is an interface (see isInterface()
)
and modifiers
modifiers is not PUBLIC
,
STATIC
, FINAL
.public ClassGenerator.FieldGenerator addField(java.lang.String typeName, java.lang.String name)
ClassGenerator.FieldGenerator
in order to create a private non-final instance field.
typeName
- the type of the fieldname
- the name of the method
FieldGenerator
protected void fileHeader()
protected void packageDecl()
protected void imports()
protected void classDecl()
protected void classDoc()
protected void fields()
protected void methods()
protected void constructors()
protected void endClass()
protected org.apache.log4j.Logger logger()
public java.lang.String toString()
toString
in class java.lang.Object
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |