[hibernate-commits] Hibernate SVN: r20770 - in jpamodelgen/trunk/src: test/java/org/hibernate/jpamodelgen/test and 1 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Mon Oct 4 09:45:29 EDT 2010


Author: hardy.ferentschik
Date: 2010-10-04 09:45:28 -0400 (Mon, 04 Oct 2010)
New Revision: 20770

Added:
   jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/embeddablemappedsuperclass/
   jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/embeddablemappedsuperclass/EmbeddableAndMappedSuperClass.java
   jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/embeddablemappedsuperclass/EmbeddableMappedSuperClassTest.java
Modified:
   jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java
Log:
METAGEN-36 Now we keep at list in JPAMetaModelEntityProcessor.createMetaModelClasses() of all generated source files to avoid writing a file twice.

Modified: jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java
===================================================================
--- jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java	2010-10-04 09:46:17 UTC (rev 20769)
+++ jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java	2010-10-04 13:45:28 UTC (rev 20770)
@@ -19,6 +19,7 @@
 
 package org.hibernate.jpamodelgen;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.List;
@@ -139,21 +140,30 @@
 	}
 
 	private void createMetaModelClasses() {
+		// keep track of all classes for which model have been generated
+		Collection<String> generatedModelClasses = new ArrayList<String>();
+
 		for ( MetaEntity entity : context.getMetaEntities() ) {
 			context.logMessage( Diagnostic.Kind.OTHER, "Writing meta model for entity " + entity );
 			ClassWriter.writeFile( entity, context );
+			generatedModelClasses.add( entity.getQualifiedName() );
 		}
 
 		// we cannot process the delayed entities in any order. There might be dependencies between them.
 		// we need to process the top level entities first
-		// TODO make sure that we don't run into circular dependencies here
 		Collection<MetaEntity> toProcessEntities = context.getMetaEmbeddables();
 		while ( !toProcessEntities.isEmpty() ) {
 			Set<MetaEntity> processedEntities = new HashSet<MetaEntity>();
+			int toProcessCountBeforeLoop = toProcessEntities.size();
 			for ( MetaEntity entity : toProcessEntities ) {
-				if ( containedInEntity( toProcessEntities, entity ) ) {
+				// see METAGEN-36
+				if ( generatedModelClasses.contains( entity.getQualifiedName() ) ) {
+					toProcessEntities.remove( entity );
 					continue;
 				}
+				if ( modelGenerationNeedsToBeDeferred( toProcessEntities, entity ) ) {
+					continue;
+				}
 				context.logMessage(
 						Diagnostic.Kind.OTHER, "Writing meta model for embeddable/mapped superclass" + entity
 				);
@@ -161,10 +171,15 @@
 				processedEntities.add( entity );
 			}
 			toProcessEntities.removeAll( processedEntities );
+			if ( toProcessEntities.size() >= toProcessCountBeforeLoop ) {
+				context.logMessage(
+						Diagnostic.Kind.ERROR, "Potential endless loop in generation of entities."
+				);
+			}
 		}
 	}
 
-	private boolean containedInEntity(Collection<MetaEntity> entities, MetaEntity containedEntity) {
+	private boolean modelGenerationNeedsToBeDeferred(Collection<MetaEntity> entities, MetaEntity containedEntity) {
 		ContainsAttributeTypeVisitor visitor = new ContainsAttributeTypeVisitor(
 				containedEntity.getTypeElement(), context
 		);
@@ -207,7 +222,7 @@
 				continue;
 			}
 
-			String fqn = ( ( TypeElement ) element ).getQualifiedName().toString();
+			String fqn = ( (TypeElement) element ).getQualifiedName().toString();
 			MetaEntity alreadyExistingMetaEntity = tryGettingExistingEntityFromContext( mirror, fqn );
 			if ( alreadyExistingMetaEntity != null && alreadyExistingMetaEntity.isMetaComplete() ) {
 				String msg = "Skipping processing of annotations for " + fqn + " since xml configuration is metadata complete.";
@@ -217,10 +232,10 @@
 
 			AnnotationMetaEntity metaEntity;
 			if ( TypeUtils.containsAnnotation( element, Embeddable.class ) ) {
-				metaEntity = new AnnotationEmbeddable( ( TypeElement ) element, context );
+				metaEntity = new AnnotationEmbeddable( (TypeElement) element, context );
 			}
 			else {
-				metaEntity = new AnnotationMetaEntity( ( TypeElement ) element, context );
+				metaEntity = new AnnotationMetaEntity( (TypeElement) element, context );
 			}
 
 			if ( alreadyExistingMetaEntity != null ) {
@@ -267,7 +282,7 @@
 
 		@Override
 		public Boolean visitDeclared(DeclaredType declaredType, Element element) {
-			TypeElement returnedElement = ( TypeElement ) context.getTypeUtils().asElement( declaredType );
+			TypeElement returnedElement = (TypeElement) context.getTypeUtils().asElement( declaredType );
 
 			String fqNameOfReturnType = returnedElement.getQualifiedName().toString();
 			String collection = Constants.COLLECTIONS.get( fqNameOfReturnType );
@@ -275,7 +290,7 @@
 				TypeMirror collectionElementType = TypeUtils.getCollectionElementType(
 						declaredType, fqNameOfReturnType, null, context
 				);
-				returnedElement = ( TypeElement ) context.getTypeUtils().asElement( collectionElementType );
+				returnedElement = (TypeElement) context.getTypeUtils().asElement( collectionElementType );
 			}
 
 			if ( type.getQualifiedName().toString().equals( returnedElement.getQualifiedName().toString() ) ) {


Property changes on: jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java
___________________________________________________________________
Name: svn:keyword
   + Id

Added: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/embeddablemappedsuperclass/EmbeddableAndMappedSuperClass.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/embeddablemappedsuperclass/EmbeddableAndMappedSuperClass.java	                        (rev 0)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/embeddablemappedsuperclass/EmbeddableAndMappedSuperClass.java	2010-10-04 13:45:28 UTC (rev 20770)
@@ -0,0 +1,33 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// $Id:$
+package org.hibernate.jpamodelgen.test.embeddablemappedsuperclass;
+
+import javax.persistence.Embeddable;
+import javax.persistence.MappedSuperclass;
+
+/**
+ * @author Hardy Ferentschik
+ */
+ at Embeddable
+ at MappedSuperclass
+public class EmbeddableAndMappedSuperClass {
+
+}
+
+


Property changes on: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/embeddablemappedsuperclass/EmbeddableAndMappedSuperClass.java
___________________________________________________________________
Name: svn:keyword
   + Id

Added: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/embeddablemappedsuperclass/EmbeddableMappedSuperClassTest.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/embeddablemappedsuperclass/EmbeddableMappedSuperClassTest.java	                        (rev 0)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/embeddablemappedsuperclass/EmbeddableMappedSuperClassTest.java	2010-10-04 13:45:28 UTC (rev 20770)
@@ -0,0 +1,46 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// $Id: BlobTest.java 20721 2010-09-27 12:40:10Z hardy.ferentschik $
+
+package org.hibernate.jpamodelgen.test.embeddablemappedsuperclass;
+
+import org.testng.annotations.Test;
+
+import org.hibernate.jpamodelgen.test.util.CompilationTest;
+
+import static org.hibernate.jpamodelgen.test.util.TestUtil.assertMetamodelClassGeneratedFor;
+import static org.hibernate.jpamodelgen.test.util.TestUtil.assertNoCompilationError;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class EmbeddableMappedSuperClassTest extends CompilationTest {
+	/**
+	 * METAGEN-36
+	 */
+	@Test
+	public void testMetaModelsGenerated() {
+		assertMetamodelClassGeneratedFor( EmbeddableAndMappedSuperClass.class );
+		assertNoCompilationError(getCompilationDiagnostics());
+	}
+
+	@Override
+	protected String getPackageNameOfTestSources() {
+		return EmbeddableMappedSuperClassTest.class.getPackage().getName();
+	}
+}
\ No newline at end of file


Property changes on: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/embeddablemappedsuperclass/EmbeddableMappedSuperClassTest.java
___________________________________________________________________
Name: svn:keyword
   + Id



More information about the hibernate-commits mailing list