Package org.apache.openjpa.enhance
Class CodeGenerator
java.lang.Object
org.apache.openjpa.enhance.CodeGenerator
Generates Java class code from metadata.
- Since:
- 0.3.0
- Author:
- Abe White, Stephen Kim
-
Constructor Summary
ConstructorDescriptionCodeGenerator
(OpenJPAConfiguration conf, Class type) Constructor.CodeGenerator
(ClassMetaData meta) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
closeClassBrace
(CodeFormat code) Append the closing code-level brace to the code; this can be overridden to add code to the bottom of the class.void
Writes the generated code to the proper directory.protected List
Return class-level annotations.protected String
Return a code template for the given class, or null to use the standard system-generated Java code.getCode()
Return the generated code, or null ifgenerateCode()
has not been called.The directory to write source to.The code formatter for the generated Java code.protected String
getDeclaration
(FieldMetaData field) Return a code template for the declaration of the given field, or null to use the system-generated default Java code.protected List
getFieldAnnotations
(FieldMetaData field) Return field-level annotations.protected String
getFieldCode
(FieldMetaData field) Return a code template for the get/set methods of the given field, or null to use the system-generated default Java code.getFile()
Return Java file to write to.Returns the set of packages that needs to be imported for this code.protected String
getInitialValue
(FieldMetaData field) Return code for the initial value for the given field, or null to use the default generated by the system.Return metadata for the type being generated.getType()
Return the type being generated.protected CodeFormat
Return a copy of the internal code format.protected void
openClassBrace
(CodeFormat code) Append the opening code-level brace to the code; this can be overridden to add code to the top of the class.void
setCodeFormat
(CodeFormat format) Set the code formatter for the generated Java code.void
setDirectory
(File dir) The directory to write source to.protected boolean
Whether to use generic collections on one-to-many and many-to-many relations instead of untyped collections.protected boolean
Whether to use property-based access on generated code.void
Write the generated code to the proper file.void
Write the code to the specifiedWriter
.
-
Constructor Details
-
CodeGenerator
Constructor. Supply configuration and class to generate code for. -
CodeGenerator
Constructor. Supply configuration and metadata to generate code for.
-
-
Method Details
-
getCodeDirectory
The directory to write source to. Defaults to the current directory. If the given directory does not match the package of the metadata, the package structure will be created below the directory. -
setDirectory
The directory to write source to. Defaults to the current directory. If the given directory does not match the package of the metadata, the package structure will be created below the directory. -
getCodeFormat
The code formatter for the generated Java code. -
setCodeFormat
Set the code formatter for the generated Java code. -
getType
Return the type being generated. -
getMetaData
Return metadata for the type being generated. -
getCode
Return the generated code, or null ifgenerateCode()
has not been called. -
generateCode
public void generateCode()Writes the generated code to the proper directory. -
writeCode
Write the generated code to the proper file.- Throws:
IOException
-
writeCode
Write the code to the specifiedWriter
.- Throws:
IOException
-
getImportPackages
Returns the set of packages that needs to be imported for this code. -
openClassBrace
Append the opening code-level brace to the code; this can be overridden to add code to the top of the class. -
closeClassBrace
Append the closing code-level brace to the code; this can be overridden to add code to the bottom of the class. -
getFile
Return Java file to write to. -
newCodeFormat
Return a copy of the internal code format. -
getClassCode
Return a code template for the given class, or null to use the standard system-generated Java code. To facilitate template reuse, the following parameters can appear in the template; the proper values will be subtituted by the system:- ${packageDec}: The package declaration, in the form "package <package name >;", or empty string if no package.
- ${imports}: Imports for the packages used by the declared field types.
- ${className}: The name of the class, without package.
- ${extendsDec}: Extends declaration, in the form "extends <superclass>", or empty string if no superclass.
- ${constructor}: A constructor that takes in all primary key fields of the class, or empty string if the class uses datastore identity.
- ${fieldDecs}: Declarations of all the declared fields.
- ${fieldCode}: Get/set methods for all the declared fields.
-
getInitialValue
Return code for the initial value for the given field, or null to use the default generated by the system. Returns null by default. -
getDeclaration
Return a code template for the declaration of the given field, or null to use the system-generated default Java code. To facilitate template reuse, the following parameters can appear in your template; the proper values will be subtituted by the system:- ${fieldName}: The name of the field.
- ${capFieldName}: The capitalized field name.
- ${propertyName}: The field name without leading '_', if any.
- ${fieldType}: The field's type name.
- ${keyType}: Key type name for maps, null otherwise.
- ${elementType}: Element type name for collections, null otherwise.
- ${fieldValue}: The field's initial value, in the form " = <value>", or empty string if none.
-
getFieldCode
Return a code template for the get/set methods of the given field, or null to use the system-generated default Java code. To facilitate template reuse, the following parameters can appear in your template; the proper values will be subtituted by the system:- ${fieldName}: The name of the field.
- ${capFieldName}: The capitalized field name.
- ${propertyName}: The field name without leading '_', if any.
- ${fieldType}: The field's type name.
- ${keyType}: Key type name for maps, null otherwise.
- ${elementType}: Element type name for collections, null otherwise.
- ${fieldValue}: The field's initial value, in the form "= <value>", or empty string if none.
-
usePropertyBasedAccess
protected boolean usePropertyBasedAccess()Whether to use property-based access on generated code. Defaults to false (field-based). -
getClassAnnotations
Return class-level annotations. Returns null by default. -
getFieldAnnotations
Return field-level annotations. Returns null by default. -
useGenericCollections
protected boolean useGenericCollections()Whether to use generic collections on one-to-many and many-to-many relations instead of untyped collections. Override in descendants to change default behavior.
-