From 99f40d7e06699be04c677d23d6feb5e88cca5d09 Mon Sep 17 00:00:00 2001 From: Hardy Ferentschik Date: Tue, 17 Jan 2012 13:49:32 +0100 Subject: [PATCH] METAGEN-67 Removing direct access to JPA persistence --- .../hibernate/jpamodelgen/ClassWriter.java | 14 +++++------ .../JPAMetaModelEntityProcessor.java | 24 ++++++++++-------- .../annotation/AnnotationMetaEntity.java | 4 +-- .../MetaAttributeGenerationVisitor.java | 21 ++++++++-------- .../hibernate/jpamodelgen/util/Constants.java | 24 ++++++++++++++++-- .../hibernate/jpamodelgen/util/TypeUtils.java | 25 ++++++++----------- .../jpamodelgen/test/manytoone/Product.java | 2 +- 7 files changed, 66 insertions(+), 48 deletions(-) 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 bdda9e51ee..09e49ae7c3 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 @@ -28,13 +28,13 @@ import javax.lang.model.element.TypeElement; import javax.lang.model.type.DeclaredType; import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; -import javax.persistence.Entity; -import javax.persistence.MappedSuperclass; import javax.tools.Diagnostic; import javax.tools.FileObject; import org.hibernate.jpamodelgen.model.MetaAttribute; import org.hibernate.jpamodelgen.model.MetaEntity; +import org.hibernate.jpamodelgen.util.Constants; +import org.hibernate.jpamodelgen.util.TypeUtils; /** * Helper class to write the actual meta model class using the {@link javax.annotation.processing.Filer} API. @@ -122,8 +122,8 @@ 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 ); - String superClassName = findMappedSuperClass(entity, context); - if(superClassName != null) { + String superClassName = findMappedSuperClass( entity, context ); + if ( superClassName != null ) { pw.print( " extends " + superClassName + META_MODEL_CLASS_NAME_SUFFIX ); } @@ -138,7 +138,7 @@ public final class ClassWriter { final Element superClassElement = ( (DeclaredType) superClass ).asElement(); String superClassName = ( (TypeElement) superClassElement ).getQualifiedName().toString(); if ( extendsSuperMetaModel( superClassElement, entity.isMetaComplete(), context ) ) { - return superClassName; + return superClassName; } superClass = ( (TypeElement) superClassElement ).getSuperclass(); } @@ -169,8 +169,8 @@ public final class ClassWriter { // to allow for the case that the metamodel class for the super entity is for example contained in another // jar file we use reflection. However, we need to consider the fact that there is xml configuration // and annotations should be ignored - if ( !entityMetaComplete && ( superClassElement.getAnnotation( Entity.class ) != null - || superClassElement.getAnnotation( MappedSuperclass.class ) != null ) ) { + if ( !entityMetaComplete && ( TypeUtils.containsAnnotation( superClassElement, Constants.ENTITY ) + || TypeUtils.containsAnnotation( superClassElement, Constants.MAPPED_SUPERCLASS ) ) ) { return true; } diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java index a24b10e72f..cb1210d563 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java @@ -37,9 +37,6 @@ import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; import javax.lang.model.util.ElementFilter; import javax.lang.model.util.SimpleTypeVisitor6; -import javax.persistence.Embeddable; -import javax.persistence.Entity; -import javax.persistence.MappedSuperclass; import javax.tools.Diagnostic; import org.hibernate.jpamodelgen.annotation.AnnotationEmbeddable; @@ -215,7 +212,12 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor { } private boolean isJPAEntity(Element element) { - return TypeUtils.containsAnnotation( element, Entity.class, MappedSuperclass.class, Embeddable.class ); + return TypeUtils.containsAnnotation( + element, + Constants.ENTITY, + Constants.MAPPED_SUPERCLASS, + Constants.EMBEDDABLE + ); } private void handleRootElementAnnotationMirrors(final Element element) { @@ -234,7 +236,7 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor { } AnnotationMetaEntity metaEntity; - if ( TypeUtils.containsAnnotation( element, Embeddable.class ) ) { + if ( TypeUtils.containsAnnotation( element, Constants.EMBEDDABLE ) ) { metaEntity = new AnnotationEmbeddable( (TypeElement) element, context ); } else { @@ -250,24 +252,24 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor { private MetaEntity tryGettingExistingEntityFromContext(AnnotationMirror mirror, String fqn) { MetaEntity alreadyExistingMetaEntity = null; - if ( TypeUtils.isAnnotationMirrorOfType( mirror, Entity.class ) ) { + if ( TypeUtils.isAnnotationMirrorOfType( mirror, Constants.ENTITY ) ) { alreadyExistingMetaEntity = context.getMetaEntity( fqn ); } - else if ( TypeUtils.isAnnotationMirrorOfType( mirror, MappedSuperclass.class ) - || TypeUtils.isAnnotationMirrorOfType( mirror, Embeddable.class ) ) { + else if ( TypeUtils.isAnnotationMirrorOfType( mirror, Constants.MAPPED_SUPERCLASS ) + || TypeUtils.isAnnotationMirrorOfType( mirror, Constants.EMBEDDABLE ) ) { alreadyExistingMetaEntity = context.getMetaEmbeddable( fqn ); } return alreadyExistingMetaEntity; } private void addMetaEntityToContext(AnnotationMirror mirror, AnnotationMetaEntity metaEntity) { - if ( TypeUtils.isAnnotationMirrorOfType( mirror, Entity.class ) ) { + if ( TypeUtils.isAnnotationMirrorOfType( mirror, Constants.ENTITY ) ) { context.addMetaEntity( metaEntity.getQualifiedName(), metaEntity ); } - else if ( TypeUtils.isAnnotationMirrorOfType( mirror, MappedSuperclass.class ) ) { + else if ( TypeUtils.isAnnotationMirrorOfType( mirror, Constants.MAPPED_SUPERCLASS ) ) { context.addMetaEntity( metaEntity.getQualifiedName(), metaEntity ); } - else if ( TypeUtils.isAnnotationMirrorOfType( mirror, Embeddable.class ) ) { + else if ( TypeUtils.isAnnotationMirrorOfType( mirror, Constants.EMBEDDABLE ) ) { context.addMetaEmbeddable( metaEntity.getQualifiedName(), metaEntity ); } } diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMetaEntity.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMetaEntity.java index 70b3683f8b..dd578043ee 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMetaEntity.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMetaEntity.java @@ -28,7 +28,6 @@ import javax.lang.model.element.PackageElement; import javax.lang.model.element.TypeElement; import javax.lang.model.util.ElementFilter; import javax.persistence.AccessType; -import javax.persistence.Transient; import org.hibernate.jpamodelgen.AccessTypeInformation; import org.hibernate.jpamodelgen.Context; @@ -36,6 +35,7 @@ import org.hibernate.jpamodelgen.ImportContextImpl; import org.hibernate.jpamodelgen.model.ImportContext; import org.hibernate.jpamodelgen.model.MetaAttribute; import org.hibernate.jpamodelgen.model.MetaEntity; +import org.hibernate.jpamodelgen.util.Constants; import org.hibernate.jpamodelgen.util.TypeUtils; /** @@ -125,7 +125,7 @@ public class AnnotationMetaEntity implements MetaEntity { continue; } - if ( TypeUtils.containsAnnotation( memberOfClass, Transient.class ) + if ( TypeUtils.containsAnnotation( memberOfClass, Constants.TRANSIENT ) || memberOfClass.getModifiers().contains( Modifier.TRANSIENT ) || memberOfClass.getModifiers().contains( Modifier.STATIC ) ) { continue; diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/MetaAttributeGenerationVisitor.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/MetaAttributeGenerationVisitor.java index 0f9cdf6fc6..a23a709855 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/MetaAttributeGenerationVisitor.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/MetaAttributeGenerationVisitor.java @@ -30,9 +30,7 @@ import javax.lang.model.type.TypeMirror; import javax.lang.model.type.TypeVariable; import javax.lang.model.util.SimpleTypeVisitor6; import javax.persistence.AccessType; -import javax.persistence.Basic; import javax.persistence.ElementCollection; -import javax.persistence.Embeddable; import javax.persistence.ManyToMany; import javax.persistence.ManyToOne; import javax.persistence.MapKeyClass; @@ -128,14 +126,17 @@ public class MetaAttributeGenerationVisitor extends SimpleTypeVisitor6 { if ( Constants.BASIC_TYPES.contains( typeName ) ) { return Boolean.TRUE; } - if ( TypeUtils.containsAnnotation( element, Embeddable.class ) ) { + if ( TypeUtils.containsAnnotation( element, Constants.EMBEDDABLE ) ) { return Boolean.TRUE; } for ( TypeMirror mirror : typeElement.getInterfaces() ) { diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/util/Constants.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/util/Constants.java index 6a2a0a876b..45c857a503 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/util/Constants.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/util/Constants.java @@ -25,6 +25,20 @@ import java.util.Map; * @author Hardy Ferentschik */ public final class Constants { + // we are trying to to reference jpa annotations directly + public static final String ENTITY = "javax.persistence.Entity"; + public static final String MAPPED_SUPERCLASS = "javax.persistence.MappedSuperclass"; + public static final String EMBEDDABLE = "javax.persistence.Embeddable"; + public static final String ID = "javax.persistence.Id"; + public static final String EMBEDDED_ID = "javax.persistence.EmbeddedId"; + public static final String TRANSIENT = "javax.persistence.Transient"; + public static final String BASIC = "javax.persistence.Basic"; + public static final String ONE_TO_ONE = "javax.persistence.OneToOne"; + public static final String MANY_TO_ONE = "javax.persistence.ManyToOne"; + public static final String MAP_KEY_CLASS = "javax.persistence.MapKeyClass"; + public static final String ELEMENT_COLLECTION = "javax.persistence.ElementCollection"; + public static final String ACCESS = "javax.persistence.Access"; + public static Map COLLECTIONS = new HashMap(); static { @@ -36,8 +50,14 @@ public final class Constants { COLLECTIONS.put( java.util.Map.class.getName(), javax.persistence.metamodel.MapAttribute.class.getName() ); // Hibernate also supports the SortedSet and SortedMap interfaces - COLLECTIONS.put( java.util.SortedSet.class.getName(), javax.persistence.metamodel.SetAttribute.class.getName() ); - COLLECTIONS.put( java.util.SortedMap.class.getName(), javax.persistence.metamodel.MapAttribute.class.getName() ); + COLLECTIONS.put( + java.util.SortedSet.class.getName(), + javax.persistence.metamodel.SetAttribute.class.getName() + ); + COLLECTIONS.put( + java.util.SortedMap.class.getName(), + javax.persistence.metamodel.MapAttribute.class.getName() + ); } public static List BASIC_TYPES = new ArrayList(); diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/util/TypeUtils.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/util/TypeUtils.java index 2af8fb1e5e..f84791947e 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/util/TypeUtils.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/util/TypeUtils.java @@ -18,6 +18,7 @@ package org.hibernate.jpamodelgen.util; import java.lang.annotation.Annotation; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -35,19 +36,15 @@ import javax.lang.model.type.TypeVariable; import javax.lang.model.util.ElementFilter; import javax.lang.model.util.Elements; import javax.lang.model.util.SimpleTypeVisitor6; -import javax.persistence.Access; import javax.persistence.AccessType; -import javax.persistence.Embeddable; -import javax.persistence.EmbeddedId; -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.MappedSuperclass; import javax.tools.Diagnostic; import org.hibernate.jpamodelgen.AccessTypeInformation; import org.hibernate.jpamodelgen.Context; import org.hibernate.jpamodelgen.MetaModelGenerationException; +//import javax.persistence.EmbeddedId; + /** * Utility class. * @@ -107,14 +104,12 @@ public final class TypeUtils { } } - public static boolean containsAnnotation(Element element, Class... annotations) { + public static boolean containsAnnotation(Element element, String... annotations) { assert element != null; assert annotations != null; List annotationClassNames = new ArrayList(); - for ( Class clazz : annotations ) { - annotationClassNames.add( clazz.getName() ); - } + Collections.addAll( annotationClassNames, annotations ); List annotationMirrors = element.getAnnotationMirrors(); for ( AnnotationMirror mirror : annotationMirrors ) { @@ -315,7 +310,7 @@ public final class TypeUtils { if ( accessTypeInfo != null && accessTypeInfo.getDefaultAccessType() != null ) { return accessTypeInfo.getDefaultAccessType(); } - if ( TypeUtils.containsAnnotation( superClass, Entity.class, MappedSuperclass.class ) ) { + if ( TypeUtils.containsAnnotation( superClass, Constants.ENTITY, Constants.MAPPED_SUPERCLASS ) ) { defaultAccessType = getAccessTypeInCaseElementIsRoot( superClass, context ); if ( defaultAccessType != null ) { accessTypeInfo = new AccessTypeInformation( fqcn, null, defaultAccessType ); @@ -359,12 +354,12 @@ public final class TypeUtils { } private static boolean isIdAnnotation(AnnotationMirror annotationMirror) { - return TypeUtils.isAnnotationMirrorOfType( annotationMirror, Id.class ) - || TypeUtils.isAnnotationMirrorOfType( annotationMirror, EmbeddedId.class ); + return TypeUtils.isAnnotationMirrorOfType( annotationMirror, Constants.ID ) + || TypeUtils.isAnnotationMirrorOfType( annotationMirror, Constants.EMBEDDED_ID ); } public static AccessType determineAnnotationSpecifiedAccessType(Element element) { - final AnnotationMirror accessAnnotationMirror = TypeUtils.getAnnotationMirror( element, Access.class ); + final AnnotationMirror accessAnnotationMirror = TypeUtils.getAnnotationMirror( element, Constants.ACCESS ); AccessType forcedAccessType = null; if ( accessAnnotationMirror != null ) { Element accessElement = (Element) TypeUtils.getAnnotationValue( @@ -411,7 +406,7 @@ public final class TypeUtils { public String visitDeclared(DeclaredType declaredType, Element element) { TypeElement returnedElement = (TypeElement) context.getTypeUtils().asElement( declaredType ); String fqNameOfReturnType = null; - if ( containsAnnotation( returnedElement, Embeddable.class ) ) { + if ( containsAnnotation( returnedElement, Constants.EMBEDDABLE ) ) { fqNameOfReturnType = returnedElement.getQualifiedName().toString(); } return fqNameOfReturnType; diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/manytoone/Product.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/manytoone/Product.java index 4d7cbefcbf..0387e21aea 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/manytoone/Product.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/manytoone/Product.java @@ -28,7 +28,7 @@ import org.hibernate.jpamodelgen.test.accesstype.Shop; */ @MappedSuperclass public class Product { - @Access(AccessType.FIELD) +// @Access(AccessType.FIELD) @ManyToOne Shop shop; }