org.brains2b.typewriter
Class ClassTemplate

java.lang.Object
  extended byorg.brains2b.typewriter.ClassTemplate
All Implemented Interfaces:
Template

public class ClassTemplate
extends java.lang.Object
implements Template

Template for creating a complete class file

call the to write a class to a specified path, including the correct indention packagename,imports,etc.

This ClassTemplate can also be used to create Inner classes into another ClassTemplate, through createInnerClass(String, String, String[])

TODO add comments about the order in which elements are written to the class file

Version:
0.30 [15-02-2004]
Author:
dennis@brains2b.nl
See Also:
createInnerClass(String, String, String[])

Field Summary
protected  ClassTemplate m_parent
           
 
Constructor Summary
protected ClassTemplate(ClassTemplate parent, java.lang.String name, java.lang.String superclass, java.lang.String[] interfaces)
          Constructor for creating a ClassTemplate for an Innerclass
  ClassTemplate(java.lang.String name, java.lang.String superclass, java.lang.String[] interfaces)
          Constructor for creating a ClassTemplate
  ClassTemplate(java.lang.String packagename, java.lang.String name, java.lang.String superclass, java.lang.String[] interfaces)
          Constructor for creating a ClassTemplate
 
Method Summary
 void addImport(java.lang.String className)
          add an import to this ClassTemplate if it has no parent (is not an Innerclass).
 CommentTemplate createComment()
          create a comment template for this class
 MethodTemplate createConstructor(java.lang.String[] params)
          create a constructor for this class template
 MethodTemplate createConstructor(java.lang.String[][] params)
          create a constructor for this class template
 ClassTemplate createInnerClass(java.lang.String name, java.lang.String superclass, java.lang.String[] interfaces)
          create an inner class template in this ClassTemplate
 MemberTemplate createMember(java.lang.String type, java.lang.String name)
          create a private member variable in this ClassTemplate without an initial value
 MemberTemplate createMember(java.lang.String type, java.lang.String name, java.lang.Object initialValue)
          create a private member variable in this ClassTemplate
 MemberTemplate createMember(java.lang.String type, java.lang.String name, java.lang.Object initialValue, boolean literal)
          create a private member variable in this ClassTemplate
 MethodTemplate createMethod(int modifiers, java.lang.String name, java.lang.String returnType, java.lang.String[] params)
          create a method with the given access, the given type of return value for the given name and with the parameters
 MethodTemplate createMethod(int modifiers, java.lang.String name, java.lang.String returnType, java.lang.String[][] params)
          create a method with the given access, the given type of return value for the given name and with the parameters
 MethodTemplate createMethod(java.lang.String name, java.lang.String[] params)
          create a method with public access and no return value for the given name and with the parameters
 MethodTemplate createMethod(java.lang.String name, java.lang.String returnType, java.lang.String[] params)
          create a method with public access and the given type of return value for the given name and with the parameters
 java.io.File getCompleteFile()
          get the filename of the completed file.
 java.lang.String getName()
          get the name of this class
 java.lang.String getObjectName(java.lang.Class defClass)
          Deprecated. use TypeWriterHelper.getClassName(Class)
 java.lang.String getObjectName(java.lang.String className)
          get the Class name without the package
 java.lang.String getPackage()
          get the package for this class
 Template getParent()
          get the direct parent of this element.
 java.lang.String makeString(java.lang.String value)
          Deprecated. use TemplateHelper.makeString(String)
 void setPackage(java.lang.String p)
          set the package name for this class
 void setParent(Template parent)
          set the direct parent of this element
 void write(java.io.PrintWriter pw, int indent)
          write the template information to the PrintWriter at the given indention.
 void writeClass(java.lang.String path)
          write a class file with filename equals name+".java" to a given path
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_parent

protected ClassTemplate m_parent
Constructor Detail

ClassTemplate

protected ClassTemplate(ClassTemplate parent,
                        java.lang.String name,
                        java.lang.String superclass,
                        java.lang.String[] interfaces)
Constructor for creating a ClassTemplate for an Innerclass

Parameters:
parent - ClassTemplate, the class which should contain this inner class, if set to null this will become a toplevel class
name - String, the name of this class
superclass - String, fully qualified class name for supper class, set to null if super class should be java.lang.Object. This is not explicitly written into the class file
interfaces - String[], an array containing fully qualified class names for the interfaces,set to null if this class does not implement any interfaces this method should implement

ClassTemplate

