public class ClassTemplate extends java.lang.Object implements Template
call the writeClass(String) 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
createInnerClass(String, String, String[])| Modifier | Constructor and Description |
|---|---|
protected |
ClassTemplate(ClassTemplate parent,
int modifier,
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
|
| Modifier and Type | Method and Description |
|---|---|
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(int modifier,
java.lang.String name,
java.lang.String superclass,
java.lang.String[] interfaces)
create an inner class template in this ClassTemplate
|
ClassTemplate |
createInnerClass(java.lang.String name,
java.lang.String superclass,
java.lang.String[] interfaces)
create an inner class template in this ClassTemplate
|
JavaDocTemplate |
createJavadoc()
create a comment template for this class file
|
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)
Deprecated.
|
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.util.Collection<ContentTemplate> |
getMethods() |
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.
|
java.lang.String |
getSuperclass() |
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
|
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
|
protected ClassTemplate(ClassTemplate parent, int modifier, java.lang.String name, java.lang.String superclass, java.lang.String[] interfaces)
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 implementpublic ClassTemplate(java.lang.String name,
java.lang.String superclass,
java.lang.String[] interfaces)
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 interfacespublic ClassTemplate(java.lang.String packagename,
java.lang.String name,
java.lang.String superclass,
java.lang.String[] interfaces)
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 filepublic void write(java.io.PrintWriter pw,
int indent)
Indention is multiplied by 4 to supply the spaces
for the indention.
public java.io.File getCompleteFile()
public void writeClass(java.lang.String path)
throws java.io.IOException
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
path - String, the path to write the class file to, excluding directories
which are contained in the package nameIOException, - thrown if the file operation cannot complete
normallyjava.io.IOExceptionpublic ClassTemplate createInnerClass(java.lang.String name, java.lang.String superclass, java.lang.String[] interfaces)
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
interfacespublic ClassTemplate createInnerClass(int modifier, java.lang.String name, java.lang.String superclass, java.lang.String[] interfaces)
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
interfacespublic CommentTemplate createComment()
public JavaDocTemplate createJavadoc()
public MethodTemplate createConstructor(java.lang.String[] params)
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.public MethodTemplate createConstructor(java.lang.String[][] params)
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"}}public MethodTemplate createMethod(java.lang.String name, java.lang.String[] params)
If a method with the exact name and parameters already exist this method is returned instead of creating a new one
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.public MethodTemplate createMethod(java.lang.String name, java.lang.String returnType, java.lang.String[] params)
If a method with the exact name and parameters already exist this method is returned instead of creating a new one
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.public MethodTemplate createMethod(int modifiers, java.lang.String name, java.lang.String returnType, java.lang.String[] params)
If a method with the exact name and parameters already exist this method is returned instead of creating a new one
modifiers - int,one ore more access-modifiers from Modifier, i.e Modifier.PRIVATE
or Modifier.PUBLIC+Modifier.STATIC+Modifier.FINALname - 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.Modifierpublic MethodTemplate createMethod(int modifiers, java.lang.String name, java.lang.String returnType, java.lang.String[][] params)
If a method with the exact name and parameters already exist this method is returned instead of creating a new one
modifiers - int,one ore more access-modifiers from Modifier, i.e Modifier.PRIVATE
or Modifier.PUBLIC+Modifier.STATIC+Modifier.FINALname - 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"}}Modifierpublic MemberTemplate createMember(java.lang.String type, java.lang.String name, java.lang.Object initialValue)
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 LiteralLiteralpublic MemberTemplate createMember(int modifier, java.lang.String type, java.lang.String name, java.lang.Object initialValue)
type - String, type of the member variable, i.e. "String"name - String, the name of a membervariableinitialValue - 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)Literal@Deprecated public MemberTemplate createMember(java.lang.String type, java.lang.String name, java.lang.Object initialValue, boolean literal)
public MemberTemplate createMember(java.lang.String type, java.lang.String name)
type - String, type of the member variable, i.e. "String"name - String, the name of a membervariablepublic void addInterface(java.lang.String className)
className - String, the class name of the interface to addpublic void addImport(java.lang.String className)
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
addImport in interface TemplateclassName - String, the className to add to the importTemplate.addImport(java.lang.String)public Template getParent()
This can be the class, method, try element or loop elements
public void setParent(Template parent)
The parent can be the class, method, try element or loop elements
public java.lang.String getName()
public java.lang.String getPackage()
public void setPackage(java.lang.String p)
It is assumed that this is a valid package name
p - String, the package to setpublic void setName(java.lang.String name)
name - String, The class name / filename for this Classpublic java.lang.String getSuperclass()
public java.util.Collection<ContentTemplate> getMethods()