Skip to content

Commit 85a3d70

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: -field annotations -optimistic lock @Version annotation -extra class code generation using MetaAttribute logic.
1 parent 251b6e6 commit 85a3d70

File tree

16 files changed

+362
-97
lines changed

16 files changed

+362
-97
lines changed

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

+2-2
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
}
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

+18-17
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

-10
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-
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>
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>
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>
+9-4
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+
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>
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
<#-- // Property accessors -->
22
<#list pojo.getAllPropertiesIterator() as property>
3-
<#if pojo.getMetaAttribAsBool(property, "gen-property", true)>
4-
<#if pojo.hasFieldJavaDoc(property)>
5-
/**
3+
<#if pojo.getMetaAttribAsBool(property, "gen-property", true)>
4+
<#if pojo.hasFieldJavaDoc(property)>
5+
/**
66
* ${pojo.getFieldJavaDoc(property, 4)}
77
*/
8-
</#if>
9-
<#include "GetPropertyAnnotation.ftl"/>
8+
</#if>
9+
10+
<#if !annotateField??><#include "GetPropertyAnnotation.ftl"/></#if><#-- Property annotations. Field annotation configured in PojoFields.ftl -->
1011
${pojo.getPropertyGetModifiers(property)} ${pojo.getJavaTypeName(property, jdk5)} ${pojo.getGetterSignature(property)}() {
1112
return this.${c2j.keyWordCheck(property.name)};
1213
}
13-
14+
1415
${pojo.getPropertySetModifiers(property)} void set${pojo.getPropertyName(property)}(${pojo.getJavaTypeName(property, jdk5)} ${c2j.keyWordCheck(property.name)}) {
1516
this.${c2j.keyWordCheck(property.name)} = ${c2j.keyWordCheck(property.name)};
1617
}
17-
</#if>
18+
</#if>
1819
</#list>
+13-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
<#if pojo.needsToString()> /**
1+
<#if pojo.needsToString()>
2+
3+
/**
24
* toString
35
* @return String
46
*/
5-
public String toString() {
6-
StringBuffer buffer = new StringBuffer();
7-
8-
buffer.append(getClass().getName()).append("@").append(Integer.toHexString(hashCode())).append(" [");
9-
<#list pojo.getToStringPropertiesIterator() as property> buffer.append("${property.getName()}").append("='").append(${pojo.getGetterSignature(property)}()).append("' ");
10-
</#list> buffer.append("]");
11-
12-
return buffer.toString();
13-
}
14-
</#if>
7+
public String toString() {
8+
StringBuffer buffer = new StringBuffer();
9+
buffer.append(getClass().getName()).append("@").append(Integer.toHexString(hashCode())).append(" [");
10+
<#list pojo.getToStringPropertiesIterator() as property>
11+
buffer.append("${property.getName()}").append("='").append(${pojo.getGetterSignature(property)}()).append("' ");
12+
</#list>
13+
buffer.append("]");
14+
return buffer.toString();
15+
}
16+
</#if>

0 commit comments

Comments
 (0)