METAGEN-64 Making sure that non mapped classes in the hierarchy don't destroy the mapped class hierarchy

This commit is contained in:
Hardy Ferentschik 2012-01-16 16:52:55 +01:00 committed by Strong Liu
parent 1cfdb8a67d
commit 9e9b7541b5
11 changed files with 251 additions and 8 deletions

View File

@ -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;
}
/**

View File

@ -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() {

View File

@ -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;
}
}

View File

@ -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() );
}
}

View File

@ -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" );
}
}

View File

@ -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" );
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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();
}
}

View File

@ -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;

View File

@ -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() {