Skip to content

Commit b2697cd

Browse files
committed
HBX-2931 Pojo template cleanup.
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. Single line very minor deprecation fix to DocHelper for Column.getSqlType.
1 parent 1ef5828 commit b2697cd

File tree

17 files changed

+363
-98
lines changed

17 files changed

+363
-98
lines changed

orm/src/main/java/org/hibernate/tool/internal/export/doc/DocHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ public String getSQLTypeName(Column column) {
401401

402402
try {
403403
TypeConfiguration tc = ((MetadataImpl)metadata).getTypeConfiguration();
404-
return column.getSqlType(tc, dialect, metadata);
404+
return column.getSqlType(metadata);
405405
} catch (HibernateException ex) {
406406

407407
// TODO: Fix this when we find a way to get the type or

orm/src/main/java/org/hibernate/tool/internal/export/java/ImportContextImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ public String generateImports() {
136136
// dont add automatically "imported" stuff
137137
} else {
138138
if(staticImports.contains(next)) {
139-
buf.append("import static " + next + ";\r\n");
139+
buf.append("import static " + next + ";\n");
140140
} else {
141-
buf.append("import " + next + ";\r\n");
141+
buf.append("import " + next + ";\n");
142142
}
143143
}
144144
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<#if ejb3>
2+
<#if pojo.hasIdentifierProperty()>
3+
<#if field.equals(clazz.identifierProperty)>
4+
${pojo.generateAnnIdGenerator()}<#--has space prepended-->
5+
</#if>
6+
</#if>
7+
<#if c2h.isOneToOne(field)>
8+
${pojo.generateOneToOneAnnotation(field, md)}
9+
<#elseif c2h.isManyToOne(field)>
10+
${pojo.generateManyToOneAnnotation(field)}
11+
<#--TODO support optional and targetEntity-->
12+
${pojo.generateJoinColumnsAnnotation(field, md)}
13+
<#elseif c2h.isCollection(field)>
14+
${pojo.generateCollectionAnnotation(field, md)}
15+
<#else>
16+
<#if pojo.generateBasicAnnotation(field)?has_content >
17+
<#if pojo.generateBasicAnnotation(field)?trim?length gt 0 >
18+
${(pojo.generateBasicAnnotation(field))}<#--has space prepended-->
19+
</#if>
20+
</#if>
21+
${pojo.generateAnnColumnAnnotation(field)}<#--has space prepended-->
22+
</#if>
23+
</#if>

orm/src/main/resources/pojo/Ejb3PropertyGetAnnotation.ftl

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
<#if ejb3>
2-
<#if pojo.hasIdentifierProperty()>
3-
<#if property.equals(clazz.identifierProperty)>
4-
${pojo.generateAnnIdGenerator()}
5-
<#-- if this is the id property (getter)-->
6-
<#-- explicitly set the column name for this property-->
7-
</#if>
8-
</#if>
9-
10-
<#if c2h.isOneToOne(property)>
11-
${pojo.generateOneToOneAnnotation(property, md)}
12-
<#elseif c2h.isManyToOne(property)>
13-
${pojo.generateManyToOneAnnotation(property)}
14-
<#--TODO support optional and targetEntity-->
2+
<#if pojo.hasIdentifierProperty()>
3+
<#if property.equals(clazz.identifierProperty)>
4+
${pojo.generateAnnIdGenerator()}
5+
<#-- if this is the id property (getter)-->
6+
<#-- explicitly set the column name for this property-->
7+
</#if>
8+
</#if>
9+
<#if c2h.isOneToOne(property)>
10+
${pojo.generateOneToOneAnnotation(property, md)}
11+
<#elseif c2h.isManyToOne(property)>
12+
${pojo.generateManyToOneAnnotation(property)}
13+
<#--TODO support optional and targetEntity-->
1514
${pojo.generateJoinColumnsAnnotation(property, md)}
16-
<#elseif c2h.isCollection(property)>
17-
${pojo.generateCollectionAnnotation(property, md)}
18-
<#else>
15+
<#elseif c2h.isCollection(property)>
16+
${pojo.generateCollectionAnnotation(property, md)}
17+
<#else>
18+
<#if pojo.generateBasicAnnotation(property) != " " >
1919
${pojo.generateBasicAnnotation(property)}
20+
</#if>
2021
${pojo.generateAnnColumnAnnotation(property)}
22+
</#if>
2123
</#if>
22-
</#if>

orm/src/main/resources/pojo/Pojo.ftl

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,17 @@ ${pojo.getPackageDeclaration()}
33

44
<#assign classbody>
55
<#include "PojoTypeDeclaration.ftl"/> {
6-
76
<#if !pojo.isInterface()>
87
<#include "PojoFields.ftl"/>
9-
108
<#include "PojoConstructors.ftl"/>
11-
129
<#include "PojoPropertyAccessors.ftl"/>
13-
1410
<#include "PojoToString.ftl"/>
15-
1611
<#include "PojoEqualsHashcode.ftl"/>
17-
1812
<#else>
1913
<#include "PojoInterfacePropertyAccessors.ftl"/>
20-
2114
</#if>
2215
<#include "PojoExtraClassCode.ftl"/>
23-
2416
}
2517
</#assign>
26-
2718
${pojo.generateImports()}
2819
${classbody}
29-
Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
1-
21
<#-- /** default constructor */ -->
32
public ${pojo.getDeclarationName()}() {
43
}
4+
<#if pojo.needsMinimalConstructor()><#-- /** minimal constructor */ -->
55

6-
<#if pojo.needsMinimalConstructor()> <#-- /** minimal constructor */ -->
76
public ${pojo.getDeclarationName()}(${c2j.asParameterList(pojo.getPropertyClosureForMinimalConstructor(), jdk5, pojo)}) {
8-
<#if pojo.isSubclass() && !pojo.getPropertyClosureForSuperclassMinimalConstructor().isEmpty()>
9-
super(${c2j.asArgumentList(pojo.getPropertyClosureForSuperclassMinimalConstructor())});
10-
</#if>
11-
<#list pojo.getPropertiesForMinimalConstructor() as field>
7+
<#if pojo.isSubclass() && !pojo.getPropertyClosureForSuperclassMinimalConstructor().isEmpty()>
8+
super(${c2j.asArgumentList(pojo.getPropertyClosureForSuperclassMinimalConstructor())});
9+
</#if>
10+
<#list pojo.getPropertiesForMinimalConstructor() as field>
1211
this.${c2j.keyWordCheck(field.name)} = ${c2j.keyWordCheck(field.name)};
13-
</#list>
12+
</#list>
1413
}
15-
</#if>
16-
<#if pojo.needsFullConstructor()>
17-
<#-- /** full constructor */ -->
18-
public ${pojo.getDeclarationName()}(${c2j.asParameterList(pojo.getPropertyClosureForFullConstructor(), jdk5, pojo)}) {
19-
<#if pojo.isSubclass() && !pojo.getPropertyClosureForSuperclassFullConstructor().isEmpty()>
20-
super(${c2j.asArgumentList(pojo.getPropertyClosureForSuperclassFullConstructor())});
2114
</#if>
22-
<#list pojo.getPropertiesForFullConstructor() as field>
23-
this.${c2j.keyWordCheck(field.name)} = ${c2j.keyWordCheck(field.name)};
24-
</#list>
15+
<#if pojo.needsFullConstructor()><#-- /** full constructor */ -->
16+
17+
public ${pojo.getDeclarationName()}(${c2j.asParameterList(pojo.getPropertyClosureForFullConstructor(), jdk5, pojo)}) {
18+
<#if pojo.isSubclass() && !pojo.getPropertyClosureForSuperclassFullConstructor().isEmpty()>
19+
super(${c2j.asArgumentList(pojo.getPropertyClosureForSuperclassFullConstructor())});
20+
</#if>
21+
<#list pojo.getPropertiesForFullConstructor() as field>
22+
this.${c2j.keyWordCheck(field.name)} = ${c2j.keyWordCheck(field.name)};
23+
</#list>
2524
}
26-
</#if>
25+
</#if>
Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
<#if pojo.needsEqualsHashCode() && !clazz.superclass?exists>
22
<#assign classNameToCastTo><#if clazz.getProxyInterfaceName?exists>${clazz.getProxyInterfaceName()}<#else>${pojo.getDeclarationName()}</#if></#assign>
3-
public boolean equals(Object other) {
4-
if ( (this == other ) ) return true;
5-
if ( (other == null ) ) return false;
6-
if ( !(other instanceof ${classNameToCastTo}) ) return false;
7-
${classNameToCastTo} castOther = ( ${classNameToCastTo} ) other;
8-
9-
return ${pojo.generateEquals("this", "castOther", jdk5)};
10-
}
11-
12-
public int hashCode() {
13-
int result = 17;
14-
15-
<#list pojo.getAllPropertiesIterator() as property> ${pojo.generateHashCode(property, "result", "this", jdk5)}
16-
</#list> return result;
17-
}
18-
</#if>
3+
4+
public boolean equals(Object other) {
5+
if ( (this == other ) ) return true;
6+
if ( (other == null ) ) return false;
7+
if ( !(other instanceof ${classNameToCastTo}) ) return false;
8+
${classNameToCastTo} castOther = ( ${classNameToCastTo} ) other;
9+
return ${pojo.generateEquals("this", "castOther", jdk5)};
10+
}
11+
12+
public int hashCode() {
13+
int result = 17;
14+
<#list pojo.getAllPropertiesIterator() as property>
15+
<#if pojo.generateHashCode(property, "result", "this", jdk5)?trim?length gt 0 >
16+
${pojo.generateHashCode(property, "result", "this", jdk5)}
17+
</#if>
18+
</#list>
19+
return result;
20+
}
21+
</#if>
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<#if pojo.hasMetaAttribute("class-code")> // The following is extra code specified in the hbm.xml files
1+
<#if pojo.hasMetaAttribute("class-code")>
2+
3+
// Extra-code via hbm.xml/RevengStrategy config.
24
${pojo.getExtraClassCode()}
3-
// end of extra code specified in the hbm.xml files
4-
</#if>
5+
// Extra-code end.
6+
</#if>
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
<#-- // Fields -->
2-
3-
<#list pojo.getAllPropertiesIterator() as field><#if pojo.getMetaAttribAsBool(field, "gen-property", true)> <#if pojo.hasMetaAttribute(field, "field-description")> /**
2+
<#list pojo.getAllPropertiesIterator() as field>
3+
<#if pojo.getMetaAttribAsBool(field, "gen-property", true)>
4+
<#if pojo.hasMetaAttribute(field, "field-description")>
5+
/**
46
${pojo.getFieldJavaDoc(field, 0)}
57
*/
6-
</#if> ${pojo.getFieldModifiers(field)} ${pojo.getJavaTypeName(field, jdk5)} ${c2j.keyWordCheck(field.name)}<#if pojo.hasFieldInitializor(field, jdk5)> = ${pojo.getFieldInitialization(field, jdk5)}</#if>;
7-
</#if>
8+
</#if>
9+
<#if annotateField??><#include "Ejb3FieldGetAnnotation.ftl"/></#if><#--an alternative to property annotation, configured in PojoPropertyAccessors.ftl-->
10+
${pojo.getFieldModifiers(field)} ${pojo.getJavaTypeName(field, jdk5)} ${c2j.keyWordCheck(field.name)}<#if pojo.hasFieldInitializor(field, jdk5)> = ${pojo.getFieldInitialization(field, jdk5)}</#if>;
11+
</#if>
812
</#list>
13+
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
<#-- if interface -->
22
<#-- Property accessors for interface -->
3-
<#list pojo.getAllPropertiesIterator() as property><#if pojo.getMetaAttribAsBool(property, "gen-property", true)> /**
4-
${c2j.toJavaDoc(c2j.getMetaAsString(property, "field-description"), 4)} */
5-
${pojo.getPropertyGetModifiers(property)} ${pojo.getJavaTypeName(property, jdk5)} ${pojo.getGetterSignature(property)}();
3+
<#list pojo.getAllPropertiesIterator() as property>
4+
<#if pojo.getMetaAttribAsBool(property, "gen-property", true)>
5+
6+
<#if c2j.getMetaAsString(property, "field-description")?trim?length gt 0>
7+
/**
8+
${c2j.toJavaDoc(c2j.getMetaAsString(property, "field-description"), 0)}
9+
*/
10+
</#if>
11+
${pojo.getPropertyGetModifiers(property)} ${pojo.getJavaTypeName(property, jdk5)} ${pojo.getGetterSignature(property)}();
612

7-
${pojo.getPropertySetModifiers(property)} void set${pojo.getPropertyName(property)}(${pojo.getJavaTypeName(property, jdk5)} ${property.name});
8-
</#if></#list>
13+
${pojo.getPropertySetModifiers(property)} void set${pojo.getPropertyName(property)}(${pojo.getJavaTypeName(property, jdk5)} ${property.name});
14+
</#if>
15+
</#list>

0 commit comments

Comments
 (0)