[hibernate-commits] Hibernate SVN: r18753 - in jpamodelgen/trunk/src: main/java/org/hibernate/jpamodelgen/annotation and 4 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Tue Feb 9 16:29:35 EST 2010


Author: hardy.ferentschik
Date: 2010-02-09 16:29:34 -0500 (Tue, 09 Feb 2010)
New Revision: 18753

Added:
   jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/mixedmode/RentalCar.java
   jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/mixedmode/RentalCompany.java
   jpamodelgen/trunk/src/test/resources/org/hibernate/jpamodelgen/test/mixedmode/rentalcar.xml
Removed:
   jpamodelgen/trunk/src/test/java/test/
Modified:
   jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java
   jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMetaEntity.java
   jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/xml/XmlMetaEntity.java
   jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/mixedmode/MixedConfigurationTest.java
Log:
METAGEN-9 First completed cut. Needs more testing

Modified: jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java
===================================================================
--- jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java	2010-02-09 20:23:31 UTC (rev 18752)
+++ jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java	2010-02-09 21:29:34 UTC (rev 18753)
@@ -17,6 +17,7 @@
 */
 package org.hibernate.jpamodelgen;
 
+import java.util.Collection;
 import java.util.List;
 import java.util.Set;
 import javax.annotation.processing.AbstractProcessor;
@@ -104,8 +105,10 @@
 
 		Set<? extends Element> elements = roundEnvironment.getRootElements();
 		for ( Element element : elements ) {
-			context.logMessage( Diagnostic.Kind.OTHER, "Processing " + element.toString() );
-			handleRootElementAnnotationMirrors( element );
+			if ( TypeUtils.containsAnnotation( element, Entity.class, MappedSuperclass.class, Embeddable.class ) ) {
+				context.logMessage( Diagnostic.Kind.OTHER, "Processing " + element.toString() );
+				handleRootElementAnnotationMirrors( element );
+			}
 		}
 
 		return ALLOW_OTHER_PROCESSORS_TO_CLAIM_ANNOTATIONS;
@@ -123,7 +126,7 @@
 		}
 	}
 
-	private boolean hostJPAAnnotations(Set<? extends TypeElement> annotations) {
+	private boolean hostJPAAnnotations(Collection<? extends TypeElement> annotations) {
 		for ( TypeElement type : annotations ) {
 			if ( TypeUtils.isTypeElementOfType( type, Entity.class ) ) {
 				return true;
@@ -151,6 +154,9 @@
 				}
 
 				AnnotationMetaEntity metaEntity = new AnnotationMetaEntity( ( TypeElement ) element, context );
+				if ( alreadyExistingMetaEntity != null ) {
+					metaEntity.mergeInMembers( alreadyExistingMetaEntity.getMembers() );
+				}
 				addMetaEntityToContext( mirror, metaEntity );
 			}
 		}

Modified: jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMetaEntity.java
===================================================================
--- jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMetaEntity.java	2010-02-09 20:23:31 UTC (rev 18752)
+++ jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMetaEntity.java	2010-02-09 21:29:34 UTC (rev 18753)
@@ -18,6 +18,7 @@
 package org.hibernate.jpamodelgen.annotation;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -38,6 +39,7 @@
 import javax.lang.model.util.Elements;
 import javax.lang.model.util.SimpleTypeVisitor6;
 import javax.persistence.AccessType;
+import javax.persistence.Basic;
 import javax.persistence.ElementCollection;
 import javax.persistence.ManyToMany;
 import javax.persistence.ManyToOne;
@@ -73,10 +75,38 @@
 		COLLECTIONS.put( "java.util.Map", "javax.persistence.metamodel.MapAttribute" );
 	}
 
+	static List<String> BASIC_TYPES = new ArrayList<String>();
+
+	static {
+		BASIC_TYPES.add( "java.lang.String" );
+		BASIC_TYPES.add( "java.lang.Boolean" );
+		BASIC_TYPES.add( "java.lang.Byte" );
+		BASIC_TYPES.add( "java.lang.Character" );
+		BASIC_TYPES.add( "java.lang.Short" );
+		BASIC_TYPES.add( "java.lang.Integer" );
+		BASIC_TYPES.add( "java.lang.Long" );
+		BASIC_TYPES.add( "java.lang.Float" );
+		BASIC_TYPES.add( "java.lang.Double" );
+		BASIC_TYPES.add( "java.math.BigInteger" );
+		BASIC_TYPES.add( "java.math.BigDecimal" );
+		BASIC_TYPES.add( "java.util.Date" );
+		BASIC_TYPES.add( "java.util.Calendar" );
+		BASIC_TYPES.add( "java.sql.Date" );
+		BASIC_TYPES.add( "java.sql.Time" );
+		BASIC_TYPES.add( "java.sql.Timestamp" );
+	}
+
+	static List<String> BASIC_ARRAY_TYPES = new ArrayList<String>();
+
+	static {
+		BASIC_ARRAY_TYPES.add( "java.lang.Character" );
+		BASIC_ARRAY_TYPES.add( "java.lang.Byte" );
+	}
+
 	private final TypeElement element;
 	private final ImportContext importContext;
 	private Context context;
