[hibernate-issues] [Hibernate-JIRA] Commented: (HBX-67) Add annotations to java templates

miguel de anda (JIRA) noreply at atlassian.com
Fri Apr 18 18:01:33 EDT 2008


    [ http://opensource.atlassian.com/projects/hibernate/browse/HBX-67?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_30024 ] 

miguel de anda commented on HBX-67:
-----------------------------------

I couldn't figure out where the original bug was but I've created a fix for this. We need to get annotations to make our hbm2java objects searchable with hibernate-search. I had a hard time getting a clean `svn diff` as i have different line breaks (I think its the reason). Anyway, here is some info on what I did:

$svn info
Path: .
URL: http://anonsvn.jboss.org/repos/hibernate/trunk/HibernateExt/tools
Repository Root: http://anonsvn.jboss.org/repos/hibernate
Repository UUID: 1b8cb986-b30d-0410-93ca-fae66ebed9b2
Revision: 14513
Node Kind: directory
Schedule: normal
Last Changed Author: max.andersen at jboss.com
Last Changed Rev: 10633
Last Changed Date: 2006-10-19 10:38:50 -0700 (Thu, 19 Oct 2006)


$svn stat
?      bin
M      src/java/org/hibernate/tool/hbm2x/pojo/BasicPOJOClass.java
M      src/java/org/hibernate/tool/hbm2x/pojo/POJOClass.java
M      src/templates/pojo/PojoFields.ftl
M      src/templates/pojo/PojoTypeDeclaration.ftl
M      src/templates/pojo/PojoPropertyAccessors.ftl


Additions or the entire file (as applicable)
src/java/org/hibernate/tool/hbm2x/pojo/POJOClass.java
+	public boolean hasFieldAnnotations(Property p);
+	public String getFieldAnnotations(Property p, int indent);
+	public boolean hasClassAnnotations();
+	public String getClassAnnotations(int indent);
+	public boolean hasGetterAnnotations(Property p);
+	public String getGetterAnnotations(Property p, int indent);

src/java/org/hibernate/tool/hbm2x/pojo/BasicPOJOClass.java
+import org.hibernate.tool.hbm2x.StringUtils;
...
+	private boolean hasPropertyAttribute(Property property, String attribute) {
+		return property!=null && property.getMetaAttribute(attribute)!=null;
+	}
+
+	private String getAnnotations(MetaAttribute c, int indent) {
+		StringBuffer sb = new StringBuffer();
+		Iterator it = c.getValues().iterator();
+		while (it.hasNext()) {
+			String v = (String)it.next();
+			sb.append(StringUtils.leftPad( v, v.length() + indent ));
+			if (it.hasNext())
+				sb.append("\n");
+		}
+		return sb.toString();
+	}
+
+	private String getAnnotations(Property property, String attribute, int indent) {
+		MetaAttribute c = property.getMetaAttribute( attribute );
+		if ( c == null ) {
+			return "";
+		} else {
+			return getAnnotations(c, indent);
+		}
+	}
+
+	public boolean hasClassAnnotations() {
+		return meta.getMetaAttribute( "class-annotation" ) != null;
+	}
+
+	public String getClassAnnotations(int indent) {
+		MetaAttribute c = meta.getMetaAttribute( "class-annotation" );
+		if ( c == null ) {
+			return "";
+		} else {
+			return getAnnotations(c, indent);
+		}
+	}
+
+	public boolean hasFieldAnnotations(Property property) {
+		return hasPropertyAttribute(property, "field-annotation");
+	}
+
+	public String getFieldAnnotations(Property property, int indent) {
+		return getAnnotations(property, "field-annotation", indent);
+	}
+
+	public boolean hasGetterAnnotations(Property property) {
+		return hasPropertyAttribute(property, "getter-annotation");
+	}
+
+	public String getGetterAnnotations(Property property, int indent) {
+		return getAnnotations(property, "getter-annotation", indent);
+	}

src/templates/pojo/PojoFields.ftl (entire file pasted)
    // Fields

<#foreach field in pojo.getAllPropertiesIterator()><#if pojo.getMetaAttribAsBool(field, "gen-property", true)> <#if pojo.hasMetaAttribute(field, "field-description")>    /**
     ${pojo.getFieldJavaDoc(field, 0)}
     */
 </#if><#if pojo.hasFieldAnnotations(field)>
${pojo.getFieldAnnotations(field, 4)}
 </#if>    ${pojo.getFieldModifiers(field)} ${pojo.getJavaTypeName(field, jdk5)} ${field.name}<#if pojo.hasFieldInitializor(field, jdk5)> = ${pojo.getFieldInitialization(field, jdk5)}</#if>;
</#if>
</#foreach>

src/templates/pojo/PojoTypeDeclaration.ftl (entire file pasted)
/**
${pojo.getClassJavaDoc(pojo.getDeclarationName() + " generated by hbm2java", 0)}
 */<#if pojo.hasClassAnnotations()>
${pojo.getClassAnnotations(0)}
 </#if>
<#include "Ejb3TypeDeclaration.ftl"/>
${pojo.getClassModifiers()} ${pojo.getDeclarationType()} ${pojo.getDeclarationName()} ${pojo.getExtendsDeclaration()} ${pojo.getImplementsDeclaration()}

src/templates/pojo/PojoPropertyAccessors.ftl (entire file pasted)
    // Property accessors
<#foreach property in pojo.getAllPropertiesIterator()>
<#if pojo.getMetaAttribAsBool(property, "gen-property", true)>
 <#if pojo.hasFieldJavaDoc(property)>
    /**
${pojo.getFieldJavaDoc(property, 4)}
     */
</#if>
 <#if pojo.hasGetterAnnotations(property)>
${pojo.getGetterAnnotations(property, 4)}
 </#if>
    <#include "Ejb3PropertyGetAnnotation.ftl"/>
    ${pojo.getPropertyGetModifiers(property)} ${pojo.getJavaTypeName(property, jdk5)} ${pojo.getGetterSignature(property)}() {
        return this.${property.name};
    }

    ${pojo.getPropertySetModifiers(property)} void set${pojo.getPropertyName(property)}(${pojo.getJavaTypeName(property, jdk5)} ${property.name}) {
        this.${property.name} = ${property.name};
    }
</#if>
</#foreach>


> Add annotations to java templates
> ---------------------------------
>
>                 Key: HBX-67
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HBX-67
>             Project: Hibernate Tools
>          Issue Type: New Feature
>            Reporter: Max Rydahl Andersen
>
> Need to add the basic jdk 1.5 annotations to the java velocity template.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        



More information about the hibernate-issues mailing list