[hibernate-commits] Hibernate SVN: r13942 - in branches/Branch_3_2/HibernateExt/tools/src: java/org/hibernate/tool/hbm2x/visitor and 2 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Mon Aug 20 08:34:37 EDT 2007


Author: max.andersen at jboss.com
Date: 2007-08-20 08:34:37 -0400 (Mon, 20 Aug 2007)
New Revision: 13942

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/visitor/JavaTypeFromValueVisitor.java
   branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/BaseTestCase.java
   branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/Hbm2DaoTest.java
   branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/Hbm2EJBDaoTest.java
   branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/Hbm2HibernateDAOTest.java
   branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/Hbm2JavaEjb3Test.java
   branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/Hbm2JavaInitializationTest.java
   branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/JdbcHbm2JavaEjb3Test.java
Log:


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	2007-08-20 11:21:32 UTC (rev 13941)
+++ branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/pojo/BasicPOJOClass.java	2007-08-20 12:34:37 UTC (rev 13942)
@@ -5,21 +5,29 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.TreeMap;
 
 import org.hibernate.cfg.reveng.ReverseEngineeringStrategyUtil;
+import org.hibernate.mapping.Any;
+import org.hibernate.mapping.Array;
+import org.hibernate.mapping.Bag;
 import org.hibernate.mapping.Collection;
 import org.hibernate.mapping.Column;
 import org.hibernate.mapping.Component;
+import org.hibernate.mapping.IdentifierBag;
 import org.hibernate.mapping.MetaAttributable;
 import org.hibernate.mapping.MetaAttribute;
+import org.hibernate.mapping.PrimitiveArray;
 import org.hibernate.mapping.Property;
 import org.hibernate.mapping.PropertyGeneration;
 import org.hibernate.mapping.Selectable;
+import org.hibernate.mapping.Set;
 import org.hibernate.mapping.SimpleValue;
 import org.hibernate.mapping.Value;
 import org.hibernate.tool.hbm2x.Cfg2JavaTool;
 import org.hibernate.tool.hbm2x.MetaAttributeConstants;
 import org.hibernate.tool.hbm2x.MetaAttributeHelper;
