Arch4J 1.1

org.arch4j.generator
Class JavaSourceFile

java.lang.Object
  |
  +--org.arch4j.generator.JavaSourceFile
All Implemented Interfaces:
JavaKeywordConstants, JavaPrimitiveConstants

public class JavaSourceFile
extends Object
implements JavaKeywordConstants

This class represents the structure of a Java source file. It defines all the sections of a well-formed Java source file. The sections are built up sequentially by calling various methods. The methods can be called in any order.

Once all the desired sections are set, the Java source can be generated. The source can be generated as a String generateSource() or it can be written to a file generate().

Version:
$Revision: 1.5 $
Author:
Ross E. Greinke

Field Summary
protected  String classComment
          A comment describing the source file.
protected  List constructors
          A list of constructor methods, of type JavaConstructorDefinition.
protected  String copyrightComment
          The comment that can be used as a separate copyright comment.
protected  List fields
          A list of class or instance variables, of type JavaFieldDefinition.
static String FILE_EXTENSION
          The standard Java file extension.
protected  boolean importFullClassNames
          Use fully qualified class names in import statements? Default = false.
protected  List imports
          A list of imported classes, expressed as JavaType objects.
protected  List methods
          A list of methods, expressed as JavaMethodDefinition objects.
protected  JavaObjectDefinition objectDefinition
          The class or interface specification for the source file.
protected  String packageName
          The package this source file belongs to.
protected  String sourcePath
          The root source directory.
 
Fields inherited from interface org.arch4j.generator.JavaKeywordConstants
ABSTRACT, CATCH, CLASS, DEFAULT_ACCESS, EXTENDS, FALSE, FINAL, FINALLY, IMPLEMENTS, IMPORT, INTERFACE, NATIVE, NEW, NULL, PACKAGE, PRIVATE, PROTECTED, PUBLIC, RETURN, STATIC, STRICTFP, SUPER, SYNCHRONIZED, THIS, THROW, THROWS, TRANSIENT, TRUE, TRY, VOID, VOLATILE
 
Fields inherited from interface org.arch4j.generator.JavaPrimitiveConstants
BOOLEAN, BYTE, CHAR, DOUBLE, FLOAT, INT, LONG, SHORT
 
Constructor Summary
JavaSourceFile(String sourcePath, JavaObjectDefinition objectDef)
          Creates a new JavaSourceFile instance with the given root source directory and object definition.
 
Method Summary
 void addConstructor(JavaConstructorDefinition constructor)
          Add a constructor method definition to the source file.
 void addConstructors(List constructors)
          Add a list of constructor methods to the source file.
 void addField(JavaFieldDefinition field)
          Add a class or instance variable (field) to the source file.
 void addFields(List fields)
          Add a list of class or instance variables (fields) to the source file.
 void addImport(JavaType importType)
          Add an import directive for the source file.
 void addImport(String importName)
          Add an import directive for the source file.
 void addImports(List imports)
          Add a list of import directives for the source file.
 void addMethod(JavaMethodDefinition method)
          Add a method definition to the source file.
 void addMethods(List methods)
          Add a list of methods to the source file.
protected  void checkContents()
          Perform a set of tasks to prepare the source file for generation.
protected  void gatherImports()
          Gather additional imports from the various parts of the source file.
 void generate()
          Generate the Java source file.
protected  void generateClassComments(CodeBuffer buffer)
          Write the class comments to the given CodeBuffer.
protected  void generateClassDefinition(CodeBuffer buffer)
          Write the class or interface definition to the given CodeBuffer.
protected  void generateConstructors(CodeBuffer buffer)
          Write the constructor method definitions to the given CodeBuffer.
protected  void generateCopyrightComments(CodeBuffer buffer)
          Write the copyright comments to the given CodeBuffer.
protected  void generateEndOfFile(CodeBuffer buffer)
          Write the end of class string to the given CodeBuffer.
protected  void generateFields(CodeBuffer buffer)
          Write the class or instance variable field definitions to the given CodeBuffer.
