METAGEN-67 Removing direct access to JPA persistence

This commit is contained in:
Hardy Ferentschik 2012-01-17 13:49:32 +01:00 committed by Strong Liu
parent 8d1a767b35
commit 99f40d7e06
7 changed files with 66 additions and 48 deletions

View File

@ -28,13 +28,13 @@ import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType; import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror; import javax.lang.model.type.TypeMirror;
import javax.persistence.Entity;
import javax.persistence.MappedSuperclass;
import javax.tools.Diagnostic; import javax.tools.Diagnostic;
import javax.tools.FileObject; import javax.tools.FileObject;
import org.hibernate.jpamodelgen.model.MetaAttribute; import org.hibernate.jpamodelgen.model.MetaAttribute;
import org.hibernate.jpamodelgen.model.MetaEntity; 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. * 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) { private static void printClassDeclaration(MetaEntity entity, PrintWriter pw, Context context) {
pw.print( "public abstract class " + entity.getSimpleName() + META_MODEL_CLASS_NAME_SUFFIX ); pw.print( "public abstract class " + entity.getSimpleName() + META_MODEL_CLASS_NAME_SUFFIX );
String superClassName = findMappedSuperClass(entity, context); String superClassName = findMappedSuperClass( entity, context );
if(superClassName != null) { if ( superClassName != null ) {
pw.print( " extends " + superClassName + META_MODEL_CLASS_NAME_SUFFIX ); pw.print( " extends " + superClassName + META_MODEL_CLASS_NAME_SUFFIX );
} }
@ -138,7 +138,7 @@ public final class ClassWriter {
final Element superClassElement = ( (DeclaredType) superClass ).asElement(); final Element superClassElement = ( (DeclaredType) superClass ).asElement();
String superClassName = ( (TypeElement) superClassElement ).getQualifiedName().toString(); String superClassName = ( (TypeElement) superClassElement ).getQualifiedName().toString();
if ( extendsSuperMetaModel( superClassElement, entity.isMetaComplete(), context ) ) { if ( extendsSuperMetaModel( superClassElement, entity.isMetaComplete(), context ) ) {
return superClassName; return superClassName;
} }
superClass = ( (TypeElement) superClassElement ).getSuperclass(); 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 // 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 // jar file we use reflection. However, we need to consider the fact that there is xml configuration
// and annotations should be ignored // and annotations should be ignored
if ( !entityMetaComplete && ( superClassElement.getAnnotation( Entity.class ) != null if ( !entityMetaComplete && ( TypeUtils.containsAnnotation( superClassElement, Constants.ENTITY )
|| superClassElement.getAnnotation( MappedSuperclass.class ) != null ) ) { || TypeUtils.containsAnnotation( superClassElement, Constants.MAPPED_SUPERCLASS ) ) ) {
return true; return true;
} }

View File

@ -37,9 +37,6 @@ import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror; import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.ElementFilter; import javax.lang.model.util.ElementFilter;
import javax.lang.model.util.SimpleTypeVisitor6; import javax.lang.model.util.SimpleTypeVisitor6;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.MappedSuperclass;
import javax.tools.Diagnostic; import javax.tools.Diagnostic;
import org.hibernate.jpamodelgen.annotation.AnnotationEmbeddable; import org.hibernate.jpamodelgen.annotation.AnnotationEmbeddable;
@ -215,7 +212,12 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
} }
private boolean isJPAEntity(Element element) { 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) { private void handleRootElementAnnotationMirrors(final Element element) {
@ -234,7 +236,7 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
} }
AnnotationMetaEntity metaEntity; AnnotationMetaEntity metaEntity;
if ( TypeUtils.containsAnnotation( element, Embeddable.class ) ) { if ( TypeUtils.containsAnnotation( element, Constants.EMBEDDABLE ) ) {
metaEntity = new AnnotationEmbeddable( (TypeElement) element, context ); metaEntity = new AnnotationEmbeddable( (TypeElement) element, context );
} }
else { else {
@ -250,24 +252,24 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
private MetaEntity tryGettingExistingEntityFromContext(AnnotationMirror mirror, String fqn) { private MetaEntity tryGettingExistingEntityFromContext(AnnotationMirror mirror, String fqn) {
MetaEntity alreadyExistingMetaEntity = null; MetaEntity alreadyExistingMetaEntity = null;
if ( TypeUtils.isAnnotationMirrorOfType( mirror, Entity.class ) ) { if ( TypeUtils.isAnnotationMirrorOfType( mirror, Constants.ENTITY ) ) {
alreadyExistingMetaEntity = context.getMetaEntity( fqn ); alreadyExistingMetaEntity = context.getMetaEntity( fqn );
} }
else if ( TypeUtils.isAnnotationMirrorOfType( mirror, MappedSuperclass.class ) else if ( TypeUtils.isAnnotationMirrorOfType( mirror, Constants.MAPPED_SUPERCLASS )
|| TypeUtils.isAnnotationMirrorOfType( mirror, Embeddable.class ) ) { || TypeUtils.isAnnotationMirrorOfType( mirror, Constants.EMBEDDABLE ) ) {
alreadyExistingMetaEntity = context.getMetaEmbeddable( fqn ); alreadyExistingMetaEntity = context.getMetaEmbeddable( fqn );
} }
return alreadyExistingMetaEntity; return alreadyExistingMetaEntity;
} }
private void addMetaEntityToContext(AnnotationMirror mirror, AnnotationMetaEntity metaEntity) { private void addMetaEntityToContext(AnnotationMirror mirror, AnnotationMetaEntity metaEntity) {
if ( TypeUtils.isAnnotationMirrorOfType( mirror, Entity.class ) ) { if ( TypeUtils.isAnnotationMirrorOfType( mirror, Constants.ENTITY ) ) {
context.addMetaEntity( metaEntity.getQualifiedName(), metaEntity ); 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 ); context.addMetaEntity( metaEntity.getQualifiedName(), metaEntity );
} }
else if ( TypeUtils.isAnnotationMirrorOfType( mirror, Embeddable.class ) ) { else if ( TypeUtils.isAnnotationMirrorOfType( mirror, Constants.EMBEDDABLE ) ) {
context.addMetaEmbeddable( metaEntity.getQualifiedName(), metaEntity ); context.addMetaEmbeddable( metaEntity.getQualifiedName(), metaEntity );
} }
} }

View File

@ -28,7 +28,6 @@ import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement; import javax.lang.model.element.TypeElement;
import javax.lang.model.util.ElementFilter; import javax.lang.model.util.ElementFilter;
import javax.persistence.AccessType; import javax.persistence.AccessType;
import javax.persistence.Transient;
import org.hibernate.jpamodelgen.AccessTypeInformation; import org.hibernate.jpamodelgen.AccessTypeInformation;
import org.hibernate.jpamodelgen.Context; 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.ImportContext;
import org.hibernate.jpamodelgen.model.MetaAttribute; import org.hibernate.jpamodelgen.model.MetaAttribute;
import org.hibernate.jpamodelgen.model.MetaEntity; import org.hibernate.jpamodelgen.model.MetaEntity;
import org.hibernate.jpamodelgen.util.Constants;
import org.hibernate.jpamodelgen.util.TypeUtils; import org.hibernate.jpamodelgen.util.TypeUtils;
/** /**
@ -125,7 +125,7 @@ public class AnnotationMetaEntity implements MetaEntity {
continue; continue;
} }
if ( TypeUtils.containsAnnotation( memberOfClass, Transient.class ) if ( TypeUtils.containsAnnotation( memberOfClass, Constants.TRANSIENT )
|| memberOfClass.getModifiers().contains( Modifier.TRANSIENT ) || memberOfClass.getModifiers().contains( Modifier.TRANSIENT )
|| memberOfClass.getModifiers().contains( Modifier.STATIC ) ) { || memberOfClass.getModifiers().contains( Modifier.STATIC ) ) {
continue; continue;

View File

@ -30,9 +30,7 @@ import javax.lang.model.type.TypeMirror;
import javax.lang.model.type.TypeVariable; import javax.lang.model.type.TypeVariable;
import javax.lang.model.util.SimpleTypeVisitor6; import javax.lang.model.util.SimpleTypeVisitor6;
import javax.persistence.AccessType; import javax.persistence.AccessType;
import javax.persistence.Basic;
import javax.persistence.ElementCollection; import javax.persistence.ElementCollection;
import javax.persistence.Embeddable;
import javax.persistence.ManyToMany; import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.MapKeyClass; import javax.persistence.MapKeyClass;
@ -128,14 +126,17 @@ public class MetaAttributeGenerationVisitor extends SimpleTypeVisitor6<Annotatio
} }
private AnnotationMetaAttribute createMetaCollectionAttribute(DeclaredType declaredType, Element element, String fqNameOfReturnType, String collection, String targetEntity) { private AnnotationMetaAttribute createMetaCollectionAttribute(DeclaredType declaredType, Element element, String fqNameOfReturnType, String collection, String targetEntity) {
if ( TypeUtils.containsAnnotation( element, ElementCollection.class ) ) { if ( TypeUtils.containsAnnotation( element, Constants.ELEMENT_COLLECTION ) ) {
String explicitTargetEntity = getTargetEntity( element.getAnnotationMirrors() ); String explicitTargetEntity = getTargetEntity( element.getAnnotationMirrors() );
TypeMirror collectionElementType = TypeUtils.getCollectionElementType( TypeMirror collectionElementType = TypeUtils.getCollectionElementType(
declaredType, fqNameOfReturnType, explicitTargetEntity, context declaredType, fqNameOfReturnType, explicitTargetEntity, context
); );
final TypeElement collectionElement = (TypeElement) context.getTypeUtils() final TypeElement collectionElement = (TypeElement) context.getTypeUtils()
.asElement( collectionElementType ); .asElement( collectionElementType );
AccessTypeInformation accessTypeInfo = context.getAccessTypeInfo( collectionElement.getQualifiedName().toString() ); AccessTypeInformation accessTypeInfo = context.getAccessTypeInfo(
collectionElement.getQualifiedName()
.toString()
);
if ( accessTypeInfo == null ) { if ( accessTypeInfo == null ) {
AccessType explicitAccessType = TypeUtils.determineAnnotationSpecifiedAccessType( AccessType explicitAccessType = TypeUtils.determineAnnotationSpecifiedAccessType(
collectionElement collectionElement
@ -166,7 +167,7 @@ public class MetaAttributeGenerationVisitor extends SimpleTypeVisitor6<Annotatio
@Override @Override
public AnnotationMetaAttribute visitExecutable(ExecutableType t, Element p) { public AnnotationMetaAttribute visitExecutable(ExecutableType t, Element p) {
if ( !p.getKind().equals( ElementKind.METHOD ) ) { if ( !p.getKind().equals( ElementKind.METHOD ) ) {
return null; return null;
} }
String string = p.getSimpleName().toString(); String string = p.getSimpleName().toString();
@ -179,9 +180,9 @@ public class MetaAttributeGenerationVisitor extends SimpleTypeVisitor6<Annotatio
} }
private boolean isBasicAttribute(Element element, Element returnedElement) { private boolean isBasicAttribute(Element element, Element returnedElement) {
if ( TypeUtils.containsAnnotation( element, Basic.class ) if ( TypeUtils.containsAnnotation( element, Constants.BASIC )
|| TypeUtils.containsAnnotation( element, OneToOne.class ) || TypeUtils.containsAnnotation( element, Constants.ONE_TO_ONE )
|| TypeUtils.containsAnnotation( element, ManyToOne.class ) ) { || TypeUtils.containsAnnotation( element, Constants.MANY_TO_ONE ) ) {
return true; return true;
} }
@ -196,7 +197,7 @@ public class MetaAttributeGenerationVisitor extends SimpleTypeVisitor6<Annotatio
private AnnotationMetaAttribute createAnnotationMetaAttributeForMap(DeclaredType declaredType, Element element, String collection, String targetEntity) { private AnnotationMetaAttribute createAnnotationMetaAttributeForMap(DeclaredType declaredType, Element element, String collection, String targetEntity) {
String keyType; String keyType;
if ( TypeUtils.containsAnnotation( element, MapKeyClass.class ) ) { if ( TypeUtils.containsAnnotation( element, Constants.MAP_KEY_CLASS ) ) {
TypeMirror typeMirror = (TypeMirror) TypeUtils.getAnnotationValue( TypeMirror typeMirror = (TypeMirror) TypeUtils.getAnnotationValue(
TypeUtils.getAnnotationMirror( TypeUtils.getAnnotationMirror(
element, MapKeyClass.class element, MapKeyClass.class
@ -317,7 +318,7 @@ class BasicAttributeVisitor extends SimpleTypeVisitor6<Boolean, Element> {
if ( Constants.BASIC_TYPES.contains( typeName ) ) { if ( Constants.BASIC_TYPES.contains( typeName ) ) {
return Boolean.TRUE; return Boolean.TRUE;
} }
if ( TypeUtils.containsAnnotation( element, Embeddable.class ) ) { if ( TypeUtils.containsAnnotation( element, Constants.EMBEDDABLE ) ) {
return Boolean.TRUE; return Boolean.TRUE;
} }
for ( TypeMirror mirror : typeElement.getInterfaces() ) { for ( TypeMirror mirror : typeElement.getInterfaces() ) {

View File

@ -25,6 +25,20 @@ import java.util.Map;
* @author Hardy Ferentschik * @author Hardy Ferentschik
*/ */
public final class Constants { 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<String, String> COLLECTIONS = new HashMap<String, String>(); public static Map<String, String> COLLECTIONS = new HashMap<String, String>();
static { static {
@ -36,8 +50,14 @@ public final class Constants {
COLLECTIONS.put( java.util.Map.class.getName(), javax.persistence.metamodel.MapAttribute.class.getName() ); COLLECTIONS.put( java.util.Map.class.getName(), javax.persistence.metamodel.MapAttribute.class.getName() );
// Hibernate also supports the SortedSet and SortedMap interfaces // Hibernate also supports the SortedSet and SortedMap interfaces
COLLECTIONS.put( java.util.SortedSet.class.getName(), javax.persistence.metamodel.SetAttribute.class.getName() ); COLLECTIONS.put(
COLLECTIONS.put( java.util.SortedMap.class.getName(), javax.persistence.metamodel.MapAttribute.class.getName() ); 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<String> BASIC_TYPES = new ArrayList<String>(); public static List<String> BASIC_TYPES = new ArrayList<String>();

View File

@ -18,6 +18,7 @@ package org.hibernate.jpamodelgen.util;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; 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.ElementFilter;
import javax.lang.model.util.Elements; import javax.lang.model.util.Elements;
import javax.lang.model.util.SimpleTypeVisitor6; import javax.lang.model.util.SimpleTypeVisitor6;
import javax.persistence.Access;
import javax.persistence.AccessType; 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 javax.tools.Diagnostic;
import org.hibernate.jpamodelgen.AccessTypeInformation; import org.hibernate.jpamodelgen.AccessTypeInformation;
import org.hibernate.jpamodelgen.Context; import org.hibernate.jpamodelgen.Context;
import org.hibernate.jpamodelgen.MetaModelGenerationException; import org.hibernate.jpamodelgen.MetaModelGenerationException;
//import javax.persistence.EmbeddedId;
/** /**
* Utility class. * 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 element != null;
assert annotations != null; assert annotations != null;
List<String> annotationClassNames = new ArrayList<String>(); List<String> annotationClassNames = new ArrayList<String>();
for ( Class<?> clazz : annotations ) { Collections.addAll( annotationClassNames, annotations );
annotationClassNames.add( clazz.getName() );
}
List<? extends AnnotationMirror> annotationMirrors = element.getAnnotationMirrors(); List<? extends AnnotationMirror> annotationMirrors = element.getAnnotationMirrors();
for ( AnnotationMirror mirror : annotationMirrors ) { for ( AnnotationMirror mirror : annotationMirrors ) {
@ -315,7 +310,7 @@ public final class TypeUtils {
if ( accessTypeInfo != null && accessTypeInfo.getDefaultAccessType() != null ) { if ( accessTypeInfo != null && accessTypeInfo.getDefaultAccessType() != null ) {
return accessTypeInfo.getDefaultAccessType(); return accessTypeInfo.getDefaultAccessType();
} }
if ( TypeUtils.containsAnnotation( superClass, Entity.class, MappedSuperclass.class ) ) { if ( TypeUtils.containsAnnotation( superClass, Constants.ENTITY, Constants.MAPPED_SUPERCLASS ) ) {
defaultAccessType = getAccessTypeInCaseElementIsRoot( superClass, context ); defaultAccessType = getAccessTypeInCaseElementIsRoot( superClass, context );
if ( defaultAccessType != null ) { if ( defaultAccessType != null ) {
accessTypeInfo = new AccessTypeInformation( fqcn, null, defaultAccessType ); accessTypeInfo = new AccessTypeInformation( fqcn, null, defaultAccessType );
@ -359,12 +354,12 @@ public final class TypeUtils {
} }
private static boolean isIdAnnotation(AnnotationMirror annotationMirror) { private static boolean isIdAnnotation(AnnotationMirror annotationMirror) {
return TypeUtils.isAnnotationMirrorOfType( annotationMirror, Id.class ) return TypeUtils.isAnnotationMirrorOfType( annotationMirror, Constants.ID )
|| TypeUtils.isAnnotationMirrorOfType( annotationMirror, EmbeddedId.class ); || TypeUtils.isAnnotationMirrorOfType( annotationMirror, Constants.EMBEDDED_ID );
} }
public static AccessType determineAnnotationSpecifiedAccessType(Element element) { 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; AccessType forcedAccessType = null;
if ( accessAnnotationMirror != null ) { if ( accessAnnotationMirror != null ) {
Element accessElement = (Element) TypeUtils.getAnnotationValue( Element accessElement = (Element) TypeUtils.getAnnotationValue(
@ -411,7 +406,7 @@ public final class TypeUtils {
public String visitDeclared(DeclaredType declaredType, Element element) { public String visitDeclared(DeclaredType declaredType, Element element) {
TypeElement returnedElement = (TypeElement) context.getTypeUtils().asElement( declaredType ); TypeElement returnedElement = (TypeElement) context.getTypeUtils().asElement( declaredType );
String fqNameOfReturnType = null; String fqNameOfReturnType = null;
if ( containsAnnotation( returnedElement, Embeddable.class ) ) { if ( containsAnnotation( returnedElement, Constants.EMBEDDABLE ) ) {
fqNameOfReturnType = returnedElement.getQualifiedName().toString(); fqNameOfReturnType = returnedElement.getQualifiedName().toString();
} }
return fqNameOfReturnType; return fqNameOfReturnType;

View File

@ -28,7 +28,7 @@ import org.hibernate.jpamodelgen.test.accesstype.Shop;
*/ */
@MappedSuperclass @MappedSuperclass
public class Product { public class Product {
@Access(AccessType.FIELD) // @Access(AccessType.FIELD)
@ManyToOne @ManyToOne
Shop shop; Shop shop;
} }