-	private List<MetaAttribute> members;
+	private Map<String, MetaAttribute> members;
 	private AccessTypeInformation entityAccessTypeInfo;
 
 	public AnnotationMetaEntity(TypeElement element, Context context) {
@@ -104,7 +134,7 @@
 	}
 
 	public List<MetaAttribute> getMembers() {
-		return members;
+		return new ArrayList<MetaAttribute>( members.values() );
 	}
 
 	@Override
@@ -112,6 +142,12 @@
 		return false;
 	}
 
+	public void mergeInMembers(Collection<MetaAttribute> attributes) {
+		for ( MetaAttribute attribute : attributes ) {
+			members.put( attribute.getPropertyName(), attribute );
+		}
+	}
+
 	private void addPersistentMembers(List<? extends Element> membersOfClass, AccessType membersKind) {
 		for ( Element memberOfClass : membersOfClass ) {
 			AccessType forcedAccessType = TypeUtils.determineAnnotationSpecifiedAccessType( memberOfClass );
@@ -128,7 +164,7 @@
 			TypeVisitor visitor = new TypeVisitor( this );
 			AnnotationMetaAttribute result = memberOfClass.asType().accept( visitor, memberOfClass );
 			if ( result != null ) {
-				members.add( result );
+				members.put( result.getPropertyName(), result );
 			}
 		}
 	}
@@ -144,7 +180,7 @@
 	}
 
 	private void init() {
-		members = new ArrayList<MetaAttribute>();
+		members = new HashMap<String, MetaAttribute>();
 		TypeUtils.determineAccessTypeForHierarchy( element, context );
 		entityAccessTypeInfo = context.getAccessTypeInfo( getQualifiedName() );
 
@@ -183,17 +219,29 @@
 		}
 
 		@Override
-		protected AnnotationMetaAttribute defaultAction(TypeMirror e, Element p) {
-			return super.defaultAction( e, p );
-		}
-
-		@Override
 		public AnnotationMetaAttribute visitPrimitive(PrimitiveType t, Element element) {
 			return new AnnotationMetaSingleAttribute( parent, element, TypeUtils.toTypeString( t ) );
 		}
 
 		@Override
 		public AnnotationMetaAttribute visitArray(ArrayType t, Element element) {
+			// METAGEN-2 - For now we handle arrays as SingularAttribute
+			// The code below is an attempt to be closer to the spec and only allow byte[], Byte[], char[] and Character[]
+//			AnnotationMetaSingleAttribute attribute = null;
+//			TypeMirror componentMirror = t.getComponentType();
+//			if ( TypeKind.CHAR.equals( componentMirror.getKind() )
+//					|| TypeKind.BYTE.equals( componentMirror.getKind() ) ) {
+//				attribute = new AnnotationMetaSingleAttribute( parent, element, TypeUtils.toTypeString( t ) );
+//			}
+//			else if ( TypeKind.DECLARED.equals( componentMirror.getKind() ) ) {
+//				TypeElement componentElement = ( TypeElement ) context.getProcessingEnvironment()
+//						.getTypeUtils()
+//						.asElement( componentMirror );
+//				if ( BASIC_ARRAY_TYPES.contains( componentElement.getQualifiedName().toString() ) ) {
+//					attribute = new AnnotationMetaSingleAttribute( parent, element, TypeUtils.toTypeString( t ) );
+//				}
+//			}
+//			return attribute;
 			return new AnnotationMetaSingleAttribute( parent, element, TypeUtils.toTypeString( t ) );
 		}
 
@@ -245,9 +293,14 @@
 				}
 			}
 			else {
-				return new AnnotationMetaSingleAttribute(
-						parent, element, returnedElement.getQualifiedName().toString()
-				);
+				if ( isBasicAttribute( element, returnedElement ) ) {
+					return new AnnotationMetaSingleAttribute(
+							parent, element, returnedElement.getQualifiedName().toString()
+					);
+				}
+				else {
+					return null;
+				}
 			}
 		}
 
@@ -258,15 +311,25 @@
 			}
 
 			String string = p.getSimpleName().toString();
-			if ( StringUtil.isPropertyName( string ) ) {
-				TypeMirror returnType = t.getReturnType();
-				return returnType.accept( this, p );
-			}
-			else {
+			if ( !StringUtil.isPropertyName( string ) ) {
 				return null;
 			}
+
+			TypeMirror returnType = t.getReturnType();
+			return returnType.accept( this, p );
 		}
 