+import org.hibernate.tool.hbm2x.visitor.DefaultValueVisitor;
 import org.hibernate.util.StringHelper;
 
 /**
@@ -851,11 +859,48 @@
 		return c2j.getJavaTypeName(p, useGenerics, this);
 	}
 	
+	static private class DefaultInitializor {
+		
+		private final String type;
+		private final boolean initToZero;
+		
+		public DefaultInitializor(String type, boolean initToZero) {
+			this.type = type;
+			this.initToZero = initToZero;					
+		}
+		
+		public String getDefaultValue(String comparator, String genericDeclaration, ImportContext importContext) {
+			StringBuffer val = new StringBuffer("new " + importContext.importType(type));
+			if(genericDeclaration!=null) {
+				val.append(genericDeclaration);
+			}
+			
+			val.append("(");
+			if(comparator!=null) {
+				val.append("new ");
+				val.append(importContext.importType(comparator));
+				val.append("()");
+				if(initToZero) val.append(",");
+			}
+			if(initToZero) {
+				val.append("0");
+			}
+			val.append(")");
+			return val.toString();
+		}
+		
+		public String getType() {
+			return type;
+		}
+	}
+	
 	static Map defaultInitializors = new HashMap();
 	static {
-		defaultInitializors.put("java.util.List", "java.util.ArrayList");
-		defaultInitializors.put("java.util.Map", "java.util.HashMap");
-		defaultInitializors.put("java.util.Set", "java.util.HashSet");		
+		defaultInitializors.put("java.util.List", new DefaultInitializor("java.util.ArrayList", true));
+		defaultInitializors.put("java.util.Map", new DefaultInitializor("java.util.HashMap", true));
+		defaultInitializors.put("java.util.Set", new DefaultInitializor("java.util.HashSet",true));		
+		defaultInitializors.put("java.util.SortedSet", new DefaultInitializor("java.util.TreeSet", false));
+		defaultInitializors.put("java.util.SortedMap", new DefaultInitializor("java.util.TreeMap", false));
 	}
 	
 	public boolean hasFieldInitializor(Property p, boolean useGenerics) {
@@ -863,22 +908,67 @@
 	}
 	
 	public String getFieldInitialization(Property p, boolean useGenerics) {
-		String javaTypeName = c2j.getJavaTypeName(p, false);
 		if(hasMetaAttribute(p, "default-value")) {
 			return MetaAttributeHelper.getMetaAsString( p.getMetaAttribute( "default-value" ) );
 		}
-		if(javaTypeName==null) {
-			return null;
+		if(c2j.getJavaTypeName(p, false)==null) {
+			throw new IllegalArgumentException();
 		} else if (p.getValue() instanceof Collection) {
-			String initialization = (String) defaultInitializors.get(javaTypeName);
+			Collection col = (Collection) p.getValue();
 			
-			String decl = null;
-			
-			if(useGenerics) {
-				decl = c2j.getGenericCollectionDeclaration((Collection) p.getValue(), true, importContext);
-			}
-			if(initialization!=null) {			
-				return "new " + importType(initialization) + (decl==null?"":decl) + "(0)";
+			DefaultInitializor initialization = (DefaultInitializor) col.accept(new DefaultValueVisitor(true) {
+			 
+				public Object accept(Bag o) {
+					return new DefaultInitializor("java.util.ArrayList", true);
+				}
+				
+				public Object accept(org.hibernate.mapping.List o) {
+					return new DefaultInitializor("java.util.ArrayList", true);
+				}
+				
+				public Object accept(org.hibernate.mapping.Map o) {
+					if(o.isSorted()) {
+						return new DefaultInitializor("java.util.TreeMap", false);
+					} else {
+						return new DefaultInitializor("java.util.HashMap", true);
+					}
+				}
+				
+				public Object accept(IdentifierBag o) {
+					return new DefaultInitializor("java.util.ArrayList", true);
+				}
+				
+				public Object accept(Set o) {
+					if(o.isSorted()) {
+						return new DefaultInitializor("java.util.TreeSet", false);
+					} else {
+						return new DefaultInitializor("java.util.HashSet", true);
+					}
+				}
+				
+				
+				public Object accept(PrimitiveArray o) {
+					return null; // TODO: default init for arrays ?
+				}
+				
+				public Object accept(Array o) {
+					return null;// TODO: default init for arrays ?
+				}
+				
+			});
+						 
+			if(initialization!=null) {
+				String comparator = null;
+				String decl = null;
+
+				if(col.isSorted()) {
+					comparator = col.getComparatorClassName();
+				}
+
+				if(useGenerics) {
+					decl = c2j.getGenericCollectionDeclaration((Collection) p.getValue(), true, importContext);
+				}
+				return initialization.getDefaultValue(comparator, decl, this);
 			} else {
 				return null;
 			}
@@ -888,3 +978,4 @@
 	}	
 	
 }
+ 
\ No newline at end of file

Modified: branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/visitor/JavaTypeFromValueVisitor.java
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/visitor/JavaTypeFromValueVisitor.java	2007-08-20 11:21:32 UTC (rev 13941)
+++ branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/visitor/JavaTypeFromValueVisitor.java	2007-08-20 12:34:37 UTC (rev 13942)
@@ -1,10 +1,14 @@
 package org.hibernate.tool.hbm2x.visitor;
 
+import java.util.List;
+
 import org.hibernate.HibernateException;
 import org.hibernate.mapping.Component;
 import org.hibernate.mapping.ManyToOne;
+import org.hibernate.mapping.Map;
 import org.hibernate.mapping.OneToMany;
 import org.hibernate.mapping.OneToOne;
+import org.hibernate.mapping.Set;
 import org.hibernate.mapping.SimpleValue;
 import org.hibernate.mapping.ToOne;
 import org.hibernate.mapping.Value;
@@ -12,6 +16,7 @@
 import org.hibernate.type.CompositeCustomType;
 import org.hibernate.type.CustomType;
 import org.hibernate.type.Type;
+import org.hibernate.type.TypeFactory;
 
 public class JavaTypeFromValueVisitor extends DefaultValueVisitor {
 
@@ -22,6 +27,22 @@
 		super( true );
 	}
 	
+	// special handling for Map's to avoid initialization of comparators that depends on the keys/values which might not be generated yet.
+	public Object accept(Map o) {
+		if ( o.isSorted() ) {
+			return "java.util.SortedMap";
+		}
+		return super.accept(o);
+	}
+	
+	// special handling for Set's to avoid initialization of comparators that depends on the keys/values which might not be generated yet.
+	public Object accept(Set o) {
+		if ( o.isSorted() ) {
+			return "java.util.SortedSet";
+		}
+		return super.accept(o);
+	}
+
 	public Object accept(Component value) {
 		// composite-element breaks without it.
 		return value.getComponentClassName();

Modified: branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/BaseTestCase.java
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/BaseTestCase.java	2007-08-20 11:21:32 UTC (rev 13941)
+++ branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/BaseTestCase.java	2007-08-20 12:34:37 UTC (rev 13942)
@@ -1,6 +1,9 @@
 package org.hibernate.tool;
 
 import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
 import java.sql.Connection;
 import java.sql.SQLException;
 import java.util.Iterator;
@@ -163,5 +166,29 @@
 		assertNotNull(document);
         return document;
     }
+	
+	protected void generateComparator() throws IOException {
+		File file = new File(getOutputDir().getAbsolutePath() + "/comparator/NoopComparator.java");
+		file.getParentFile().mkdirs();
+		
+		FileWriter fileWriter = new FileWriter(file);
+		PrintWriter pw = new PrintWriter(fileWriter);
+		
+		pw.println("package comparator;");
+
+		pw.println("import java.util.Comparator;");
+
+		pw.println("public class NoopComparator implements Comparator {\n" + 
+				"\n" + 
+				"			public int compare(Object o1, Object o2) {\n" + 
+				"				return 0;\n" + 
+				"			}\n" + 
+				"\n" + 
+				"		}\n" + 
+				"");
+		
+		pw.flush();
+		pw.close();
+	}
 }
 

Modified: branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/Hbm2DaoTest.java
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/Hbm2DaoTest.java	2007-08-20 11:21:32 UTC (rev 13941)
+++ branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/Hbm2DaoTest.java	2007-08-20 12:34:37 UTC (rev 13942)
@@ -5,6 +5,7 @@
 package org.hibernate.tool.hbm2x;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -38,11 +39,13 @@
 		assertFileAndExists(new File(getOutputDir(), "org/hibernate/tool/hbm2x/AuthorHome.java") );
 	}
 	
-	public void testCompilable() {
+	public void testCompilable() throws IOException {
 		
 		File file = new File("compilable");
 		file.mkdir();
 		
+		generateComparator();
+		
 		List list = new ArrayList();
 		TestHelper.compile(getOutputDir(), file, TestHelper.visitAllFiles(getOutputDir(), list), "1.5", "" );
 		

Modified: branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/Hbm2EJBDaoTest.java
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/Hbm2EJBDaoTest.java	2007-08-20 11:21:32 UTC (rev 13941)
+++ branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/Hbm2EJBDaoTest.java	2007-08-20 12:34:37 UTC (rev 13942)
@@ -5,6 +5,7 @@
 package org.hibernate.tool.hbm2x;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -37,8 +38,10 @@
 		assertFileAndExists(new File(getOutputDir(), "org/hibernate/tool/hbm2x/AuthorHome.java") );
 	}
 	
-	public void testCompilable() {
+	public void testCompilable() throws IOException {
 		
+		generateComparator();
+		
 		File file = new File("compilable");
 		file.mkdir();
 		

Modified: branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/Hbm2HibernateDAOTest.java
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/Hbm2HibernateDAOTest.java	2007-08-20 11:21:32 UTC (rev 13941)
+++ branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/Hbm2HibernateDAOTest.java	2007-08-20 12:34:37 UTC (rev 13942)
@@ -5,6 +5,7 @@
 package org.hibernate.tool.hbm2x;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -24,6 +25,8 @@
 	protected void setUp() throws Exception {
 		super.setUp();
 		
+		
+		
 		POJOExporter javaExporter = new POJOExporter(getCfg(), getOutputDir() );
 		POJOExporter exporter = new DAOExporter(getCfg(), getOutputDir() );
 		exporter.getProperties().setProperty("ejb3", "false");
@@ -37,8 +40,9 @@
 		assertFileAndExists(new File(getOutputDir(), "org/hibernate/tool/hbm2x/AuthorHome.java") );
 	}
 	
-	public void testCompilable() {
+	public void testCompilable() throws IOException {
 		
+		generateComparator();
 		File file = new File("compilable");
 		file.mkdir();
 		

Modified: branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/Hbm2JavaEjb3Test.java
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/Hbm2JavaEjb3Test.java	2007-08-20 11:21:32 UTC (rev 13941)
+++ branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/Hbm2JavaEjb3Test.java	2007-08-20 12:34:37 UTC (rev 13942)
@@ -6,6 +6,7 @@
 
 import java.io.File;
 import java.io.FileWriter;
+import java.io.IOException;
 import java.io.PrintWriter;
 import java.util.ArrayList;
 import java.util.List;
@@ -32,28 +33,8 @@
 	protected void setUp() throws Exception {
 		super.setUp();
 
-		File file = new File(getOutputDir().getAbsolutePath() + "/comparator/NoopComparator.java");
-		file.getParentFile().mkdirs();
+		generateComparator();
 		
-		FileWriter fileWriter = new FileWriter(file);
-		PrintWriter pw = new PrintWriter(fileWriter);
-		
-		pw.println("package comparator;");
-
-		pw.println("import java.util.Comparator;");
-
-		pw.println("public class NoopComparator implements Comparator {\n" + 
-				"\n" + 
-				"			public int compare(Object o1, Object o2) {\n" + 
-				"				return 0;\n" + 
-				"			}\n" + 
-				"\n" + 
-				"		}\n" + 
-				"");
-		
-		pw.flush();
-		pw.close();
-		
 		POJOExporter exporter = new POJOExporter(getCfg(), getOutputDir() );
 		exporter.setTemplatePath(new String[0]);
 		exporter.getProperties().setProperty("ejb3", "true");
@@ -62,6 +43,8 @@
 		exporter.start();
 	}
 
+	
+
 	public void testFileExistence() {
 		assertFileAndExists( new File(getOutputDir().getAbsolutePath() + "/org/hibernate/tool/hbm2x/Author.java") );
 		assertFileAndExists( new File(getOutputDir().getAbsolutePath() + "/org/hibernate/tool/hbm2x/Article.java") );

Modified: branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/Hbm2JavaInitializationTest.java
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/Hbm2JavaInitializationTest.java	2007-08-20 11:21:32 UTC (rev 13941)
+++ branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/Hbm2JavaInitializationTest.java	2007-08-20 12:34:37 UTC (rev 13942)
@@ -72,6 +72,17 @@
 		assertEquals("new ArrayList<Article>(0)", clazz.getFieldInitialization(p, true));
 		assertEquals("new ArrayList(0)", clazz.getFieldInitialization(p, false));
 
+		p = classMapping.getProperty("bagstrings");
+		
+		assertEquals("Bag's are just a collection", "java.util.Collection", cfg2java.getJavaTypeName( p, false ));
+		assertEquals("Should be a a generic'd collection when generics=true", "java.util.Collection<java.lang.String>", cfg2java.getJavaTypeName( p, true ));
+		assertEquals("Collection<String>",cfg2java.getJavaTypeName(p, true, clazz));		
+		assertEquals("new ArrayList<String>(0)", clazz.getFieldInitialization(p, true));
+		assertEquals("new ArrayList(0)", clazz.getFieldInitialization(p, false));
+
+		p = classMapping.getProperty("bagstrings");
+		assertEquals("new ArrayList(0)", clazz.getFieldInitialization(p, false));
+		
 		p = classMapping.getProperty("naturalSortedArticlesMap");
 
 		assertEquals("java.util.SortedMap", cfg2java.getJavaTypeName( p, false));

Modified: branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/JdbcHbm2JavaEjb3Test.java
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/JdbcHbm2JavaEjb3Test.java	2007-08-20 11:21:32 UTC (rev 13941)
+++ branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/JdbcHbm2JavaEjb3Test.java	2007-08-20 12:34:37 UTC (rev 13942)
@@ -36,7 +36,7 @@
 
 	public void testUniqueConstraints() {
 		assertEquals(null, findFirstString( "uniqueConstraints", new File(getOutputDir(),"Master.java") ));
-		assertNotNull(findFirstString( "uniqueConstraints", new File(getOutputDir(),"UniqueMaster.java") ));
+		assertNotNull(findFirstString( "uniqueConstraints", new File(getOutputDir(),"Uniquemaster.java") ));
 	}
 	public void testCompile() {
 




More information about the hibernate-commits mailing list