public ClassTemplate(java.lang.String name,
                     java.lang.String superclass,
                     java.lang.String[] interfaces)
Constructor for creating a ClassTemplate

Parameters:
name - String, the name of this class, will be used to make the filename
superclass - String, fully qualified class name for supper class, set to null if super class should be java.lang.Object. This is not explicitly written into the class file
interfaces - String[], an array containing fully qualified class names for the interfaces this method should implement, set to null if this class does not implement any interfaces

ClassTemplate

public ClassTemplate(java.lang.String packagename,
                     java.lang.String name,
                     java.lang.String superclass,
                     java.lang.String[] interfaces)
Constructor for creating a ClassTemplate

Parameters:
name - String, the name of this class, will be used to make the filename
superclass - String, fully qualified class name for supper class, set to null if super class should be java.lang.Object. This is not explicitly written into the class file
Method Detail

write

public void write(java.io.PrintWriter pw,
                  int indent)
Description copied from interface: Template
write the template information to the PrintWriter at the given indention.

Indention is multiplied by 4 to supply the spaces for the indention.

Specified by:
write in interface Template
Parameters:
pw - PrintWriter, an initialized PrintWriter to write this element of the Class to
indent - int, the indention for this element.

Value is multiplied by four to give the number of spaces used for indention


getCompleteFile

public java.io.File getCompleteFile()
get the filename of the completed file. This will only be non null if the writeClass(String) method has been called

Returns:
File, the File of the class which was just written
Since:
0.11

writeClass

public void writeClass(java.lang.String path)
                throws java.io.IOException
write a class file with filename equals name+".java" to a given path

If a package name is defined the appropriate directories are added to the path and if necessary created. The path supplied here should however exist

Parameters:
path - String, the path to write the class file to, excluding directories which are contained in the package name
Throws:
IOException, - thrown if the file operation cannot complete normally
java.io.IOException

createInnerClass

public ClassTemplate createInnerClass(java.lang.String name,
                                      java.lang.String superclass,
                                      java.lang.String[] interfaces)
create an inner class template in this ClassTemplate

Parameters:
name - String, name of this inner class
superclass - String, fully qualified class name to use as super class for this inner class, if set to null the default java.lang.Object will be used
interfaces - String[], an array of fully qualified class names for the interfaces this inner class should implement, set to null if this class does not implement any interfaces
Returns:
ClassTemplate, the template to use with this innerclass

createComment

public CommentTemplate createComment()
create a comment template for this class

Returns:
ContentTemplate, the template holding comments for this class

createConstructor

public MethodTemplate createConstructor(java.lang.String[] params)
create a constructor for this class template

Parameters:
params - String[], the types for the arguments to create a constructor with. The variable names for this constructor will be generated as arg0, arg1, etc.
Returns:
MethodTemplate, an instance of MethodTemplate where isConstructor is true

createConstructor

public MethodTemplate createConstructor(java.lang.String[][] params)
create a constructor for this class template

Parameters:
params - String[][], an array containing the types for the arguments to create a constructor with and the variable names used for the types, could be something like new String[] {{"int","String"},{"id","name"}}
Returns:
MethodTemplate, an instance of MethodTemplate where isConstructor is true

createMethod

public MethodTemplate createMethod(java.lang.String name,
                                   java.lang.String[] params)
create a method with public access and no return value for the given name and with the parameters

If a method with the exact name and parameters already exist this method is returned instead of creating a new one

Parameters:
name - String, the name of this method, i.e. "setName"
params - String[], the types for the arguments to pass to the method with. The variable names for this constructor will be generated as arg0, arg1, etc.
Returns:
MethodTemplate

createMethod

public MethodTemplate createMethod(java.lang.String name,
                                   java.lang.String returnType,
                                   java.lang.String[] params)
create a method with public access and the given type of return value for the given name and with the parameters

If a method with the exact name and parameters already exist this method is returned instead of creating a new one

Parameters:
name - String, the name of this method, i.e. "setName"
returnType - String, the type of return value, could be a class name or a primitive name, i.e. "int"
params - String[], the types for the arguments to pass to the method with. The variable names for this constructor will be generated as arg0, arg1, etc.
Returns:
MethodTemplate

createMethod

public MethodTemplate createMethod(int modifiers,
                                   java.lang.String name,
                                   java.lang.String returnType,
                                   java.lang.String[] params)
create a method with the given access, the given type of return value for the given name and with the parameters