+		private boolean isBasicAttribute(Element element, Element returnedElement) {
+			if ( TypeUtils.containsAnnotation( element, Basic.class )
+					|| TypeUtils.containsAnnotation( element, OneToOne.class )
+					|| TypeUtils.containsAnnotation( element, ManyToOne.class ) ) {
+				return true;
+			}
+
+			BasicAttributeVisitor basicVisitor = new BasicAttributeVisitor();
+			return returnedElement.asType().accept( basicVisitor, returnedElement );
+		}
+
 		private AnnotationMetaAttribute createAnnotationMetaAttributeForMap(DeclaredType declaredType, Element element, String collection, String targetEntity) {
 			String keyType;
 			if ( TypeUtils.containsAnnotation( element, MapKeyClass.class ) ) {
@@ -336,7 +399,11 @@
 		}
 
 		private String getKeyType(DeclaredType t) {
-			return TypeUtils.extractClosestRealTypeAsString( t.getTypeArguments().get( 0 ), context );
+			List<? extends TypeMirror> typeArguments = t.getTypeArguments();
+			if ( typeArguments.size() == 0 ) {
+				context.logMessage( Diagnostic.Kind.ERROR, "Entity: " + getQualifiedName() );
+			}
+			return TypeUtils.extractClosestRealTypeAsString( typeArguments.get( 0 ), context );
 		}
 
 		/**
@@ -376,4 +443,49 @@
 			return targetEntityName;
 		}
 	}
+
+	/**
+	 * Checks whether the visited type is a basic attibute according to the JPA 2 spec
+	 * ( secction 2.8 Mapping Defaults for Non-Relationship Fields or Properties)
+	 */
+	class BasicAttributeVisitor extends SimpleTypeVisitor6<Boolean, Element> {
+		@Override
+		public Boolean visitPrimitive(PrimitiveType t, Element element) {
+			return Boolean.TRUE;
+		}
+
+		@Override
+		public Boolean visitArray(ArrayType t, Element element) {
+			TypeMirror componentMirror = t.getComponentType();
+			TypeElement componentElement = ( TypeElement ) context.getProcessingEnvironment()
+					.getTypeUtils()
+					.asElement( componentMirror );
+
+			return BASIC_ARRAY_TYPES.contains( componentElement.getQualifiedName().toString() );
+		}
+
+		@Override
+		public Boolean visitDeclared(DeclaredType declaredType, Element element) {
+			if ( ElementKind.ENUM.equals( element.getKind() ) ) {
+				return Boolean.TRUE;
+			}
+
+			if ( ElementKind.CLASS.equals( element.getKind() ) ) {
+				TypeElement typeElement = ( ( TypeElement ) element );
+				String typeName = typeElement.getQualifiedName().toString();
+				if ( BASIC_TYPES.contains( typeName ) ) {
+					return Boolean.TRUE;
+				}
+				for ( TypeMirror mirror : typeElement.getInterfaces() ) {
+					TypeElement interfaceElement = ( TypeElement ) context.getProcessingEnvironment()
+							.getTypeUtils()
+							.asElement( mirror );
+					if ( "java.io.Serializable".equals( interfaceElement.getQualifiedName().toString() ) ) {
+						return Boolean.TRUE;
+					}
+				}
+			}
+			return Boolean.FALSE;
+		}
+	}
 }

Modified: jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/xml/XmlMetaEntity.java
===================================================================
--- jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/xml/XmlMetaEntity.java	2010-02-09 20:23:31 UTC (rev 18752)
+++ jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/xml/XmlMetaEntity.java	2010-02-09 21:29:34 UTC (rev 18753)
@@ -18,6 +18,7 @@
 package org.hibernate.jpamodelgen.xml;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -162,7 +163,7 @@
 	public TypeElement getTypeElement() {
 		return element;
 	}
-
+	
 	@Override
 	public boolean isMetaComplete() {
 		return isMetaComplete;

Modified: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/mixedmode/MixedConfigurationTest.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/mixedmode/MixedConfigurationTest.java	2010-02-09 20:23:31 UTC (rev 18752)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/mixedmode/MixedConfigurationTest.java	2010-02-09 21:29:34 UTC (rev 18753)
@@ -46,10 +46,19 @@
 		assertMetamodelClassGeneratedFor( Vehicle.class );
 		assertMetamodelClassGeneratedFor( Truck.class );
 		assertPresenceOfFieldInMetamodelFor(
-				Truck.class, "horsePower", "Property horsePower has explicit access type and should be in metamodel"
+				Truck.class, "horsePower", "Property 'horsePower' has explicit access type and should be in metamodel"
 		);
 	}
 
