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

View File

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

View File

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

View File

@ -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<Annotatio
}
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() );
TypeMirror collectionElementType = TypeUtils.getCollectionElementType(
declaredType, fqNameOfReturnType, explicitTargetEntity, context
);
final TypeElement collectionElement = (TypeElement) context.getTypeUtils()
.asElement( collectionElementType );
AccessTypeInformation accessTypeInfo = context.getAccessTypeInfo( collectionElement.getQualifiedName().toString() );
AccessTypeInformation accessTypeInfo = context.getAccessTypeInfo(
collectionElement.getQualifiedName()
.toString()
);
if ( accessTypeInfo == null ) {
AccessType explicitAccessType = TypeUtils.determineAnnotationSpecifiedAccessType(
collectionElement
@ -179,9 +180,9 @@ public class MetaAttributeGenerationVisitor extends SimpleTypeVisitor6<Annotatio
}
private boolean isBasicAttribute(Element element, Element returnedElement) {
if ( TypeUtils.containsAnnotation( element, Basic.class )
|| TypeUtils.containsAnnotation( element, OneToOne.class )
|| TypeUtils.containsAnnotation( element, ManyToOne.class ) ) {
if ( TypeUtils.containsAnnotation( element, Constants.BASIC )
|| TypeUtils.containsAnnotation( element, Constants.ONE_TO_ONE )
|| TypeUtils.containsAnnotation( element, Constants.MANY_TO_ONE ) ) {
return true;
}
@ -196,7 +197,7 @@ public class MetaAttributeGenerationVisitor extends SimpleTypeVisitor6<Annotatio
private AnnotationMetaAttribute createAnnotationMetaAttributeForMap(DeclaredType declaredType, Element element, String collection, String targetEntity) {
String keyType;
if ( TypeUtils.containsAnnotation( element, MapKeyClass.class ) ) {
if ( TypeUtils.containsAnnotation( element, Constants.MAP_KEY_CLASS ) ) {
TypeMirror typeMirror = (TypeMirror) TypeUtils.getAnnotationValue(
TypeUtils.getAnnotationMirror(
element, MapKeyClass.class
@ -317,7 +318,7 @@ class BasicAttributeVisitor extends SimpleTypeVisitor6<Boolean, Element> {
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() ) {

View File

@ -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<String, String> COLLECTIONS = new HashMap<String, String>();
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<String> BASIC_TYPES = new ArrayList<String>();

View File

@ -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<String> annotationClassNames = new ArrayList<String>();
for ( Class<?> clazz : annotations ) {
annotationClassNames.add( clazz.getName() );
}
Collections.addAll( annotationClassNames, annotations );
List<? extends AnnotationMirror> 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;

View File

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