If a method with the exact name and parameters already exist this method is returned instead of creating a new one

Parameters:
modifiers - int,one ore more access-modifiers from Modifier, i.e Modifier.PRIVATE or Modifier.PUBLIC+Modifier.STATIC+Modifier.FINAL
name - String, the name of this method, i.e. "setName"
returnType - String, the type of return value, could be a class name or a primitive name, i.e. "int"
params - String[], the types for the arguments to pass to the method with. The variable names for this constructor will be generated as arg0, arg1, etc.
Returns:
MethodTemplate
See Also:
Modifier

createMethod

public MethodTemplate createMethod(int modifiers,
                                   java.lang.String name,
                                   java.lang.String returnType,
                                   java.lang.String[][] params)
create a method with the given access, the given type of return value for the given name and with the parameters

If a method with the exact name and parameters already exist this method is returned instead of creating a new one

Parameters:
modifiers - int,one ore more access-modifiers from Modifier, i.e Modifier.PRIVATE or Modifier.PUBLIC+Modifier.STATIC+Modifier.FINAL
name - String, the name of this method, i.e. "setName"
returnType - String, the type of return value, could be a class name or a primitive name, i.e. "int"
params - String[][], an array containing the types for the arguments to create a constructor with and the variable names used for the types, could be something like new String[] {{"int","String"},{"id","name"}}
Returns:
MethodTemplate
See Also:
Modifier

createMember

public MemberTemplate createMember(java.lang.String type,
                                   java.lang.String name,
                                   java.lang.Object initialValue)
create a private member variable in this ClassTemplate

Parameters:
type - String, type of the member variable, i.e. "String"
name - String, the name of a membervariable
initialValue - Object, a representation of the initial value, either a String or a Literal
Returns:
MemberTemplate, can be ignored while all the work is already done
See Also:
Literal

createMember

public MemberTemplate createMember(java.lang.String type,
                                   java.lang.String name,
                                   java.lang.Object initialValue,
                                   boolean literal)
create a private member variable in this ClassTemplate

Parameters:
type - String, type of the member variable, i.e. "String"
name - String, the name of a membervariable
initialValue - Object, a representation of the initial value
literal - boolean, true if the value passed as initialValue should not be seen as a String but as a Literal value. same as using (String,String,Literal)
Returns:
MemberTemplate, can be ignored while all the work is already done
See Also:
Literal

createMember

public MemberTemplate createMember(java.lang.String type,
                                   java.lang.String name)
create a private member variable in this ClassTemplate without an initial value

Parameters:
type - String, type of the member variable, i.e. "String"
name - String, the name of a membervariable
Returns:
MemberTemplate, can be ignored while all the work is already done

makeString

public java.lang.String makeString(java.lang.String value)
Deprecated. use TemplateHelper.makeString(String)


getObjectName

public java.lang.String getObjectName(java.lang.Class defClass)
Deprecated. use TypeWriterHelper.getClassName(Class)


getObjectName

public java.lang.String getObjectName(java.lang.String className)
Deprecated. use TypeWriterHelper.getClassName(String)

Description copied from interface: Template
get the Class name without the package

Specified by:
getObjectName in interface Template
Parameters:
className - String, a classname with or without a package
Returns:
String, the classname without the package
See Also:
Template.getObjectName(java.lang.String)

addImport

public void addImport(java.lang.String className)
add an import to this ClassTemplate if it has no parent (is not an Innerclass).

imports are added if the className contains package information

The imports are backed by a TreeSet to sort the imports and prevent imports to be duplicated

Specified by:
addImport in interface Template
Parameters:
className - String, the className to add to the import
See Also:
Template.addImport(java.lang.String)

getParent

public Template getParent()
Description copied from interface: Template
get the direct parent of this element.

This can be the class, method, try element or loop elements

Specified by:
getParent in interface Template
Returns:
Template, the parent of this element

setParent

public void setParent(Template parent)
Description copied from interface: Template
set the direct parent of this element

The parent can be the class, method, try element or loop elements

Specified by:
setParent in interface Template
Parameters:
parent - Template, the parent of this element

getName

public java.lang.String getName()
get the name of this class

Returns:
String the name of this class

getPackage

public java.lang.String getPackage()
get the package for this class

Returns:
String, the package name

setPackage

public void setPackage(java.lang.String p)
set the package name for this class

It is assumed that this is a valid package name

Parameters:
p - String, the package to set