Download Sources

com.liguorien.flex.generator.handlers
Class AbstractGeneratorHandler<T extends FlexGeneratorHandler>

java.lang.Object
  extended by com.liguorien.flex.generator.handlers.AbstractGeneratorHandler<T>
All Implemented Interfaces:
FlexGeneratorHandler
Direct Known Subclasses:
Dom4jBuilderHandler, FlexModelBuilderHandler, FlexModelHandler

public abstract class AbstractGeneratorHandler<T extends FlexGeneratorHandler>
extends java.lang.Object
implements FlexGeneratorHandler

Basic implementation of FlexGeneratorHandler. The basic use of this class is to extends it to create a new type of generated code.

The behavior of this handler can easily be customized. It provides a List of MethodWriter, whichs contains a writer for each generated methods.

Version:
0.2
Author:
Nicolas Désy

Constructor Summary
AbstractGeneratorHandler()
          Creates a new instance of AbstractGeneratorHandler
 
Method Summary
 void addContextMethodWriter(MethodWriter<T> writer)
          Add a new MethodWriter for the current class only.
 void addImport(java.lang.String str)
          Add a import statement for the current class.
 void addMethodWriter(MethodWriter<T> writer)
          Add a MethodWriter to this handler.
 void addPropertyWriter(PropertyWriter<T> writer)
          Add a PropertyWriter to this handler.
protected  void appendClassPrefix(java.lang.StringBuilder buffer)
          Appends a prefix to name of the generated class.
protected  void appendClassSuffix(java.lang.StringBuilder buffer)
          Appends a suffix to name of the generated class.
protected abstract  void appendExtension(java.lang.StringBuilder buffer)
          Appends the extension of the generated file.
 void clearImports()
          Clear the import statements
 java.util.List<java.beans.PropertyDescriptor> getAttributes()
          Returns a List of PropertyDescriptor which will be XML attributes.
 java.lang.StringBuilder getClassName(java.lang.Class clazz)
          Get the name of the generated class.
abstract  ClassType getClassType()
          Get the ClassType for this handler.
abstract  ClassWriter getClassWriter()
          Get the ClassWriter of this handler.
 java.util.Set<MethodWriter<T>> getContextMethodWriters()
          Returns the method writers which are been added for the current class
 java.lang.Class<?> getCurrentClass()
          Return the current Entity class.
 java.util.List<java.beans.PropertyDescriptor> getElements()
          Returns a List of PropertyDescriptor which will be XML elements.
 FlexGenerator getGenerator()
          Getter for property generator.
 java.util.Set<java.lang.String> getImports()
          Returns a Set which contains the import statement for the current class
 java.util.List<MethodWriter<T>> getMethodWriters()
          Returns the method writers of this handler.
abstract  java.io.File getOutputDirectory()
          Return the output directory of this handler
 java.util.List<PropertyWriter<T>> getPropertyWriters()
          Returns the default property writers of this handler.
protected  java.lang.String getRelativeFilePath(java.lang.Class clazz)
          Returns a file path relative to the output directory.
 java.io.Writer getWriter()
          Getter for property writer.
protected  void handleClassBegin(java.lang.Class<?> clazz, ClassWriter cw, java.lang.String className, java.lang.String subClass, java.util.Set<java.lang.String> interfaces)
          Basic implementation of FlexGeneratorHandler.handleClassBegin(java.lang.Class) but with a different signature.
 void handleClassEnd(java.lang.Class<?> clazz)
          Basic implementation of FlexGeneratorHandler.handleClassEnd(java.lang.Class)
protected  void handleClassEnd(java.lang.Class<?> clazz, ClassWriter cw)
          This is where the class is actually generated.
 void handleProperty(java.beans.PropertyDescriptor property)
          Basic implementation of FlexGeneratorHandler.handleProperty(java.beans.PropertyDescriptor)
