org.brains2b.typewriter
Class ClassTemplate
java.lang.Object
org.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[])
The class written will order the imports alphabetically, orders the method
starting at public down to private, ordering methods alphabetically. Same for
the method
- Version:
- 0.32.2 [28-11-2006]
- Author:
- dennis@brains2b.nl
- See Also:
createInnerClass(String, String, String[])
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).
|
void |
addInterface(java.lang.String className)
Add a interface to this class by supplying its full class name |
CommentTemplate |
createComment()
create a comment template for this class file |
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(int modifier,
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)
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)
|
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 |
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 |
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 |
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 |
java.io.File |
getCompleteFile()
get the filename of the completed file.
|
java.lang.String |
getName()
get the name of this class |
java.lang.String |
getPackage()
get the package for this class |
Template |
getParent()
get the direct parent of this element.
This can be the class, method, try element or loop elements |
void |
setName(java.lang.String name)
set the name of the class |
void |
setPackage(java.lang.String p)
set the package name for this class
It is assumed that this is a valid package name |
void |
setParent(Template parent)
set the direct parent of this element
The parent can be the class, method, try element or loop elements |
void |
write(java.io.PrintWriter pw,
int indent)
write the template information to the PrintWriter at the given
indention.
Indention is multiplied by 4 to supply the spaces
for the indention. |
void |
writeClass(java.lang.String path)
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. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
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 classname
- String, the name of this classsuperclass
- 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 fileinterfaces
- 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 filenamesuperclass
- 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 fileinterfaces
- 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 filenamesuperclass
- 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
write
public void write(java.io.PrintWriter pw,
int indent)
- 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 toindent
- 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 classsuperclass
- 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 usedinterfaces
- 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 file
- 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 membervariableinitialValue
- 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(int modifier,
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 membervariableinitialValue
- Object, a representation of the initial value
* @param initialValue Object, the initial value for this member, supply a Literal
object for a String or char that should be used as a Literal value
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,
java.lang.Object initialValue,
boolean 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
addInterface
public void addInterface(java.lang.String className)
- Add a interface to this class by supplying its full class name
- Parameters:
className
- String, the class name of the interface to add- Since:
- TypeWriter 0.21
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()
- 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)
- 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
setName
public void setName(java.lang.String name)
- set the name of the class
- Parameters:
name
- String, The class name / filename for this Class- Since:
- TypeWriter 0.21