[hibernate-commits] Hibernate SVN: r20785 - in jpamodelgen/trunk/src: main/java/org/hibernate/jpamodelgen/util and 2 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Thu Oct 7 08:21:21 EDT 2010


Author: hardy.ferentschik
Date: 2010-10-07 08:21:20 -0400 (Thu, 07 Oct 2010)
New Revision: 20785

Added:
   jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/usertype/
   jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/usertype/ContactDetails.java
   jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/usertype/PhoneNumber.java
   jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/usertype/UserTypeTest.java
Modified:
   jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/annotation/MetaAttributeGenerationVisitor.java
   jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/util/TypeUtils.java
Log:
METAGEN-28 Added check for @Type to isBasicAttribute(). Made sure @Type is not directly referenced. Added test.

Modified: jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/annotation/MetaAttributeGenerationVisitor.java
===================================================================
--- jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/annotation/MetaAttributeGenerationVisitor.java	2010-10-07 09:36:10 UTC (rev 20784)
+++ jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/annotation/MetaAttributeGenerationVisitor.java	2010-10-07 12:21:20 UTC (rev 20785)
@@ -59,6 +59,12 @@
 	 */
 	private static final String ORG_HIBERNATE_ANNOTATIONS_TARGET = "org.hibernate.annotations.Target";
 
+	/**
+	 * FQCN of the Hibernate specific @Type annotation. We do not use the class directly to avoid depending on Hibernate
+	 * Core.
+	 */
+	private static final String ORG_HIBERNATE_ANNOTATIONS_TYPE = "org.hibernate.annotations.Type";
+
 	private final AnnotationMetaEntity entity;
 	private final Context context;
 
@@ -162,7 +168,7 @@
 	@Override
 	public AnnotationMetaAttribute visitExecutable(ExecutableType t, Element p) {
 		if ( !p.getKind().equals( ElementKind.METHOD ) ) {
-			return null;
+			return null;                                                                                       
 		}
 
 		String string = p.getSimpleName().toString();
@@ -181,6 +187,11 @@
 			return true;
 		}
 
+		// METAGEN-28
+		if ( TypeUtils.getAnnotationMirror( element, ORG_HIBERNATE_ANNOTATIONS_TYPE ) != null ) {
+			return true;
+		}
+
 		BasicAttributeVisitor basicVisitor = new BasicAttributeVisitor( context );
 		return returnedElement.asType().accept( basicVisitor, returnedElement );
 	}

Modified: jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/util/TypeUtils.java
===================================================================
--- jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/util/TypeUtils.java	2010-10-07 09:36:10 UTC (rev 20784)
+++ jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/util/TypeUtils.java	2010-10-07 12:21:20 UTC (rev 20785)
@@ -170,21 +170,21 @@
 	}
 
 	/**
-	 * Returns the annotation mirror for the specified annotation class from the {@code Element}.
+	 * Checks whether the {@code Element} hosts the annotation with the given fully qualified class name.
 	 *
 	 * @param element the element to check for the hosted annotation
-	 * @param clazz the annotation class to check for
+	 * @param fqcn the fully qualified class name of the annotation to check for
 	 *
 	 * @return the annotation mirror for the specified annotation class from the {@code Element} or {@code null} in case
 	 *         the {@code TypeElement} does not host the specified annotation.
 	 */
-	public static AnnotationMirror getAnnotationMirror(Element element, Class<? extends Annotation> clazz) {
+	public static AnnotationMirror getAnnotationMirror(Element element, String fqcn) {
 		assert element != null;
-		assert clazz != null;
+		assert fqcn != null;
 
 		AnnotationMirror mirror = null;
 		for ( AnnotationMirror am : element.getAnnotationMirrors() ) {
-			if ( isAnnotationMirrorOfType( am, clazz ) ) {
+			if ( isAnnotationMirrorOfType( am, fqcn ) ) {
 				mirror = am;
 				break;
 			}
@@ -192,6 +192,20 @@
 		return mirror;
 	}
 
+	/**
+	 * Checks whether the {@code Element} hosts an annotation of the specified class.
+	 *
+	 * @param element the element to check for the hosted annotation
+	 * @param clazz the annotation class to check for
+	 *
+	 * @return the annotation mirror for the specified annotation class from the {@code Element} or {@code null} in case
+	 *         the {@code TypeElement} does not host the specified annotation.
+	 */
+	public static AnnotationMirror getAnnotationMirror(Element element, Class<? extends Annotation> clazz) {
+		assert clazz != null;
+		return getAnnotationMirror( element, clazz.getName() );
+	}
+
 	public static Object getAnnotationValue(AnnotationMirror annotationMirror, String parameterValue) {
 		assert annotationMirror != null;
 		assert parameterValue != null;

Added: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/usertype/ContactDetails.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/usertype/ContactDetails.java	                        (rev 0)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/usertype/ContactDetails.java	2010-10-07 12:21:20 UTC (rev 20785)
@@ -0,0 +1,38 @@
+/*
+ * 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.usertype;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+import org.hibernate.annotations.Type;
+
+/**
+ * @author Hardy Ferentschik
+ */
+ at Entity
+public class ContactDetails {
+	@Id
+	private long id;
+
+	@Type(type = "foo")
+	private PhoneNumber phoneNumber;
+}
+
+


Property changes on: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/usertype/ContactDetails.java
___________________________________________________________________
Name: svn:keywords
   + Id

Added: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/usertype/PhoneNumber.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/usertype/PhoneNumber.java	                        (rev 0)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/usertype/PhoneNumber.java	2010-10-07 12:21:20 UTC (rev 20785)
@@ -0,0 +1,27 @@
+/*
+ * 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.usertype;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class PhoneNumber {
+}
+
+


Property changes on: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/usertype/PhoneNumber.java
___________________________________________________________________
Name: svn:keywords
   + Id

Copied: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/usertype/UserTypeTest.java (from rev 20783, jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/TypesMappedSuperclassTest.java)
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/usertype/UserTypeTest.java	                        (rev 0)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/usertype/UserTypeTest.java	2010-10-07 12:21:20 UTC (rev 20785)
@@ -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$
+
+package org.hibernate.jpamodelgen.test.usertype;
+
+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.assertPresenceOfFieldInMetamodelFor;
+
+/**
+ * @author Hardy Ferentschik
+ * @see METAGEN-28
+ */
+public class UserTypeTest extends CompilationTest {
+	@Test
+	public void testCustomUserTypeInMetaModel() {
+		assertMetamodelClassGeneratedFor( ContactDetails.class );
+		assertPresenceOfFieldInMetamodelFor(
+				ContactDetails.class, "phoneNumber", "@Type annotated filed should be in metamodel"
+		);
+	}
+
+	@Override
+	protected String getPackageNameOfCurrentTest() {
+		return UserTypeTest.class.getPackage().getName();
+	}
+}
\ No newline at end of file



More information about the hibernate-commits mailing list