Skip to content

Commit

Permalink
HBX-2931 Pojo template cleanup.
Browse files Browse the repository at this point in the history
Adjust freemarker pojo templates for uniform whitespace usage.
ImportContextImpl - normalise line separators: remove carriage return char.
Add a template for field annotations.
Allow selection of field annotations over default property annotations by
  configuration of a toolclass property  "hibernatetool.annotateField.toolclass".
  The class allocated to this property is not important only the name "annotateField".
Tweak existing h2 database EntityNamingTest for testing of all the
  freemarker templates.
  • Loading branch information
dareni committed Jan 29, 2025
1 parent 1ef5828 commit 58c1af2
Show file tree
Hide file tree
Showing 16 changed files with 362 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ public String generateImports() {
// dont add automatically "imported" stuff
} else {
if(staticImports.contains(next)) {
buf.append("import static " + next + ";\r\n");
buf.append("import static " + next + ";\n");
} else {
buf.append("import " + next + ";\r\n");
buf.append("import " + next + ";\n");
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions orm/src/main/resources/pojo/Ejb3FieldGetAnnotation.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<#if ejb3>
<#if pojo.hasIdentifierProperty()>
<#if field.equals(clazz.identifierProperty)>
${pojo.generateAnnIdGenerator()}<#--has space prepended-->
</#if>
</#if>
<#if c2h.isOneToOne(field)>
${pojo.generateOneToOneAnnotation(field, md)}
<#elseif c2h.isManyToOne(field)>
${pojo.generateManyToOneAnnotation(field)}
<#--TODO support optional and targetEntity-->
${pojo.generateJoinColumnsAnnotation(field, md)}
<#elseif c2h.isCollection(field)>
${pojo.generateCollectionAnnotation(field, md)}
<#else>
<#if pojo.generateBasicAnnotation(field)?has_content >
<#if pojo.generateBasicAnnotation(field)?trim?length gt 0 >
${(pojo.generateBasicAnnotation(field))}<#--has space prepended-->
</#if>
</#if>
${pojo.generateAnnColumnAnnotation(field)}<#--has space prepended-->
</#if>
</#if>
35 changes: 18 additions & 17 deletions orm/src/main/resources/pojo/Ejb3PropertyGetAnnotation.ftl
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
<#if ejb3>
<#if pojo.hasIdentifierProperty()>
<#if property.equals(clazz.identifierProperty)>
${pojo.generateAnnIdGenerator()}
<#-- if this is the id property (getter)-->
<#-- explicitly set the column name for this property-->
</#if>
</#if>

<#if c2h.isOneToOne(property)>
${pojo.generateOneToOneAnnotation(property, md)}
<#elseif c2h.isManyToOne(property)>
${pojo.generateManyToOneAnnotation(property)}
<#--TODO support optional and targetEntity-->
<#if pojo.hasIdentifierProperty()>
<#if property.equals(clazz.identifierProperty)>
${pojo.generateAnnIdGenerator()}
<#-- if this is the id property (getter)-->
<#-- explicitly set the column name for this property-->
</#if>
</#if>
<#if c2h.isOneToOne(property)>
${pojo.generateOneToOneAnnotation(property, md)}
<#elseif c2h.isManyToOne(property)>
${pojo.generateManyToOneAnnotation(property)}
<#--TODO support optional and targetEntity-->
${pojo.generateJoinColumnsAnnotation(property, md)}
<#elseif c2h.isCollection(property)>
${pojo.generateCollectionAnnotation(property, md)}
<#else>
<#elseif c2h.isCollection(property)>
${pojo.generateCollectionAnnotation(property, md)}
<#else>
<#if pojo.generateBasicAnnotation(property) != " " >
${pojo.generateBasicAnnotation(property)}
</#if>
${pojo.generateAnnColumnAnnotation(property)}
</#if>
</#if>
</#if>
10 changes: 0 additions & 10 deletions orm/src/main/resources/pojo/Pojo.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,17 @@ ${pojo.getPackageDeclaration()}

<#assign classbody>
<#include "PojoTypeDeclaration.ftl"/> {

<#if !pojo.isInterface()>
<#include "PojoFields.ftl"/>

<#include "PojoConstructors.ftl"/>

<#include "PojoPropertyAccessors.ftl"/>

<#include "PojoToString.ftl"/>

<#include "PojoEqualsHashcode.ftl"/>

<#else>
<#include "PojoInterfacePropertyAccessors.ftl"/>

</#if>
<#include "PojoExtraClassCode.ftl"/>

}
</#assign>

${pojo.generateImports()}
${classbody}

33 changes: 16 additions & 17 deletions orm/src/main/resources/pojo/PojoConstructors.ftl
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@

<#-- /** default constructor */ -->
public ${pojo.getDeclarationName()}() {
}
<#if pojo.needsMinimalConstructor()><#-- /** minimal constructor */ -->

<#if pojo.needsMinimalConstructor()> <#-- /** minimal constructor */ -->
public ${pojo.getDeclarationName()}(${c2j.asParameterList(pojo.getPropertyClosureForMinimalConstructor(), jdk5, pojo)}) {
<#if pojo.isSubclass() && !pojo.getPropertyClosureForSuperclassMinimalConstructor().isEmpty()>
super(${c2j.asArgumentList(pojo.getPropertyClosureForSuperclassMinimalConstructor())});
</#if>
<#list pojo.getPropertiesForMinimalConstructor() as field>
<#if pojo.isSubclass() && !pojo.getPropertyClosureForSuperclassMinimalConstructor().isEmpty()>
super(${c2j.asArgumentList(pojo.getPropertyClosureForSuperclassMinimalConstructor())});
</#if>
<#list pojo.getPropertiesForMinimalConstructor() as field>
this.${c2j.keyWordCheck(field.name)} = ${c2j.keyWordCheck(field.name)};
</#list>
</#list>
}
</#if>
<#if pojo.needsFullConstructor()>
<#-- /** full constructor */ -->
public ${pojo.getDeclarationName()}(${c2j.asParameterList(pojo.getPropertyClosureForFullConstructor(), jdk5, pojo)}) {
<#if pojo.isSubclass() && !pojo.getPropertyClosureForSuperclassFullConstructor().isEmpty()>
super(${c2j.asArgumentList(pojo.getPropertyClosureForSuperclassFullConstructor())});
</#if>
<#list pojo.getPropertiesForFullConstructor() as field>
this.${c2j.keyWordCheck(field.name)} = ${c2j.keyWordCheck(field.name)};
</#list>
<#if pojo.needsFullConstructor()><#-- /** full constructor */ -->

public ${pojo.getDeclarationName()}(${c2j.asParameterList(pojo.getPropertyClosureForFullConstructor(), jdk5, pojo)}) {
<#if pojo.isSubclass() && !pojo.getPropertyClosureForSuperclassFullConstructor().isEmpty()>
super(${c2j.asArgumentList(pojo.getPropertyClosureForSuperclassFullConstructor())});
</#if>
<#list pojo.getPropertiesForFullConstructor() as field>
this.${c2j.keyWordCheck(field.name)} = ${c2j.keyWordCheck(field.name)};
</#list>
}
</#if>
</#if>
35 changes: 19 additions & 16 deletions orm/src/main/resources/pojo/PojoEqualsHashcode.ftl
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
<#if pojo.needsEqualsHashCode() && !clazz.superclass?exists>
<#assign classNameToCastTo><#if clazz.getProxyInterfaceName?exists>${clazz.getProxyInterfaceName()}<#else>${pojo.getDeclarationName()}</#if></#assign>
public boolean equals(Object other) {
if ( (this == other ) ) return true;
if ( (other == null ) ) return false;
if ( !(other instanceof ${classNameToCastTo}) ) return false;
${classNameToCastTo} castOther = ( ${classNameToCastTo} ) other;

return ${pojo.generateEquals("this", "castOther", jdk5)};
}

public int hashCode() {
int result = 17;

<#list pojo.getAllPropertiesIterator() as property> ${pojo.generateHashCode(property, "result", "this", jdk5)}
</#list> return result;
}
</#if>

public boolean equals(Object other) {
if ( (this == other ) ) return true;
if ( (other == null ) ) return false;
if ( !(other instanceof ${classNameToCastTo}) ) return false;
${classNameToCastTo} castOther = ( ${classNameToCastTo} ) other;
return ${pojo.generateEquals("this", "castOther", jdk5)};
}

public int hashCode() {
int result = 17;
<#list pojo.getAllPropertiesIterator() as property>
<#if pojo.generateHashCode(property, "result", "this", jdk5)?trim?length gt 0 >
${pojo.generateHashCode(property, "result", "this", jdk5)}
</#if>
</#list>
return result;
}
</#if>
8 changes: 5 additions & 3 deletions orm/src/main/resources/pojo/PojoExtraClassCode.ftl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<#if pojo.hasMetaAttribute("class-code")> // The following is extra code specified in the hbm.xml files
<#if pojo.hasMetaAttribute("class-code")>

// Extra-code via hbm.xml/RevengStrategy config.
${pojo.getExtraClassCode()}
// end of extra code specified in the hbm.xml files
</#if>
// Extra-code end.
</#if>
13 changes: 9 additions & 4 deletions orm/src/main/resources/pojo/PojoFields.ftl
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<#-- // Fields -->

<#list pojo.getAllPropertiesIterator() as field><#if pojo.getMetaAttribAsBool(field, "gen-property", true)> <#if pojo.hasMetaAttribute(field, "field-description")> /**
<#list pojo.getAllPropertiesIterator() as field>
<#if pojo.getMetaAttribAsBool(field, "gen-property", true)>
<#if pojo.hasMetaAttribute(field, "field-description")>
/**
${pojo.getFieldJavaDoc(field, 0)}
*/
</#if> ${pojo.getFieldModifiers(field)} ${pojo.getJavaTypeName(field, jdk5)} ${c2j.keyWordCheck(field.name)}<#if pojo.hasFieldInitializor(field, jdk5)> = ${pojo.getFieldInitialization(field, jdk5)}</#if>;
</#if>
</#if>
<#if annotateField??><#include "Ejb3FieldGetAnnotation.ftl"/></#if><#--an alternative to property annotation, configured in PojoPropertyAccessors.ftl-->
${pojo.getFieldModifiers(field)} ${pojo.getJavaTypeName(field, jdk5)} ${c2j.keyWordCheck(field.name)}<#if pojo.hasFieldInitializor(field, jdk5)> = ${pojo.getFieldInitialization(field, jdk5)}</#if>;
</#if>
</#list>

17 changes: 12 additions & 5 deletions orm/src/main/resources/pojo/PojoInterfacePropertyAccessors.ftl
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
<#-- if interface -->
<#-- Property accessors for interface -->
<#list pojo.getAllPropertiesIterator() as property><#if pojo.getMetaAttribAsBool(property, "gen-property", true)> /**
${c2j.toJavaDoc(c2j.getMetaAsString(property, "field-description"), 4)} */
${pojo.getPropertyGetModifiers(property)} ${pojo.getJavaTypeName(property, jdk5)} ${pojo.getGetterSignature(property)}();
<#list pojo.getAllPropertiesIterator() as property>
<#if pojo.getMetaAttribAsBool(property, "gen-property", true)>

<#if c2j.getMetaAsString(property, "field-description")?trim?length gt 0>
/**
${c2j.toJavaDoc(c2j.getMetaAsString(property, "field-description"), 0)}
*/
</#if>
${pojo.getPropertyGetModifiers(property)} ${pojo.getJavaTypeName(property, jdk5)} ${pojo.getGetterSignature(property)}();

${pojo.getPropertySetModifiers(property)} void set${pojo.getPropertyName(property)}(${pojo.getJavaTypeName(property, jdk5)} ${property.name});
</#if></#list>
${pojo.getPropertySetModifiers(property)} void set${pojo.getPropertyName(property)}(${pojo.getJavaTypeName(property, jdk5)} ${property.name});
</#if>
</#list>
15 changes: 8 additions & 7 deletions orm/src/main/resources/pojo/PojoPropertyAccessors.ftl
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
<#-- // Property accessors -->
<#list pojo.getAllPropertiesIterator() as property>
<#if pojo.getMetaAttribAsBool(property, "gen-property", true)>
<#if pojo.hasFieldJavaDoc(property)>
/**
<#if pojo.getMetaAttribAsBool(property, "gen-property", true)>
<#if pojo.hasFieldJavaDoc(property)>
/**
* ${pojo.getFieldJavaDoc(property, 4)}
*/
</#if>
<#include "GetPropertyAnnotation.ftl"/>
</#if>

<#if !annotateField??><#include "GetPropertyAnnotation.ftl"/></#if><#-- Property annotations. Field annotation configured in PojoFields.ftl -->
${pojo.getPropertyGetModifiers(property)} ${pojo.getJavaTypeName(property, jdk5)} ${pojo.getGetterSignature(property)}() {
return this.${c2j.keyWordCheck(property.name)};
}

${pojo.getPropertySetModifiers(property)} void set${pojo.getPropertyName(property)}(${pojo.getJavaTypeName(property, jdk5)} ${c2j.keyWordCheck(property.name)}) {
this.${c2j.keyWordCheck(property.name)} = ${c2j.keyWordCheck(property.name)};
}
</#if>
</#if>
</#list>
24 changes: 13 additions & 11 deletions orm/src/main/resources/pojo/PojoToString.ftl
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<#if pojo.needsToString()> /**
<#if pojo.needsToString()>

/**
* toString
* @return String
*/
public String toString() {
StringBuffer buffer = new StringBuffer();

buffer.append(getClass().getName()).append("@").append(Integer.toHexString(hashCode())).append(" [");
<#list pojo.getToStringPropertiesIterator() as property> buffer.append("${property.getName()}").append("='").append(${pojo.getGetterSignature(property)}()).append("' ");
</#list> buffer.append("]");

return buffer.toString();
}
</#if>
public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append(getClass().getName()).append("@").append(Integer.toHexString(hashCode())).append(" [");
<#list pojo.getToStringPropertiesIterator() as property>
buffer.append("${property.getName()}").append("='").append(${pojo.getGetterSignature(property)}()).append("' ");
</#list>
buffer.append("]");
return buffer.toString();
}
</#if>
Loading

0 comments on commit 58c1af2

Please sign in to comment.