From 9e9b7541b5ebef3c35c40c5ede192022405c41c5 Mon Sep 17 00:00:00 2001 From: Hardy Ferentschik Date: Mon, 16 Jan 2012 16:52:55 +0100 Subject: [PATCH] METAGEN-64 Making sure that non mapped classes in the hierarchy don't destroy the mapped class hierarchy --- .../hibernate/jpamodelgen/ClassWriter.java | 18 +++++-- .../TypesMappedSuperclassTest.java | 3 +- .../unmappedclassinhierarchy/BaseEntity.java | 47 +++++++++++++++++++ .../unmappedclassinhierarchy/MappedBase.java | 38 +++++++++++++++ .../NormalExtendsEntity.java | 23 +++++++++ .../NormalExtendsMapped.java | 23 +++++++++ .../test/unmappedclassinhierarchy/SubA.java | 31 ++++++++++++ .../test/unmappedclassinhierarchy/SubB.java | 31 ++++++++++++ .../UnmappedClassInHierarchyTest.java | 40 ++++++++++++++++ .../test/usertype/ContactDetails.java | 2 - .../test/usertype/UserTypeTest.java | 3 +- 11 files changed, 251 insertions(+), 8 deletions(-) create mode 100644 tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/unmappedclassinhierarchy/BaseEntity.java create mode 100644 tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/unmappedclassinhierarchy/MappedBase.java create mode 100644 tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/unmappedclassinhierarchy/NormalExtendsEntity.java create mode 100644 tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/unmappedclassinhierarchy/NormalExtendsMapped.java create mode 100644 tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/unmappedclassinhierarchy/SubA.java create mode 100644 tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/unmappedclassinhierarchy/SubB.java create mode 100644 tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/unmappedclassinhierarchy/UnmappedClassInHierarchyTest.java diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/ClassWriter.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/ClassWriter.java index 70c5e1485e..bdda9e51ee 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/ClassWriter.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/ClassWriter.java @@ -122,17 +122,27 @@ public final class ClassWriter { private static void printClassDeclaration(MetaEntity entity, PrintWriter pw, Context context) { pw.print( "public abstract class " + entity.getSimpleName() + META_MODEL_CLASS_NAME_SUFFIX ); - final TypeMirror superClass = entity.getTypeElement().getSuperclass(); + String superClassName = findMappedSuperClass(entity, context); + if(superClassName != null) { + pw.print( " extends " + superClassName + META_MODEL_CLASS_NAME_SUFFIX ); + } + + pw.println( " {" ); + } + + private static String findMappedSuperClass(MetaEntity entity, Context context) { + TypeMirror superClass = entity.getTypeElement().getSuperclass(); //superclass of Object is of NoType which returns some other kind - if ( superClass.getKind() == TypeKind.DECLARED ) { + while ( superClass.getKind() == TypeKind.DECLARED ) { //F..king Ch...t Have those people used their horrible APIs even once? final Element superClassElement = ( (DeclaredType) superClass ).asElement(); String superClassName = ( (TypeElement) superClassElement ).getQualifiedName().toString(); if ( extendsSuperMetaModel( superClassElement, entity.isMetaComplete(), context ) ) { - pw.print( " extends " + superClassName + META_MODEL_CLASS_NAME_SUFFIX ); + return superClassName; } + superClass = ( (TypeElement) superClassElement ).getSuperclass(); } - pw.println( " {" ); + return null; } /** diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/TypesMappedSuperclassTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/TypesMappedSuperclassTest.java index 2d95e86b79..d095332a92 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/TypesMappedSuperclassTest.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/TypesMappedSuperclassTest.java @@ -19,13 +19,14 @@ package org.hibernate.jpamodelgen.test.typedmappedsuperclass; import org.testng.annotations.Test; import org.hibernate.jpamodelgen.test.util.CompilationTest; +import org.hibernate.jpamodelgen.test.util.TestForIssue; import static org.hibernate.jpamodelgen.test.util.TestUtil.assertMetamodelClassGeneratedFor; /** * @author Hardy Ferentschik - * @see METAGEN-37 */ +@TestForIssue(jiraKey = "METAGEN-37") public class TypesMappedSuperclassTest extends CompilationTest { @Test public void testExtractClosestRealType() { diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/unmappedclassinhierarchy/BaseEntity.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/unmappedclassinhierarchy/BaseEntity.java new file mode 100644 index 0000000000..3d58e9770a --- /dev/null +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/unmappedclassinhierarchy/BaseEntity.java @@ -0,0 +1,47 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2012, 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. + */ +package org.hibernate.jpamodelgen.test.unmappedclassinhierarchy; + +import javax.persistence.Access; +import javax.persistence.AccessType; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.SequenceGenerator; + +@Entity +@Access(AccessType.FIELD) +public class BaseEntity { + @Id + @SequenceGenerator(name = "test1_id_gen", sequenceName = "test1_seq") + @GeneratedValue(generator = "test1_id_gen", strategy = GenerationType.SEQUENCE) + protected Integer id; + + protected String name; + + public BaseEntity() { + } + + public Integer getId() { + return id; + } + + public String getName() { + return name; + } +} diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/unmappedclassinhierarchy/MappedBase.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/unmappedclassinhierarchy/MappedBase.java new file mode 100644 index 0000000000..2d9a4d1732 --- /dev/null +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/unmappedclassinhierarchy/MappedBase.java @@ -0,0 +1,38 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2012, 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. + */ +package org.hibernate.jpamodelgen.test.unmappedclassinhierarchy; + +import java.util.Date; +import javax.persistence.Access; +import javax.persistence.AccessType; +import javax.persistence.MappedSuperclass; + +@MappedSuperclass +@Access(AccessType.FIELD) +public abstract class MappedBase { + private Date creationDate; + private Date updatedOn; + + protected MappedBase(final Date date) { + this.creationDate = date; + this.updatedOn = date; + } + + protected MappedBase() { + this( new Date() ); + } +} diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/unmappedclassinhierarchy/NormalExtendsEntity.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/unmappedclassinhierarchy/NormalExtendsEntity.java new file mode 100644 index 0000000000..ded8842783 --- /dev/null +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/unmappedclassinhierarchy/NormalExtendsEntity.java @@ -0,0 +1,23 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2012, 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. + */ +package org.hibernate.jpamodelgen.test.unmappedclassinhierarchy; + +public class NormalExtendsEntity extends BaseEntity { + public String doSomething(String s) { + return ( s != null ? s : "empty" ); + } +} diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/unmappedclassinhierarchy/NormalExtendsMapped.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/unmappedclassinhierarchy/NormalExtendsMapped.java new file mode 100644 index 0000000000..2a05beb20f --- /dev/null +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/unmappedclassinhierarchy/NormalExtendsMapped.java @@ -0,0 +1,23 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2012, 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. + */ +package org.hibernate.jpamodelgen.test.unmappedclassinhierarchy; + +public class NormalExtendsMapped extends MappedBase { + public String doSomething(String s) { + return ( s != null ? s : "empty" ); + } +} diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/unmappedclassinhierarchy/SubA.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/unmappedclassinhierarchy/SubA.java new file mode 100644 index 0000000000..a75d9fa300 --- /dev/null +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/unmappedclassinhierarchy/SubA.java @@ -0,0 +1,31 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2012, 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. + */ +package org.hibernate.jpamodelgen.test.unmappedclassinhierarchy; + +import javax.persistence.Access; +import javax.persistence.AccessType; +import javax.persistence.Entity; + +@Entity +@Access(AccessType.FIELD) +public class SubA extends NormalExtendsEntity { + protected String street; + + public String getStreet() { + return street; + } +} diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/unmappedclassinhierarchy/SubB.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/unmappedclassinhierarchy/SubB.java new file mode 100644 index 0000000000..ed7e2bb08b --- /dev/null +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/unmappedclassinhierarchy/SubB.java @@ -0,0 +1,31 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2012, 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. + */ +package org.hibernate.jpamodelgen.test.unmappedclassinhierarchy; + +import javax.persistence.Access; +import javax.persistence.AccessType; +import javax.persistence.Entity; + +@Entity +@Access(AccessType.FIELD) +public class SubB extends NormalExtendsMapped { + protected String street; + + public String getStreet() { + return street; + } +} diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/unmappedclassinhierarchy/UnmappedClassInHierarchyTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/unmappedclassinhierarchy/UnmappedClassInHierarchyTest.java new file mode 100644 index 0000000000..6d4755036e --- /dev/null +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/unmappedclassinhierarchy/UnmappedClassInHierarchyTest.java @@ -0,0 +1,40 @@ +/* + * 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. + */ +package org.hibernate.jpamodelgen.test.unmappedclassinhierarchy; + +import org.testng.annotations.Test; + +import org.hibernate.jpamodelgen.test.util.CompilationTest; + +import static org.hibernate.jpamodelgen.test.util.TestUtil.assertSuperClassRelationShipInMetamodel; + +/** + * @author Emmanuel Bernard + * @author Hardy Ferentschik + */ +public class UnmappedClassInHierarchyTest extends CompilationTest { + @Test + public void testUnmappedClassInHierarchy() throws Exception { + assertSuperClassRelationShipInMetamodel( SubA.class, BaseEntity.class ); + assertSuperClassRelationShipInMetamodel( SubB.class, MappedBase.class ); + } + + @Override + protected String getPackageNameOfCurrentTest() { + return UnmappedClassInHierarchyTest.class.getPackage().getName(); + } +} diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/usertype/ContactDetails.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/usertype/ContactDetails.java index fd9d9a520c..9a5020bf36 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/usertype/ContactDetails.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/usertype/ContactDetails.java @@ -14,8 +14,6 @@ * 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; diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/usertype/UserTypeTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/usertype/UserTypeTest.java index ee1e9e2595..be39346cf9 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/usertype/UserTypeTest.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/usertype/UserTypeTest.java @@ -19,14 +19,15 @@ package org.hibernate.jpamodelgen.test.usertype; import org.testng.annotations.Test; import org.hibernate.jpamodelgen.test.util.CompilationTest; +import org.hibernate.jpamodelgen.test.util.TestForIssue; import static org.hibernate.jpamodelgen.test.util.TestUtil.assertMetamodelClassGeneratedFor; import static org.hibernate.jpamodelgen.test.util.TestUtil.assertPresenceOfFieldInMetamodelFor; /** * @author Hardy Ferentschik - * @see METAGEN-28 */ +@TestForIssue(jiraKey = "METAGEN-28") public class UserTypeTest extends CompilationTest { @Test public void testCustomUserTypeInMetaModel() {