Author: max.andersen(a)jboss.com
Date: 2006-11-01 09:31:16 -0500 (Wed, 01 Nov 2006)
New Revision: 10675
Added:
branches/Branch_3_2/HibernateExt/tools/src/templates/doc/inherit.gif
Modified:
branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/Cfg2JavaTool.java
branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/pojo/BasicPOJOClass.java
branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/pojo/ImportContextImpl.java
branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/Customer.hbm.xml
branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/Hbm2JavaTest.java
Log:
HBX-794 Inner-class names lose their outer-class context
Modified:
branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/Cfg2JavaTool.java
===================================================================
---
branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/Cfg2JavaTool.java 2006-11-01
06:55:20 UTC (rev 10674)
+++
branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/Cfg2JavaTool.java 2006-11-01
14:31:16 UTC (rev 10675)
@@ -178,9 +178,6 @@
}
else {
String rawType = getRawTypeName( p, useGenerics, true, importContext );
- if(rawType!=null) {
- rawType = rawType.replace( '$', '.' );
- }
if(rawType==null) {
throw new IllegalStateException("getJavaTypeName *must* return a
value");
}
Modified:
branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/pojo/BasicPOJOClass.java
===================================================================
---
branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/pojo/BasicPOJOClass.java 2006-11-01
06:55:20 UTC (rev 10674)
+++
branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/pojo/BasicPOJOClass.java 2006-11-01
14:31:16 UTC (rev 10675)
@@ -74,25 +74,16 @@
/** Return package name. Note: Does not handle inner classes */
public String getPackageName() {
- String generatedClass =
getMetaAsString(MetaAttributeConstants.GENERATED_CLASS).trim();
- if(StringHelper.isEmpty(generatedClass) ) {
- generatedClass = getMappedClassName();
- }
- if(generatedClass==null) return ""; // will occur for
<dynamic-component>
+ String generatedClass = getGeneratedClassName();
return StringHelper.qualifier(generatedClass.trim());
}
public String getShortName() {
- return StringHelper.unqualify(getMappedClassName());
+ return qualifyInnerClass(StringHelper.unqualify(getMappedClassName()));
}
public String getQualifiedDeclarationName() {
- String generatedName = getMetaAsString( MetaAttributeConstants.GENERATED_CLASS );
- if ( generatedName == null || generatedName.trim().length() == 0 ) {
- generatedName = getMappedClassName();
- }
-
- generatedName = generatedName.replace( '$', '.' ).trim();
+ String generatedName = qualifyInnerClass(getGeneratedClassName());
String qualifier = StringHelper.qualifier( getMappedClassName() );
if ( "".equals( qualifier ) ) {
return qualifier + "." + generatedName;
@@ -106,9 +97,24 @@
* @return unqualified classname for this class (can be changed by meta attribute
"generated-class")
*/
public String getDeclarationName() {
- return StringHelper.unqualify( getQualifiedDeclarationName() );
+ return qualifyInnerClass(StringHelper.unqualify( getGeneratedClassName() ));
}
+ protected String getGeneratedClassName()
+ {
+ String generatedClass =
getMetaAsString(MetaAttributeConstants.GENERATED_CLASS).trim();
+ if(StringHelper.isEmpty(generatedClass) ) {
+ generatedClass = getMappedClassName();
+ }
+ if(generatedClass==null) return ""; // will occur for
<dynamic-component>
+ return generatedClass;
+ }
+
+ protected String qualifyInnerClass(String className)
+ {
+ return className.replace('$', '.');
+ }
+
protected abstract String getMappedClassName();
public String getMetaAsString(String attribute) {
Modified:
branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/pojo/ImportContextImpl.java
===================================================================
---
branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/pojo/ImportContextImpl.java 2006-11-01
06:55:20 UTC (rev 10674)
+++
branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/pojo/ImportContextImpl.java 2006-11-01
14:31:16 UTC (rev 10675)
@@ -50,7 +50,7 @@
* @return import string
*/
public String importType(String fqcn) {
- String result = fqcn;
+ String result = fqcn;
String additionalTypePart = null;
if(fqcn.indexOf('<')>=0) {
@@ -63,35 +63,38 @@
fqcn = result;
}
+ String pureFqcn = fqcn.replace( '$', '.' );
+
boolean canBeSimple = true;
String simpleName = StringHelper.unqualify(fqcn);
if(simpleNames.containsKey(simpleName)) {
String existingFqcn = (String) simpleNames.get(simpleName);
- if(existingFqcn.equals(fqcn)) {
+ if(existingFqcn.equals(pureFqcn)) {
canBeSimple = true;
} else {
canBeSimple = false;
}
} else {
canBeSimple = true;
- simpleNames.put(simpleName, fqcn);
- imports.add( fqcn );
+ simpleNames.put(simpleName, pureFqcn);
+ imports.add( pureFqcn );
}
- if ( inSamePackage(fqcn) || (imports.contains( fqcn ) && canBeSimple) ) {
+ if ( inSamePackage(fqcn) || (imports.contains( pureFqcn ) && canBeSimple) ) {
result = StringHelper.unqualify( result ); // dequalify
} else if ( inJavaLang( fqcn ) ) {
result = result.substring( "java.lang.".length() );
}
if(additionalTypePart!=null) {
- return result + additionalTypePart;
- } else {
- return result;
- }
+ result = result + additionalTypePart;
+ }
+
+ result = result.replace( '$', '.' );
+ return result;
}
public String staticImport(String fqcn, String member) {
@@ -129,7 +132,7 @@
for ( Iterator imps = imports.iterator(); imps.hasNext(); ) {
String next = (String) imps.next();
- if(isPrimitive(next) || inDefaultPackage(next) || inJavaLang(next) ||
inSamePackage(next)) {
+ if(isPrimitive(next) || inDefaultPackage(next) || inJavaLang(next) ||
inSamePackage(next)) {
// dont add automatically "imported" stuff
} else {
if(staticImports.contains(next)) {
@@ -140,6 +143,9 @@
}
}
+ if(buf.indexOf( "$" )>=0) {
+ return buf.toString();
+ }
return buf.toString();
}
}
Added: branches/Branch_3_2/HibernateExt/tools/src/templates/doc/inherit.gif
===================================================================
(Binary files differ)
Property changes on: branches/Branch_3_2/HibernateExt/tools/src/templates/doc/inherit.gif
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
Modified:
branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/Customer.hbm.xml
===================================================================
---
branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/Customer.hbm.xml 2006-11-01
06:55:20 UTC (rev 10674)
+++
branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/Customer.hbm.xml 2006-11-01
14:31:16 UTC (rev 10675)
@@ -96,6 +96,11 @@
<many-to-many class="Product" />
</array>
+ <!-- hbm2java can't generate a innerclass (Test$Entry) correctly. should probably
skip it as a fallback.-->
+ <!-- <component name="mapEntry"
class="java.util.Map$Entry">
+ <property name="value" type="string"/>
+ <property name="key" type="string"/>
+ </component> -->
</class>
Modified:
branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/Hbm2JavaTest.java
===================================================================
---
branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/Hbm2JavaTest.java 2006-11-01
06:55:20 UTC (rev 10674)
+++
branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/Hbm2JavaTest.java 2006-11-01
14:31:16 UTC (rev 10675)
@@ -379,18 +379,45 @@
assertEquals("Entity", context.importType("org.test.Entity"));
assertEquals("org.other.test.Entity",
context.importType("org.other.test.Entity"));
-
+
assertEquals("Collection<org.marvel.Hulk>",
context.importType("java.util.Collection<org.marvel.Hulk>"));
assertEquals("Map<java.lang.String, org.marvel.Hulk>",
context.importType("java.util.Map<java.lang.String, org.marvel.Hulk>"));
assertEquals("Collection<org.marvel.Hulk>[]",
context.importType("java.util.Collection<org.marvel.Hulk>[]"));
- assertEquals("Map<java.lang.String, org.marvel.Hulk>",
context.importType("java.util.Map<java.lang.String, org.marvel.Hulk>"));
+ assertEquals("Map<java.lang.String, org.marvel.Hulk>",
context.importType("java.util.Map<java.lang.String,
org.marvel.Hulk>"));
String string = context.generateImports();
assertTrue(string.indexOf("import org.hibernate.Session;")<0);
assertTrue(string.indexOf("import org.test.Entity;")>0);
- assertTrue("Entity can only be imported once", string.indexOf("import
org.other.test.Entity;")<0);
+ assertTrue("Entity can only be imported once", string.indexOf("import
org.other.test.Entity;")<0);
assertFalse(string.indexOf("<")>=0);
+ assertEquals("Outer.Entity",
context.importType("org.test.Outer$Entity"));
+ assertEquals("org.other.test.Outer.Entity",
context.importType("org.other.test.Outer$Entity"));
+
+ assertEquals("Collection<org.marvel.Outer.Hulk>",
context.importType("java.util.Collection<org.marvel.Outer$Hulk>"));
+ assertEquals("Map<java.lang.String, org.marvel.Outer.Hulk>",
context.importType("java.util.Map<java.lang.String,
org.marvel.Outer$Hulk>"));
+ assertEquals("Collection<org.marvel.Outer.Hulk>[]",
context.importType("java.util.Collection<org.marvel.Outer$Hulk>[]"));
+ assertEquals("Map<java.lang.String, org.marvel.Outer.Hulk>",
context.importType("java.util.Map<java.lang.String,
org.marvel.Outer$Hulk>"));
+
+
+ //assertEquals("Test.Entry",
context.importType("org.hibernate.Test.Entry")); what should be the behavior for
this ?
+ assertEquals("Test.Entry",
context.importType("org.hibernate.Test$Entry"));
+
+ assertEquals("Map.Entry",
context.importType("java.util.Map$Entry"));
+ assertEquals("Entry", context.importType("java.util.Map.Entry"));
// we can't detect that it is the same class here unless we try an load all strings so
we fall back to default class name.
+
+ assertEquals("List<java.util.Map.Entry>", context.importType(
"java.util.List<java.util.Map$Entry>" ));
+ assertEquals("List<org.hibernate.Test.Entry>", context.importType(
"java.util.List<org.hibernate.Test$Entry>" ));
+
+
+ string = context.generateImports();
+
+ assertTrue(string.indexOf("import java.util.Map")>=0);
+ assertTrue(string.indexOf("import java.utilMap$")<0);
+ assertTrue(string.indexOf("$")<0);
+
+
+
}
public void testEqualsHashCode() {