HHH-6133 mock attribute annotations wrt access type, was mock one attribute annotations on both property and field

This commit is contained in:
Strong Liu 2011-05-12 15:34:39 +08:00 committed by Hardy Ferentschik
parent 635ee5a0ec
commit 7e999e421d
26 changed files with 384 additions and 82 deletions

View File

@ -64,26 +64,31 @@ class ExclusiveAnnotationFilter extends AbstractAnnotationFilter {
group.add( EMBEDDABLE );
group.scope = Scope.TYPE;
exclusiveGroupList.add( group );
group = new ExclusiveGroup();
group.add( SECONDARY_TABLES );
group.add( SECONDARY_TABLE );
group.scope = Scope.TYPE;
exclusiveGroupList.add( group );
group = new ExclusiveGroup();
group.add( PRIMARY_KEY_JOIN_COLUMNS );
group.add( PRIMARY_KEY_JOIN_COLUMN );
group.scope = Scope.ATTRIBUTE;
exclusiveGroupList.add( group );
group = new ExclusiveGroup();
group.add( SQL_RESULT_SET_MAPPING );
group.add( SQL_RESULT_SET_MAPPINGS );
group.scope = Scope.TYPE;
exclusiveGroupList.add( group );
group = new ExclusiveGroup();
group.add( NAMED_NATIVE_QUERY );
group.add( NAMED_NATIVE_QUERIES );
group.scope = Scope.TYPE;
exclusiveGroupList.add( group );
group = new ExclusiveGroup();
group.add( NAMED_QUERY );
group.add( NAMED_QUERIES );
@ -132,7 +137,7 @@ class ExclusiveAnnotationFilter extends AbstractAnnotationFilter {
Iterator<AnnotationInstance> iter = indexedAnnotationInstanceList.iterator();
while ( iter.hasNext() ) {
AnnotationInstance ann = iter.next();
if ( MockHelper.targetEquals( target, ann.target(), true ) ) {
if ( MockHelper.targetEquals( target, ann.target() ) ) {
iter.remove();
}
}

View File

@ -6,6 +6,8 @@ import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.DotName;
/**
* if class from index has matched annotations, then remove.
*
* @author Strong Liu
*/
class NameAnnotationFilter extends AbstractAnnotationFilter {

View File

@ -42,7 +42,7 @@ class NameTargetAnnotationFilter extends AbstractAnnotationFilter {
for ( Iterator<AnnotationInstance> iter = indexedAnnotationInstanceList.iterator(); iter.hasNext(); ) {
AnnotationInstance ann = iter.next();
if ( MockHelper.targetEquals( target, ann.target(), false ) ) {
if ( MockHelper.targetEquals( target, ann.target() ) ) {
iter.remove();
}
}

View File

@ -28,6 +28,7 @@ import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.hibernate.AssertionFailure;
import org.hibernate.metamodel.source.annotation.xml.XMLAccessType;
import org.hibernate.metamodel.source.annotation.xml.XMLAttributes;
import org.hibernate.metamodel.source.annotation.xml.XMLEntityListeners;
@ -51,12 +52,28 @@ abstract class AbstractEntityObjectMocker extends AnnotationMocker {
AbstractEntityObjectMocker(IndexBuilder indexBuilder, EntityMappingsMocker.Default defaults) {
super( indexBuilder, defaults );
}
private boolean isPreProcessCalled=false;
final void process() {
final void preProcess() {
applyDefaults();
classInfo = indexBuilder.createClassInfo( getClassName() );
DotName classDotName = classInfo.name();
indexBuilder.metadataComplete( classDotName, isMetadataComplete() );
parserAccessType( getAccessType(), getTarget() );
isPreProcessCalled = true;
}
final void process() {
if(!isPreProcessCalled){
throw new AssertionFailure( "preProcess should be called before process" );
}
if ( getAccessType() == null ) {
XMLAccessType accessType = AccessHelper.getEntityAccess( getTargetName(), indexBuilder );
if ( accessType == null ) {
accessType = getDefaults().getAccess();
}
parserAccessType( accessType, getTarget() );
}
processExtra();
if ( isExcludeDefaultListeners() ) {
create( EXCLUDE_DEFAULT_LISTENERS );
@ -65,7 +82,7 @@ abstract class AbstractEntityObjectMocker extends AnnotationMocker {
create( EXCLUDE_SUPERCLASS_LISTENERS );
}
parserIdClass( getIdClass() );
parserAccessType( getAccessType(), getTarget() );
if ( getAttributes() != null ) {
getAttributesBuilder().parser();

View File

@ -54,7 +54,7 @@ import org.hibernate.metamodel.source.annotations.JPADotNames;
* @author Strong Liu
*/
abstract class AbstractMocker implements JPADotNames {
protected IndexBuilder indexBuilder;
final protected IndexBuilder indexBuilder;
AbstractMocker(IndexBuilder indexBuilder) {
this.indexBuilder = indexBuilder;
@ -241,7 +241,7 @@ abstract class AbstractMocker implements JPADotNames {
if ( MockHelper.isNotEmpty( annotationInstanceList ) ) {
for ( AnnotationInstance annotationInstance : annotationInstanceList ) {
AnnotationTarget annotationTarget = annotationInstance.target();
if ( MockHelper.targetEquals( target, annotationTarget, false ) ) {
if ( MockHelper.targetEquals( target, annotationTarget ) ) {
if ( operation.process( annotationInstance ) ) {
return;
}

View File

@ -0,0 +1,163 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc..
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.metamodel.source.annotations.xml.mocker;
import java.util.List;
import java.util.Map;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.MethodInfo;
import org.hibernate.AnnotationException;
import org.hibernate.metamodel.source.annotation.xml.XMLAccessType;
import org.hibernate.metamodel.source.annotations.JPADotNames;
import org.hibernate.metamodel.source.annotations.util.JandexHelper;
/**
* @author Strong Liu
*/
class AccessHelper implements JPADotNames {
static XMLAccessType getAccessFromIdPosition(DotName className, IndexBuilder indexBuilder) {
Map<DotName, List<AnnotationInstance>> indexedAnnotations = indexBuilder.getIndexedAnnotations( className );
Map<DotName, List<AnnotationInstance>> ormAnnotations = indexBuilder.getClassInfoAnnotationsMap( className );
XMLAccessType accessType = getAccessFromIdPosition( ormAnnotations );
if ( accessType == null ) {
accessType = getAccessFromIdPosition( indexedAnnotations );
}
if ( accessType == null ) {
ClassInfo parent = indexBuilder.getClassInfo( className );
if ( parent == null ) {
parent = indexBuilder.getIndexedClassInfo( className );
}
if ( parent != null ) {
DotName parentClassName = parent.superName();
accessType = getAccessFromIdPosition( parentClassName, indexBuilder );
}
}
return accessType;
}
private static XMLAccessType getAccessFromIdPosition(Map<DotName, List<AnnotationInstance>> annotations) {
if ( annotations == null || annotations.isEmpty() || !( annotations.containsKey( ID ) ) ) {
return null;
}
List<AnnotationInstance> idAnnotationInstances = annotations.get( ID );
if ( MockHelper.isNotEmpty( idAnnotationInstances ) ) {
return processIdAnnotations( idAnnotationInstances );
}
return null;
}
private static XMLAccessType processIdAnnotations(List<AnnotationInstance> idAnnotations) {
XMLAccessType accessType = null;
for ( AnnotationInstance annotation : idAnnotations ) {
AnnotationTarget tmpTarget = annotation.target();
if ( tmpTarget == null ) {
continue;
}
if ( accessType == null ) {
accessType = annotationTargetToAccessType( tmpTarget );
}
else {
if ( !accessType.equals( annotationTargetToAccessType( tmpTarget ) ) ) {
throw new AnnotationException( "Inconsistent placement of @Id annotation within hierarchy " );
}
}
}
return accessType;
}
static XMLAccessType annotationTargetToAccessType(AnnotationTarget target) {
return ( target instanceof MethodInfo ) ? XMLAccessType.PROPERTY : XMLAccessType.FIELD;
}
static XMLAccessType getEntityAccess(DotName className, IndexBuilder indexBuilder) {
Map<DotName, List<AnnotationInstance>> indexedAnnotations = indexBuilder.getIndexedAnnotations( className );
Map<DotName, List<AnnotationInstance>> ormAnnotations = indexBuilder.getClassInfoAnnotationsMap( className );
XMLAccessType accessType = getAccess( ormAnnotations );
if ( accessType == null ) {
accessType = getAccess( indexedAnnotations );
}
if ( accessType == null ) {
ClassInfo parent = indexBuilder.getClassInfo( className );
if ( parent == null ) {
parent = indexBuilder.getIndexedClassInfo( className );
}
if ( parent != null ) {
DotName parentClassName = parent.superName();
accessType = getEntityAccess( parentClassName, indexBuilder );
}
}
return accessType;
}
private static XMLAccessType getAccess(Map<DotName, List<AnnotationInstance>> annotations) {
if ( annotations == null || annotations.isEmpty() || !isEntityObject( annotations ) ) {
return null;
}
List<AnnotationInstance> accessAnnotationInstances = annotations.get( JPADotNames.ACCESS );
if ( MockHelper.isNotEmpty( accessAnnotationInstances ) ) {
for ( AnnotationInstance annotationInstance : accessAnnotationInstances ) {
if ( annotationInstance.target() != null && annotationInstance.target() instanceof ClassInfo ) {
return XMLAccessType.valueOf( annotationInstance.value().asEnum() );
}
}
}
return null;
}
private static boolean isEntityObject(Map<DotName, List<AnnotationInstance>> annotations) {
return annotations.containsKey( ENTITY ) || annotations.containsKey( MAPPED_SUPERCLASS ) || annotations
.containsKey( EMBEDDABLE );
}
static XMLAccessType getAccessFromAttributeAnnotation(DotName className, String attributeName, IndexBuilder indexBuilder) {
Map<DotName, List<AnnotationInstance>> indexedAnnotations = indexBuilder.getIndexedAnnotations( className );
if ( indexedAnnotations != null && indexedAnnotations.containsKey( ACCESS ) ) {
List<AnnotationInstance> annotationInstances = indexedAnnotations.get( ACCESS );
if ( MockHelper.isNotEmpty( annotationInstances ) ) {
for ( AnnotationInstance annotationInstance : annotationInstances ) {
AnnotationTarget indexedPropertyTarget = annotationInstance.target();
if ( indexedPropertyTarget == null ) {
continue;
}
if ( JandexHelper.getPropertyName( indexedPropertyTarget ).equals( attributeName ) ) {
XMLAccessType accessType = XMLAccessType.valueOf( annotationInstance.value().asEnum() );
XMLAccessType targetAccessType = ( indexedPropertyTarget instanceof MethodInfo ) ? XMLAccessType.PROPERTY : XMLAccessType.FIELD;
if ( accessType == targetAccessType ) {
return targetAccessType;
}
}
}
}
}
return null;
}
}

View File

@ -66,5 +66,9 @@ class BasicMocker extends PropertyMocker {
protected XMLAccessType getAccessType() {
return basic.getAccess();
}
@Override
protected void setAccessType(XMLAccessType accessType) {
basic.setAccess( accessType );
}
}

View File

@ -84,4 +84,8 @@ class ElementCollectionMocker extends PropertyMocker {
protected XMLAccessType getAccessType() {
return elementCollection.getAccess();
}
@Override
protected void setAccessType(XMLAccessType accessType) {
elementCollection.setAccess( accessType );
}
}

View File

@ -77,9 +77,6 @@ class EmbeddableMocker extends AbstractEntityObjectMocker {
if ( embeddable.isMetadataComplete() == null ) {
embeddable.setMetadataComplete( getDefaults().getMetadataComplete() );
}
if ( embeddable.getAccess() == null ) {
embeddable.setAccess( getDefaults().getAccess() );
}
LOG.debugf( "Adding XML overriding information for %s", className );
}

View File

@ -53,4 +53,8 @@ class EmbeddedIdMocker extends PropertyMocker {
protected XMLAccessType getAccessType() {
return embeddedId.getAccess();
}
@Override
protected void setAccessType(XMLAccessType accessType) {
embeddedId.setAccess( accessType );
}
}

View File

@ -56,4 +56,9 @@ class EmbeddedMocker extends PropertyMocker {
protected XMLAccessType getAccessType() {
return embedded.getAccess();
}
@Override
protected void setAccessType(XMLAccessType accessType) {
embedded.setAccess( accessType );
}
}

View File

@ -24,6 +24,7 @@
package org.hibernate.metamodel.source.annotations.xml.mocker;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.jboss.jandex.Index;
@ -109,20 +110,33 @@ public class EntityMappingsMocker {
private void processEntityMappings(List<XMLEntityMappings> entityMappingsList) {
List<AbstractEntityObjectMocker> mockerList = new ArrayList<AbstractEntityObjectMocker>();
for ( XMLEntityMappings entityMappings : entityMappingsList ) {
final Default defaults = getEntityMappingsDefaults( entityMappings );
globalAnnotations.collectGlobalMappings( entityMappings, defaults );
for ( XMLMappedSuperclass mappedSuperclass : entityMappings.getMappedSuperclass() ) {
new MappedSuperclassMocker( indexBuilder, mappedSuperclass, defaults ).process();
AbstractEntityObjectMocker mocker =
new MappedSuperclassMocker( indexBuilder, mappedSuperclass, defaults );
mockerList.add( mocker );
mocker.preProcess();
}
for ( XMLEmbeddable embeddable : entityMappings.getEmbeddable() ) {
new EmbeddableMocker( indexBuilder, embeddable, defaults ).process();
AbstractEntityObjectMocker mocker =
new EmbeddableMocker( indexBuilder, embeddable, defaults );
mockerList.add( mocker );
mocker.preProcess();
}
for ( XMLEntity entity : entityMappings.getEntity() ) {
globalAnnotations.collectGlobalMappings( entity, defaults );
new EntityMocker( indexBuilder, entity, defaults ).process();
AbstractEntityObjectMocker mocker =
new EntityMocker( indexBuilder, entity, defaults );
mockerList.add( mocker );
mocker.preProcess();
}
}
for ( AbstractEntityObjectMocker mocker : mockerList ) {
mocker.process();
}
}
private void processGlobalConfiguration() {

View File

@ -2,10 +2,14 @@ package org.hibernate.metamodel.source.annotations.xml.mocker;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.persistence.AccessType;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.logging.Logger;
import org.hibernate.internal.CoreMessageLogger;
@ -26,6 +30,7 @@ import org.hibernate.metamodel.source.annotation.xml.XMLPreRemove;
import org.hibernate.metamodel.source.annotation.xml.XMLPreUpdate;
import org.hibernate.metamodel.source.annotation.xml.XMLSecondaryTable;
import org.hibernate.metamodel.source.annotation.xml.XMLTable;
import org.hibernate.metamodel.source.annotations.util.JandexHelper;
/**
@ -97,6 +102,33 @@ class EntityMocker extends AbstractEntityObjectMocker {
return create( TABLE, annotationValueList );
}
protected AccessType getDefaultAccess() {
if ( entity.getAccess() != null ) {
return AccessType.valueOf( entity.getAccess().value() );
}
return null;
}
protected AccessType getAccessFromIndex(DotName className) {
//todo 这里实际上不应该从getIndexedAnnotations获取而是应该先处理完所有的entitymapped-superclass先不处理attributes呢
//然后获取这个
Map<DotName, List<AnnotationInstance>> indexedAnnotations = indexBuilder.getIndexedAnnotations( className );
List<AnnotationInstance> accessAnnotationInstances = indexedAnnotations.get( ACCESS );
if ( MockHelper.isNotEmpty( accessAnnotationInstances ) ) {
for ( AnnotationInstance annotationInstance : accessAnnotationInstances ) {
if ( annotationInstance.target() != null && annotationInstance.target() instanceof ClassInfo ) {
ClassInfo ci = (ClassInfo) ( annotationInstance.target() );
if ( className.equals( ci.name() ) ) {
//todo does ci need to have @Entity or @MappedSuperClass ??
return AccessType.valueOf( annotationInstance.value().asEnum() );
}
}
}
}
return null;
}
@Override
protected void applyDefaults() {
if ( getDefaults() == null ) {
@ -115,9 +147,6 @@ class EntityMocker extends AbstractEntityObjectMocker {
if ( entity.isMetadataComplete() == null ) {
entity.setMetadataComplete( getDefaults().getMetadataComplete() );
}
if ( entity.getAccess() != null ) {
entity.setAccess( getDefaults().getAccess() );
}
LOG.debugf( "Adding XML overriding information for %s", className );
}

View File

@ -25,12 +25,17 @@ package org.hibernate.metamodel.source.annotations.xml.mocker;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.MethodInfo;
import org.hibernate.AnnotationException;
import org.hibernate.HibernateException;
import org.hibernate.metamodel.source.annotation.xml.XMLAccessType;
import org.hibernate.metamodel.source.annotation.xml.XMLGeneratedValue;
import org.hibernate.metamodel.source.annotation.xml.XMLId;
@ -72,10 +77,15 @@ class IdMocker extends PropertyMocker {
return id.getName();
}
@Override
protected XMLAccessType getAccessType() {
return id.getAccess();
}
@Override
protected void setAccessType(XMLAccessType accessType) {
id.setAccess( accessType );
}
}

View File

@ -150,6 +150,17 @@ public class IndexBuilder {
return map;
}
public Map<DotName, List<AnnotationInstance>> getClassInfoAnnotationsMap(DotName name) {
return classInfoAnnotationsMap.get( name );
}
public ClassInfo getClassInfo(DotName name) {
return classes.get( name );
}
public ClassInfo getIndexedClassInfo(DotName name){
return index.getClassByName( name );
}
void collectGlobalConfigurationFromIndex(GlobalAnnotations globalAnnotations) {
for ( DotName annName : IndexedAnnotationFilter.GLOBAL_ANNOTATIONS ) {
List<AnnotationInstance> annotationInstanceList = index.getAnnotations( annName );
@ -239,8 +250,7 @@ public class IndexBuilder {
interfaces[i] = DotName.createSimple( classInterfaces[i].getName() );
}
}
//todo how to get access_flag from a Class?
access_flag = 0x0001;
access_flag = (short) ( clazz.getModifiers() | 0x20 );//(modifiers | ACC_SUPER)
}
Map<DotName, List<AnnotationInstance>> map = new HashMap<DotName, List<AnnotationInstance>>();
classInfoAnnotationsMap.put( classDotName, map );

View File

@ -76,4 +76,8 @@ class ManyToManyMocker extends PropertyMocker {
protected XMLAccessType getAccessType() {
return manyToMany.getAccess();
}
@Override
protected void setAccessType(XMLAccessType accessType) {
manyToMany.setAccess( accessType );
}
}

View File

@ -72,5 +72,9 @@ class ManyToOneMocker extends PropertyMocker {
protected XMLAccessType getAccessType() {
return manyToOne.getAccess();
}
@Override
protected void setAccessType(XMLAccessType accessType) {
manyToOne.setAccess( accessType );
}
}

View File

@ -61,9 +61,6 @@ class MappedSuperclassMocker extends AbstractEntityObjectMocker {
if ( mappedSuperclass.isMetadataComplete() == null ) {
mappedSuperclass.setMetadataComplete( getDefaults().getMetadataComplete() );
}
if ( mappedSuperclass.getAccess() == null ) {
mappedSuperclass.setAccess( getDefaults().getAccess() );
}
LOG.debugf( "Adding XML overriding information for %s", className );
}

View File

@ -43,7 +43,6 @@ import org.hibernate.HibernateException;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.metamodel.source.annotation.xml.XMLCascadeType;
import org.hibernate.metamodel.source.annotations.JPADotNames;
import org.hibernate.metamodel.source.annotations.util.JandexHelper;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.classloading.spi.ClassLoaderService;
@ -267,33 +266,24 @@ public class MockHelper {
/**
* @param t1 can't be null
* @param t2 can't be null
* @param ignoreAccess true t1 and t2 can't be ClassInfo
*/
public static boolean targetEquals(AnnotationTarget t1, AnnotationTarget t2, boolean ignoreAccess) {
public static boolean targetEquals(AnnotationTarget t1, AnnotationTarget t2) {
if ( t1 == t2 ) {
return true;
}
if ( t1.getClass() == t2.getClass() ) {
if ( t1.getClass() == ClassInfo.class ) {
return ( (ClassInfo) t1 ).name().equals( ( (ClassInfo) t2 ).name() );
}
else if ( t1.getClass() == MethodInfo.class ) {
return ( (MethodInfo) t1 ).name().equals( ( (MethodInfo) t2 ).name() );
}
else {
return ( (FieldInfo) t1 ).name().equals( ( (FieldInfo) t2 ).name() );
}
}
if ( ignoreAccess && t1.getClass() != ClassInfo.class && t2.getClass() != ClassInfo.class ) {
if ( t1.getClass() == FieldInfo.class ) {
String fieldName = JandexHelper.getPropertyName( t2 );
return ( (FieldInfo) t1 ).name().equals( fieldName );
}
else {
String fieldName = JandexHelper.getPropertyName( t1 );
return ( (FieldInfo) t2 ).name().equals( fieldName );
}
if ( t1 != null && t2 != null ) {
if ( t1.getClass() == t2.getClass() ) {
if ( t1.getClass() == ClassInfo.class ) {
return ( (ClassInfo) t1 ).name().equals( ( (ClassInfo) t2 ).name() );
}
else if ( t1.getClass() == MethodInfo.class ) {
return ( (MethodInfo) t1 ).name().equals( ( (MethodInfo) t2 ).name() );
}
else {
return ( (FieldInfo) t1 ).name().equals( ( (FieldInfo) t2 ).name() );
}
}
}
return false;
}

View File

@ -78,4 +78,8 @@ class OneToManyMocker extends PropertyMocker {
protected XMLAccessType getAccessType() {
return oneToMany.getAccess();
}
@Override
protected void setAccessType(XMLAccessType accessType) {
oneToMany.setAccess( accessType );
}
}

View File

@ -76,4 +76,8 @@ class OneToOneMocker extends PropertyMocker {
protected XMLAccessType getAccessType() {
return oneToOne.getAccess();
}
@Override
protected void setAccessType(XMLAccessType accessType) {
oneToOne.setAccess( accessType );
}
}

View File

@ -46,12 +46,6 @@ import org.hibernate.metamodel.source.annotation.xml.XMLTemporalType;
*/
abstract class PropertyMocker extends AnnotationMocker {
protected ClassInfo classInfo;
protected XMLAccessType accessType;
private void setTarget(AnnotationTarget target) {
this.target = target;
}
private AnnotationTarget target;
PropertyMocker(IndexBuilder indexBuilder, ClassInfo classInfo, EntityMappingsMocker.Default defaults) {
@ -61,45 +55,73 @@ abstract class PropertyMocker extends AnnotationMocker {
protected abstract void processExtra();
@Override
protected AnnotationTarget getTarget() {
return target;
}
protected abstract String getFieldName();
protected abstract XMLAccessType getAccessType();
protected abstract void setAccessType(XMLAccessType accessType);
@Override
protected DotName getTargetName() {
return classInfo.name();
}
protected void resolveTarget() {
XMLAccessType accessType = getAccessType();
if ( accessType == null ) {
accessType = AccessHelper.getAccessFromAttributeAnnotation( getTargetName(), getFieldName(), indexBuilder );
if ( accessType == null ) {
accessType = AccessHelper.getEntityAccess( getTargetName(), indexBuilder );
}
if ( accessType == null ) {
accessType = AccessHelper.getAccessFromIdPosition( getTargetName(), indexBuilder );
}
if ( accessType == null ) {
accessType = XMLAccessType.PROPERTY;
}
setAccessType( accessType );
}
}
@Override
protected AnnotationTarget getTarget() {
if ( target == null ) {
target = getTargetFromAttributeAccessType( getAccessType() );
}
return target;
}
protected AnnotationTarget getTargetFromAttributeAccessType(XMLAccessType accessType) {
if ( accessType == null ) {
throw new IllegalArgumentException( "access type can't be null." );
}
switch ( accessType ) {
case FIELD:
return MockHelper.getTarget(
indexBuilder.getServiceRegistry(),
classInfo,
getFieldName(),
MockHelper.TargetType.FIELD
);
case PROPERTY:
return MockHelper.getTarget(
indexBuilder.getServiceRegistry(),
classInfo,
getFieldName(),
MockHelper.TargetType.PROPERTY
);
default:
throw new HibernateException( "can't determin access type [" + accessType + "]" );
}
}
@Override
final void process() {
processByTarget( MockHelper.TargetType.FIELD );
processByTarget( MockHelper.TargetType.PROPERTY );
}
private void processByTarget(MockHelper.TargetType type) {
boolean isTargetAvailable = false;
try {
setTarget(
MockHelper.getTarget(
indexBuilder.getServiceRegistry(), classInfo, getFieldName(), type
)
);
isTargetAvailable = true;
}
catch ( HibernateException e ) {
//ignore
e.printStackTrace();
}
if ( isTargetAvailable ) {
parserAccessType( getAccessType(), getTarget() );
processExtra();
}
resolveTarget();
processExtra();
}
protected AnnotationInstance parserMapKeyColumn(XMLMapKeyColumn mapKeyColumn, AnnotationTarget target) {

View File

@ -51,6 +51,10 @@ class TransientMocker extends PropertyMocker {
@Override
protected XMLAccessType getAccessType() {
return null;
return XMLAccessType.FIELD;
}
@Override
protected void setAccessType(XMLAccessType accessType) {
//ignore
}
}

View File

@ -55,6 +55,10 @@ class VersionMocker extends PropertyMocker {
protected XMLAccessType getAccessType() {
return version.getAccess();
}
@Override
protected void setAccessType(XMLAccessType accessType) {
version.setAccess( accessType );
}
}

View File

@ -23,14 +23,15 @@ public class BasicMockerTest extends AbstractMockerTest {
XMLEntity entity = createEntity();
IndexBuilder indexBuilder = getIndexBuilder();
EntityMocker entityMocker = new EntityMocker( indexBuilder, entity, new EntityMappingsMocker.Default() );
entityMocker.preProcess();
entityMocker.process();
Index index = indexBuilder.build( new EntityMappingsMocker.Default() );
assertEquals( 1, index.getKnownClasses().size() );
DotName itemName = DotName.createSimple( Item.class.getName() );
assertHasAnnotation( index, itemName, JPADotNames.ENTITY );
assertHasAnnotation( index, itemName, JPADotNames.ID, 2 );
assertHasAnnotation( index, itemName, JPADotNames.GENERATED_VALUE, 2 );
assertHasAnnotation( index, itemName, JPADotNames.ID );
assertHasAnnotation( index, itemName, JPADotNames.GENERATED_VALUE );
}
@Test
@ -44,6 +45,7 @@ public class BasicMockerTest extends AbstractMockerTest {
defaults.setSchema( "HIBERNATE_SCHEMA" );
defaults.setCatalog( "HIBERNATE_CATALOG" );
EntityMocker entityMocker = new EntityMocker( indexBuilder, entity, defaults );
entityMocker.preProcess();
entityMocker.process();
Index index = indexBuilder.build( new EntityMappingsMocker.Default() );

View File

@ -40,6 +40,7 @@ public class OverrideTest extends AbstractMockerTest {
EntityMappingsMocker.Default defaults = new EntityMappingsMocker.Default();
defaults.setMetadataComplete( true );
EntityMocker entityMocker = new EntityMocker( indexBuilder, author, defaults );
entityMocker.preProcess();
entityMocker.process();
Index index = indexBuilder.build( new EntityMappingsMocker.Default() );
DotName className = DotName.createSimple( Author.class.getName() );
@ -80,6 +81,7 @@ public class OverrideTest extends AbstractMockerTest {
EntityMappingsMocker.Default defaults = new EntityMappingsMocker.Default();
defaults.setCascadePersist( true );
EntityMocker entityMocker = new EntityMocker( indexBuilder, author, defaults );
entityMocker.preProcess();
entityMocker.process();
Index index = indexBuilder.build( new EntityMappingsMocker.Default() );
DotName className = DotName.createSimple( Author.class.getName() );
@ -97,7 +99,6 @@ public class OverrideTest extends AbstractMockerTest {
index,
className,
JPADotNames.ONE_TO_MANY,
2,
new CascadeAnnotationValueChecker( new String[] { "PERSIST", "ALL" } )
);
}
@ -139,7 +140,7 @@ public class OverrideTest extends AbstractMockerTest {
assertAnnotationValue(
index,
className,
JPADotNames.ATTRIBUTE_OVERRIDES, 2, new AnnotationValueChecker() {
JPADotNames.ATTRIBUTE_OVERRIDES, new AnnotationValueChecker() {
@Override
public void check(AnnotationInstance annotationInstance) {
AnnotationValue value = annotationInstance.value();
@ -167,6 +168,7 @@ public class OverrideTest extends AbstractMockerTest {
@Test
public void testSchemaInPersistenceMetadata() {
Index index =getMockedIndex( "default-schema.xml" );
index.printAnnotations();
//Global Configuration should be accessed like this, not from ClassInfo
List<AnnotationInstance> annotationInstanceList = index.getAnnotations( JPADotNames.TABLE_GENERATOR );
assertNotNull( annotationInstanceList );
@ -200,6 +202,7 @@ public class OverrideTest extends AbstractMockerTest {
@Test
public void testSchemaInEntityMapping() {
Index index =getMockedIndex( "default-schema2.xml" );
index.printAnnotations();
//Global Configuration should be accessed like this, not from ClassInfo
List<AnnotationInstance> annotationInstanceList = index.getAnnotations( JPADotNames.TABLE_GENERATOR );
assertNotNull( annotationInstanceList );