Author: epbernard
Date: 2009-07-08 09:18:09 -0400 (Wed, 08 Jul 2009)
New Revision: 17047
Added:
jpamodelgen/trunk/test/src/main/java/model/Customer.java
jpamodelgen/trunk/test/src/main/java/model/User.java
jpamodelgen/trunk/test/src/test/java/AccessTypeTest.java
jpamodelgen/trunk/test/src/test/java/InheritanceTest.java
Modified:
jpamodelgen/trunk/generator/src/main/java/org/hibernate/jpa/metamodel/ap/IMetaEntity.java
jpamodelgen/trunk/generator/src/main/java/org/hibernate/jpa/metamodel/ap/JPAMetaModelEntityProcessor.java
jpamodelgen/trunk/generator/src/main/java/org/hibernate/jpa/metamodel/ap/annotation/MetaEntity.java
jpamodelgen/trunk/generator/src/main/java/org/hibernate/jpa/metamodel/ap/xml/XmlMetaEntity.java
Log:
Support for entity inheritance
Modified:
jpamodelgen/trunk/generator/src/main/java/org/hibernate/jpa/metamodel/ap/IMetaEntity.java
===================================================================
---
jpamodelgen/trunk/generator/src/main/java/org/hibernate/jpa/metamodel/ap/IMetaEntity.java 2009-07-08
13:07:29 UTC (rev 17046)
+++
jpamodelgen/trunk/generator/src/main/java/org/hibernate/jpa/metamodel/ap/IMetaEntity.java 2009-07-08
13:18:09 UTC (rev 17047)
@@ -18,6 +18,7 @@
package org.hibernate.jpa.metamodel.ap;
import javax.lang.model.element.Name;
+import javax.lang.model.element.TypeElement;
import java.util.List;
/**
@@ -39,4 +40,6 @@
String staticImport(String fqcn, String member);
String importType(Name qualifiedName);
+
+ TypeElement getTypeElement();
}
Modified:
jpamodelgen/trunk/generator/src/main/java/org/hibernate/jpa/metamodel/ap/JPAMetaModelEntityProcessor.java
===================================================================
---
jpamodelgen/trunk/generator/src/main/java/org/hibernate/jpa/metamodel/ap/JPAMetaModelEntityProcessor.java 2009-07-08
13:07:29 UTC (rev 17046)
+++
jpamodelgen/trunk/generator/src/main/java/org/hibernate/jpa/metamodel/ap/JPAMetaModelEntityProcessor.java 2009-07-08
13:18:09 UTC (rev 17047)
@@ -24,6 +24,9 @@
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
+import javax.lang.model.type.TypeMirror;
+import javax.lang.model.type.TypeKind;
+import javax.lang.model.type.DeclaredType;
import javax.persistence.Embeddable;
import javax.persistence.MappedSuperclass;
import javax.tools.Diagnostic;
@@ -175,7 +178,10 @@
final String annotationType = mirror.getAnnotationType().toString();
if ( element.getKind() == ElementKind.CLASS &&
- annotationType.equals( ENTITY_ANN ) ) {
+ ( annotationType.equals( ENTITY_ANN )
+ || annotationType.equals( MAPPED_SUPERCLASS_ANN )
+ || annotationType.equals( EMBEDDABLE_ANN )
+ ) ) {
MetaEntity metaEntity = new MetaEntity( processingEnv, ( TypeElement ) element );
// TODO instead of just adding the entity we have to do some merging.
@@ -243,8 +249,10 @@
pw.println( "@" + entity.importType(
"javax.persistence.metamodel.StaticMetamodel" ) + "(" +
entity.getSimpleName() + ".class)" );
- pw.println( "public abstract class " + entity.getSimpleName() +
"_" + " {" );
+
+ printClassDeclaration( entity, pw );
+
pw.println();
List<IMetaAttribute> members = entity.getMembers();
@@ -263,6 +271,24 @@
}
}
+ private void printClassDeclaration(IMetaEntity entity, PrintWriter pw) {
+ pw.print( "public abstract class " + entity.getSimpleName() + "_"
);
+
+ final TypeMirror superClass = entity.getTypeElement().getSuperclass();
+ //superclass of Object is of NoType which returns some other kind
+ String superclassDeclaration = "";
+ if (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 ( metaEntities.containsKey( superClassName ) ) {
+ pw.print( " extends " + superClassName + "_ " );
+ }
+ }
+
+ pw.println( "{" );
+ }
+
private InputStream getInputStreamForResource(String resource) {
String pkg = getPackage( resource );
String name = getRelativeName( resource );
Modified:
jpamodelgen/trunk/generator/src/main/java/org/hibernate/jpa/metamodel/ap/annotation/MetaEntity.java
===================================================================
---
jpamodelgen/trunk/generator/src/main/java/org/hibernate/jpa/metamodel/ap/annotation/MetaEntity.java 2009-07-08
13:07:29 UTC (rev 17046)
+++
jpamodelgen/trunk/generator/src/main/java/org/hibernate/jpa/metamodel/ap/annotation/MetaEntity.java 2009-07-08
13:18:09 UTC (rev 17047)
@@ -220,6 +220,10 @@
return importType( qualifiedName.toString() );
}
+ public TypeElement getTypeElement() {
+ return element;
+ }
+
private String getKeyType(DeclaredType t) {
return t.getTypeArguments().get( 0 ).toString();
}
Modified:
jpamodelgen/trunk/generator/src/main/java/org/hibernate/jpa/metamodel/ap/xml/XmlMetaEntity.java
===================================================================
---
jpamodelgen/trunk/generator/src/main/java/org/hibernate/jpa/metamodel/ap/xml/XmlMetaEntity.java 2009-07-08
13:07:29 UTC (rev 17046)
+++
jpamodelgen/trunk/generator/src/main/java/org/hibernate/jpa/metamodel/ap/xml/XmlMetaEntity.java 2009-07-08
13:18:09 UTC (rev 17047)
@@ -1,4 +1,4 @@
-// $Id:$
+// $Id$
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
@@ -60,13 +60,13 @@
final private List<IMetaAttribute> members = new
ArrayList<IMetaAttribute>();
- private TypeElement type;
+ private TypeElement element;
- public XmlMetaEntity(Entity ormEntity, String packageName, TypeElement type) {
+ public XmlMetaEntity(Entity ormEntity, String packageName, TypeElement element) {
this.ormEntity = ormEntity;
this.packageName = packageName;
importContext = new ImportContextImpl( getPackageName() );
- this.type = type;
+ this.element = element;
Attributes attributes = ormEntity.getAttributes();
Id id = attributes.getId().get( 0 );
XmlMetaSingleAttribute attribute = new XmlMetaSingleAttribute( this, id.getName(),
getType( id.getName() ) );
@@ -128,9 +128,13 @@
return importType( qualifiedName.toString() );
}
+ public TypeElement getTypeElement() {
+ return element;
+ }
+
private String[] getCollectionType(String propertyName) {
String types[] = new String[2];
- for ( Element elem : type.getEnclosedElements() ) {
+ for ( Element elem : element.getEnclosedElements() ) {
if ( elem.getSimpleName().toString().equals( propertyName ) ) {
DeclaredType type = ( ( DeclaredType ) elem.asType() );
types[0] = type.getTypeArguments().get( 0 ).toString();
@@ -143,7 +147,7 @@
// TODO - so far only prototype. Only tested for the Order orm.xml
private String getType(String propertyName) {
String typeName = null;
- for ( Element elem : type.getEnclosedElements() ) {
+ for ( Element elem : element.getEnclosedElements() ) {
if ( elem.getSimpleName().toString().equals( propertyName ) ) {
switch ( elem.asType().getKind() ) {
case INT: {
@@ -177,7 +181,7 @@
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append( "XmlMetaEntity" );
- sb.append( "{type=" ).append( type );
+ sb.append( "{type=" ).append( element );
sb.append( '}' );
return sb.toString();
}
Added: jpamodelgen/trunk/test/src/main/java/model/Customer.java
===================================================================
--- jpamodelgen/trunk/test/src/main/java/model/Customer.java (rev
0)
+++ jpamodelgen/trunk/test/src/main/java/model/Customer.java 2009-07-08 13:18:09 UTC (rev
17047)
@@ -0,0 +1,22 @@
+package model;
+
+import java.util.Set;
+import javax.persistence.Entity;
+import javax.persistence.OneToMany;
+
+/**
+ * @author Emmanuel Bernard
+ */
+@Entity
+public class Customer extends User {
+ private Set<Order> orders;
+
+ public Set<Order> getOrders() {
+ return orders;
+ }
+
+ @OneToMany
+ public void setOrders(Set<Order> orders) {
+ this.orders = orders;
+ }
+}
Added: jpamodelgen/trunk/test/src/main/java/model/User.java
===================================================================
--- jpamodelgen/trunk/test/src/main/java/model/User.java (rev 0)
+++ jpamodelgen/trunk/test/src/main/java/model/User.java 2009-07-08 13:18:09 UTC (rev
17047)
@@ -0,0 +1,41 @@
+package model;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Transient;
+
+/**
+ * @author Emmanuel Bernard
+ */
+@Entity
+public class User {
+ private Long id;
+ private String nonPersistent;
+ private String name;
+
+ @Id
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ @Transient
+ public String getNonPersistent() {
+ return nonPersistent;
+ }
+
+ public void setNonPersistent(String nonPersistent) {
+ this.nonPersistent = nonPersistent;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+}
Added: jpamodelgen/trunk/test/src/test/java/AccessTypeTest.java
===================================================================
--- jpamodelgen/trunk/test/src/test/java/AccessTypeTest.java (rev
0)
+++ jpamodelgen/trunk/test/src/test/java/AccessTypeTest.java 2009-07-08 13:18:09 UTC (rev
17047)
@@ -0,0 +1,23 @@
+import java.lang.reflect.Field;
+
+import org.testng.annotations.Test;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+import org.testng.Assert;
+
+/**
+ * @author Emmanuel Bernard
+ */
+@Test
+public class AccessTypeTest {
+ @Test
+ public void testDefaultAccessType() throws Exception{
+ Class<?> user_ = Class.forName( "model.User_" );
+ try {
+ final Field nonPersistentField = user_.getField( "nonPersistent" );
+ Assert.fail( "field should not be persistent" );
+ }
+ catch (NoSuchFieldException e) {}
+
+ }
+}
Added: jpamodelgen/trunk/test/src/test/java/InheritanceTest.java
===================================================================
--- jpamodelgen/trunk/test/src/test/java/InheritanceTest.java (rev
0)
+++ jpamodelgen/trunk/test/src/test/java/InheritanceTest.java 2009-07-08 13:18:09 UTC (rev
17047)
@@ -0,0 +1,16 @@
+import org.testng.annotations.Test;
+import org.testng.Assert;
+import model.Customer_;
+import model.User_;
+
+/**
+ * @author Emmanuel Bernard
+ */
+@Test
+public class InheritanceTest {
+ @Test
+ public void testSuperEntity() throws Exception {
+ Assert.assertEquals( Customer_.class.getSuperclass(), User_.class,
+ "Entity with super entity should inherit at the metamodel level");
+ }
+}