protected  void generateFullImports(CodeBuffer buffer)
           
protected  void generateImports(CodeBuffer buffer)
          Write the import directives to the given CodeBuffer.
protected  void generateMethods(CodeBuffer buffer)
          Write the method definitions to the given CodeBuffer.
protected  void generatePackage(CodeBuffer buffer)
          Write the package statement to the given CodeBuffer.
protected  void generateSettersAndGetters(CodeBuffer buffer)
          Write the setter and getter method definitions to the given CodeBuffer.
 String generateSource()
          Generate the Java source representation of the source file as a multi-line String.
protected  void generateStarredImports(CodeBuffer buffer)
           
 void setClassComment(String classComment)
          Set the class comment for the source file.
 void setCopyrightComment(String copyrightComment)
          Set the copyright comment for the source file.
 void setImportFullClassNames(boolean value)
          Set whether fully qualified class names are used in import statements.
 void setObjectDefinition(JavaObjectDefinition objectDef)
          Set the class or interface definition for the class file.
 void setPackageName(String packageName)
          Set the package name for the source file.
 void setSourcePath(String sourcePath)
          Set the root source directory for the receiver.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FILE_EXTENSION

public static final String FILE_EXTENSION
The standard Java file extension.

See Also:
Constant Field Values

importFullClassNames

protected boolean importFullClassNames
Use fully qualified class names in import statements? Default = false.


sourcePath

protected String sourcePath
The root source directory.


copyrightComment

protected String copyrightComment
The comment that can be used as a separate copyright comment.


packageName

protected String packageName
The package this source file belongs to.


imports

protected List imports
A list of imported classes, expressed as JavaType objects.


classComment

protected String classComment
A comment describing the source file. Can be a multi-line string.


objectDefinition

protected JavaObjectDefinition objectDefinition
The class or interface specification for the source file.


fields

protected List fields
A list of class or instance variables, of type JavaFieldDefinition.


constructors

protected List constructors
A list of constructor methods, of type JavaConstructorDefinition.


methods

protected List methods
A list of methods, expressed as JavaMethodDefinition objects.

Constructor Detail

JavaSourceFile

public JavaSourceFile(String sourcePath,
                      JavaObjectDefinition objectDef)
Creates a new JavaSourceFile instance with the given root source directory and object definition.

Parameters:
sourcePath - The root source directory used for file generation.
objectDef - The class or interface specification.
Method Detail

setImportFullClassNames

public void setImportFullClassNames(boolean value)
Set whether fully qualified class names are used in import statements. If false, * will be used.

Parameters:
value - true for fully qualified names.

setSourcePath

public void setSourcePath(String sourcePath)
Set the root source directory for the receiver. This is the directory where the Java source file will be written.

Parameters:
sourcePath - The root source directory used for file generation.

setCopyrightComment

public void setCopyrightComment(String copyrightComment)
Set the copyright comment for the source file. It will appear above the package name. If the copyright comment contains more than one line, format it with a CodeBuffer. Create it with an initial indentation level of zero.

Parameters:
copyrightComment - The copyright comments for the source file.

setPackageName

public void setPackageName(String packageName)
Set the package name for the source file. It should be in standard Java dotted notation, e.g. org.arch4j./code>. If the package name is left unset, the package will be determined by looking for a package name in the object definition.

Parameters:
packageName - The package that the source file is part of.
See Also:
setObjectDefinition(JavaObjectDefinition)

addImport

public void addImport(String importName)
Add an import directive for the source file. The import name may include the package name.

Parameters:
importName - The import name.

addImport

public void addImport(JavaType importType)
Add an import directive for the source file. The import is expressed as a Java type.

Parameters:
importType - The import type.

addImports

public void addImports(List imports)
Add a list of import directives for the source file.

Parameters:
imports - The list of import names or types to add.

setClassComment

public void setClassComment(String classComment)
Set the class comment for the source file. If the comment contains more than one line, format it with a CodeBuffer. Create it with an initial indentation level of zero.

Parameters:
classComment - The class-level comments for the source file.

setObjectDefinition