+	@Test
+	public void testMixedConfiguration() {
+		assertMetamodelClassGeneratedFor( RentalCar.class );
+		assertMetamodelClassGeneratedFor( RentalCompany.class );
+		assertPresenceOfFieldInMetamodelFor(
+				RentalCar.class, "company", "Property 'company' should be included due to xml configuration"
+		);
+	}
+
 	@Override
 	protected String getPackageNameOfTestSources() {
 		return MixedConfigurationTest.class.getPackage().getName();
@@ -58,8 +67,10 @@
 	@Override
 	protected Collection<String> getOrmFiles() {
 		List<String> ormFiles = new ArrayList<String>();
-		ormFiles.add( TestUtil.fcnToPath( MixedConfigurationTest.class.getPackage().getName() ) + "/car.xml" );
-		ormFiles.add( TestUtil.fcnToPath( MixedConfigurationTest.class.getPackage().getName() ) + "/truck.xml" );
+		String dir = TestUtil.fcnToPath( MixedConfigurationTest.class.getPackage().getName() );
+		ormFiles.add( dir + "/car.xml" );
+		ormFiles.add( dir + "/rentalcar.xml" );
+		ormFiles.add( dir + "/truck.xml" );
 		return ormFiles;
 	}
 }
\ No newline at end of file

Copied: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/mixedmode/RentalCar.java (from rev 18734, jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/mixedmode/Car.java)
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/mixedmode/RentalCar.java	                        (rev 0)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/mixedmode/RentalCar.java	2010-02-09 21:29:34 UTC (rev 18753)
@@ -0,0 +1,61 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat, Inc. and/or its affiliates, 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.
+*/
+package org.hibernate.jpamodelgen.test.mixedmode;
+
+import javax.persistence.Entity;
+
+
+/**
+ * @author Hardy Ferentschik
+ */
+ at Entity
+public class RentalCar extends Car {
+	private RentalCompany company;
+
+	private CURRENTLY_HIRED hired;
+
+	private Character[] chassisNumber;
+
+	public RentalCompany getCompany() {
+		return company;
+	}
+
+	public void setCompany(RentalCompany company) {
+		this.company = company;
+	}
+
+	public Character[] getChassisNumber() {
+		return chassisNumber;
+	}
+
+	public void setChassisNumber(Character[] chassisNumber) {
+		this.chassisNumber = chassisNumber;
+	}
+
+	public CURRENTLY_HIRED getHired() {
+		return hired;
+	}
+
+	public void setHired(CURRENTLY_HIRED hired) {
+		this.hired = hired;
+	}
+
+	enum CURRENTLY_HIRED {
+		YES, NO
+	}
+}
\ No newline at end of file

Added: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/mixedmode/RentalCompany.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/mixedmode/RentalCompany.java	                        (rev 0)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/mixedmode/RentalCompany.java	2010-02-09 21:29:34 UTC (rev 18753)
@@ -0,0 +1,50 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat, Inc. and/or its affiliates, 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.
+*/
+package org.hibernate.jpamodelgen.test.mixedmode;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+
+/**
+ * @author Hardy Ferentschik
+ */
+ at Entity
+public class RentalCompany {
+	@Id
+	@GeneratedValue
+	private long id;
+
+	private String name;
+
+	public long getId() {
+		return id;
+	}
+
+	public void setId(long id) {
+		this.id = id;
+	}
+
+	public String getName() {
+		return name;
+	}
+
+	public void setName(String name) {
+		this.name = name;
+	}
+}
\ No newline at end of file

Copied: jpamodelgen/trunk/src/test/resources/org/hibernate/jpamodelgen/test/mixedmode/rentalcar.xml (from rev 18734, jpamodelgen/trunk/src/test/resources/org/hibernate/jpamodelgen/test/mixedmode/car.xml)
===================================================================
--- jpamodelgen/trunk/src/test/resources/org/hibernate/jpamodelgen/test/mixedmode/rentalcar.xml	                        (rev 0)
+++ jpamodelgen/trunk/src/test/resources/org/hibernate/jpamodelgen/test/mixedmode/rentalcar.xml	2010-02-09 21:29:34 UTC (rev 18753)
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
+                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+                 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd"
+                 version="2.0"
+        >
+    <!-- foo.bar should get ignored since class is fully qualified -->
+    <package>foo.bar</package>
+    <entity class="org.hibernate.jpamodelgen.test.mixedmode.RentalCar">
+        <attributes>
+            <many-to-one name="company" access="FIELD"/>
+        </attributes>
+    </entity>
+</entity-mappings>
+


Property changes on: jpamodelgen/trunk/src/test/resources/org/hibernate/jpamodelgen/test/mixedmode/rentalcar.xml
___________________________________________________________________
Name: svn:keywords
   + Id



More information about the hibernate-commits mailing list