protected  void initWriter(java.lang.Class<?> clazz)
          Init writer used to output the generated classes.
 void resetImports()
          Clear and reset the import statements.
 void setGenerator(FlexGenerator generator)
          Setter for property generator.
 void setWriter(java.io.Writer writer)
          Setter for property writer.
 void writeClassName(java.io.Writer w, java.lang.Class clazz, java.lang.String classPrefix, java.lang.String classSuffix)
          Writes the name of the generated class.
protected  void writeDocumentation(java.io.Writer w)
          Write the document for the current generated class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.liguorien.flex.generator.handlers.FlexGeneratorHandler
handleClassBegin
 

Constructor Detail

AbstractGeneratorHandler

public AbstractGeneratorHandler()
Creates a new instance of AbstractGeneratorHandler

Method Detail

addImport

public void addImport(java.lang.String str)
Add a import statement for the current class.

Parameters:
str - The fully qualified name of the class

getImports

public java.util.Set<java.lang.String> getImports()

Returns a Set which contains the import statement for the current class

Returns:
a Set which contains the import statement for the current class

clearImports

public void clearImports()

Clear the import statements


resetImports

public void resetImports()

Clear and reset the import statements. Subclass can override to add default import. It will called between each class.


getClassWriter

public abstract ClassWriter getClassWriter()

Get the ClassWriter of this handler.

Returns:
The ClassWriter of this handler.
See Also:
ClassWriter

getClassType

public abstract ClassType getClassType()

Get the ClassType for this handler.

Specified by:
getClassType in interface FlexGeneratorHandler
Returns:
The ClassType of this handler.
See Also:
ClassType

handleClassBegin

protected void handleClassBegin(java.lang.Class<?> clazz,
                                ClassWriter cw,
                                java.lang.String className,
                                java.lang.String subClass,
                                java.util.Set<java.lang.String> interfaces)

Basic implementation of FlexGeneratorHandler.handleClassBegin(java.lang.Class) but with a different signature. Should be called by subclass in their own implementation of FlexGeneratorHandler.handleClassBegin(java.lang.Class).

Parameters:
clazz - The current class
cw - The current ClassWriter
className - The name of the generated class
subClass - The subclass of the generated (can be null)
interfaces - A Set of interface which are implemented by the generated class
See Also:
FlexGeneratorHandler.handleClassBegin(java.lang.Class)

handleProperty

public void handleProperty(java.beans.PropertyDescriptor property)

Basic implementation of FlexGeneratorHandler.handleProperty(java.beans.PropertyDescriptor)

This implementation check if the property will an attribute or an element in the generated XML. The property descripator can later be accessed by getAttributes() and getElements().

Specified by:
handleProperty in interface FlexGeneratorHandler
Parameters:
property - The property descriptor to handle

writeDocumentation

protected void writeDocumentation(java.io.Writer w)

Write the document for the current generated class.

The default behavior is to invoke FlexGenerator.writeDocumentation(java.io.Writer, java.lang.Class, int, java.lang.Object...).

Can be overrided to change the behavior.

Parameters:
w - The writer used to write the documentation.

handleClassEnd

public void handleClassEnd(java.lang.Class<?> clazz)

Basic implementation of FlexGeneratorHandler.handleClassEnd(java.lang.Class)

The default behavior is to invoke handleClassEnd(Class clazz, ClassWriter cw) using getClassWriter() as the ClassWriter.

Specified by:
handleClassEnd in interface FlexGeneratorHandler
Parameters:
clazz - The current class

handleClassEnd

protected void handleClassEnd(java.lang.Class<?> clazz,
                              ClassWriter cw)

This is where the class is actually generated.

Parameters:
clazz - The current class
cw - The ClassWriter used to generated the class.

addMethodWriter

public void addMethodWriter(MethodWriter<T> writer)

Add a MethodWriter to this handler.

Parameters:
writer - A MethodWriter instance.

addContextMethodWriter

public void addContextMethodWriter(MethodWriter<T> writer)

Add a new MethodWriter for the current class only.