public void setObjectDefinition(JavaObjectDefinition objectDef)
Set the class or interface definition for the class file. If the object definition includes a package specification and the package is not specifically set for the source file, the class or interface package specification will be used.

Parameters:
objectDef - The class or interface specification.
See Also:
setPackageName(String)

addField

public void addField(JavaFieldDefinition field)
Add a class or instance variable (field) to the source file.

Parameters:
field - The field definition.

addFields

public void addFields(List fields)
Add a list of class or instance variables (fields) to the source file.

Parameters:
fields - The list of field definitions to add.

addConstructor

public void addConstructor(JavaConstructorDefinition constructor)
Add a constructor method definition to the source file.

Parameters:
constructor - The constructor definition.

addConstructors

public void addConstructors(List constructors)
Add a list of constructor methods to the source file.

Parameters:
constructors - The list of constructor definitions to add.

addMethod

public void addMethod(JavaMethodDefinition method)
Add a method definition to the source file.

Parameters:
method - The method definition.

addMethods

public void addMethods(List methods)
Add a list of methods to the source file.

Parameters:
methods - The list of method definitions to add.

generate

public void generate()
              throws IOException
Generate the Java source file.

The Java source file will be created (or overwritten) as a .java file in one of two places. If no package is specified (the default package), the file will be created in the root source directory. If a package name is specified, the package name will be converted to a pathname and the file will be written to that directory.

Throws:
IOException - If a file error occurs.
See Also:
setSourcePath(String), setPackageName(String), generateSource()

generateSource

public String generateSource()
Generate the Java source representation of the source file as a multi-line String.

Returns:
The Java source file representation as a String.

checkContents

protected void checkContents()
Perform a set of tasks to prepare the source file for generation.


gatherImports

protected void gatherImports()
Gather additional imports from the various parts of the source file.


generateCopyrightComments

protected void generateCopyrightComments(CodeBuffer buffer)
Write the copyright comments to the given CodeBuffer.

Parameters:
buffer - The CodeBuffer with which to append the data.

generatePackage

protected void generatePackage(CodeBuffer buffer)
Write the package statement to the given CodeBuffer.

Parameters:
buffer - The CodeBuffer with which to append the data.

generateImports

protected void generateImports(CodeBuffer buffer)
Write the import directives to the given CodeBuffer.

Parameters:
buffer - The CodeBuffer with which to append the data.

generateFullImports

protected void generateFullImports(CodeBuffer buffer)

generateStarredImports

protected void generateStarredImports(CodeBuffer buffer)

generateClassComments

protected void generateClassComments(CodeBuffer buffer)
Write the class comments to the given CodeBuffer.

Parameters:
buffer - The CodeBuffer with which to append the data.

generateClassDefinition

protected void generateClassDefinition(CodeBuffer buffer)
Write the class or interface definition to the given CodeBuffer.

Parameters:
buffer - The CodeBuffer with which to append the data.

generateFields

protected void generateFields(CodeBuffer buffer)
Write the class or instance variable field definitions to the given CodeBuffer.

Parameters:
buffer - The CodeBuffer with which to append the data.

generateConstructors

protected void generateConstructors(CodeBuffer buffer)
Write the constructor method definitions to the given CodeBuffer.

Parameters:
buffer - The CodeBuffer with which to append the data.

generateSettersAndGetters

protected void generateSettersAndGetters(CodeBuffer buffer)
Write the setter and getter method definitions to the given CodeBuffer.

Parameters:
buffer - The CodeBuffer with which to append the data.

generateMethods

protected void generateMethods(CodeBuffer buffer)
Write the method definitions to the given CodeBuffer.

Parameters:
buffer - The CodeBuffer with which to append the data.

generateEndOfFile

protected void generateEndOfFile(CodeBuffer buffer)
Write the end of class string to the given CodeBuffer.

Parameters:
buffer - The CodeBuffer with which to append the data.

Arch4J 1.1

Copyright © 2000-2004 SpiderLogic, a service of Wipfli Ullrich Bertelson LLP.