Parameters:
writer - A MethodWriter instance.

addPropertyWriter

public void addPropertyWriter(PropertyWriter<T> writer)

Add a PropertyWriter to this handler.

Parameters:
writer - A PropertyWriter instance.

getMethodWriters

public java.util.List<MethodWriter<T>> getMethodWriters()

Returns the method writers of this handler.

Returns:
A List of PropertyWriter instances.

getContextMethodWriters

public java.util.Set<MethodWriter<T>> getContextMethodWriters()

Returns the method writers which are been added for the current class

Returns:
A Set of MethodWriter instances.

getPropertyWriters

public java.util.List<PropertyWriter<T>> getPropertyWriters()

Returns the default property writers of this handler.

Returns:
A List of PropertyWriter instances.

getAttributes

public java.util.List<java.beans.PropertyDescriptor> getAttributes()

Returns a List of PropertyDescriptor which will be XML attributes.

Returns:
A List of PropertyDescriptor.

getElements

public java.util.List<java.beans.PropertyDescriptor> getElements()

Returns a List of PropertyDescriptor which will be XML elements.

Returns:
A List of PropertyDescriptor.

getOutputDirectory

public abstract java.io.File getOutputDirectory()

Return the output directory of this handler

Returns:
A File object which represent the root of output directory

appendClassPrefix

protected void appendClassPrefix(java.lang.StringBuilder buffer)

Appends a prefix to name of the generated class. The default behavior is to append (if not null) value of FlexGenerator.getClassPrefix().

Parameters:
buffer - The buffer which the prefix must be appended.

appendClassSuffix

protected void appendClassSuffix(java.lang.StringBuilder buffer)

Appends a suffix to name of the generated class. The default behavior is to append (if not null) value of FlexGenerator.getClassSuffix().

Parameters:
buffer - The buffer which the suffix must be appended.

appendExtension

protected abstract void appendExtension(java.lang.StringBuilder buffer)

Appends the extension of the generated file.

Parameters:
buffer - The buffer which the extension must be appended.

getCurrentClass

public java.lang.Class<?> getCurrentClass()

Return the current Entity class.

Specified by:
getCurrentClass in interface FlexGeneratorHandler
Returns:
The current entity class.

writeClassName

public void writeClassName(java.io.Writer w,
                           java.lang.Class clazz,
                           java.lang.String classPrefix,
                           java.lang.String classSuffix)
                    throws java.io.IOException

Writes the name of the generated class. It's used by the MethodWriter who need a reference to the class.

Parameters:
w - The writer used to write the classname.
clazz - An entity class
classPrefix - The prefix of the classname (can be null)
classSuffix - The suffix of the classname (can be null)
Throws:
java.io.IOException - If an IO error occurs.

getClassName

public java.lang.StringBuilder getClassName(java.lang.Class clazz)

Get the name of the generated class. It's used by the MethodWriter who need a reference to the class.

Parameters:
clazz - An entity class
Returns:
A StringBuilder instance.

getRelativeFilePath

protected java.lang.String getRelativeFilePath(java.lang.Class clazz)

Returns a file path relative to the output directory.

Parameters:
clazz - An entity class
Returns:
A filepath relative to the output directory.

initWriter

protected void initWriter(java.lang.Class<?> clazz)

Init writer used to output the generated classes.

Parameters:
clazz - The current entity class.

getWriter

public java.io.Writer getWriter()
Getter for property writer.

Returns:
Value of property writer.

setWriter

public void setWriter(java.io.Writer writer)
Setter for property writer.

Parameters:
writer - New value of property writer.

getGenerator

public FlexGenerator getGenerator()
Getter for property generator.

Returns:
Value of property generator.

setGenerator

public void setGenerator(FlexGenerator generator)
Setter for property generator.

Specified by:
setGenerator in interface FlexGeneratorHandler
Parameters:
generator - New value of property generator.

FlexGenerator

Copyright 2006 Nicolas Désy. All rights reserved.