diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/MockedNames.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/MockedNames.java new file mode 100644 index 0000000000..f8104e4f1d --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/MockedNames.java @@ -0,0 +1,19 @@ +package org.hibernate.metamodel.source.annotations.xml; + +import org.jboss.jandex.DotName; + +/** + * @author Strong Liu + */ +public interface MockedNames { + public static final DotName DEFAULT_ACCESS = DotName.createSimple( "default.access" ); + public static final DotName DEFAULT_DELIMITED_IDENTIFIERS = DotName.createSimple( "default.delimited.identifiers" ); + public static final DotName DEFAULT_ENTITY_LISTENERS = DotName.createSimple( "default.entity.listeners" ); + public static final DotName DEFAULT_POST_LOAD = DotName.createSimple( "default.entity.listener.post.load" ); + public static final DotName DEFAULT_POST_PERSIST = DotName.createSimple( "default.entity.listener.post.persist" ); + public static final DotName DEFAULT_POST_REMOVE = DotName.createSimple( "default.entity.listener.post.remove" ); + public static final DotName DEFAULT_POST_UPDATE = DotName.createSimple( "default.entity.listener.post.update" ); + public static final DotName DEFAULT_PRE_PERSIST = DotName.createSimple( "default.entity.listener.pre.persist" ); + public static final DotName DEFAULT_PRE_REMOVE = DotName.createSimple( "default.entity.listener.pre.remove" ); + public static final DotName DEFAULT_PRE_UPDATE = DotName.createSimple( "default.entity.listener.pre.update" ); +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/mocker/AbstractMocker.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/mocker/AbstractMocker.java index fcf4e9872a..4853374329 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/mocker/AbstractMocker.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/mocker/AbstractMocker.java @@ -24,29 +24,14 @@ package org.hibernate.metamodel.source.annotations.xml.mocker; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; -import java.util.Map; -import java.util.Set; import org.jboss.jandex.AnnotationInstance; import org.jboss.jandex.AnnotationTarget; import org.jboss.jandex.AnnotationValue; import org.jboss.jandex.DotName; -import org.hibernate.AssertionFailure; import org.hibernate.metamodel.source.annotation.xml.XMLAccessType; -import org.hibernate.metamodel.source.annotation.xml.XMLAssociationOverride; -import org.hibernate.metamodel.source.annotation.xml.XMLAttributeOverride; -import org.hibernate.metamodel.source.annotation.xml.XMLCollectionTable; -import org.hibernate.metamodel.source.annotation.xml.XMLColumn; -import org.hibernate.metamodel.source.annotation.xml.XMLEnumType; -import org.hibernate.metamodel.source.annotation.xml.XMLJoinColumn; -import org.hibernate.metamodel.source.annotation.xml.XMLJoinTable; -import org.hibernate.metamodel.source.annotation.xml.XMLLob; -import org.hibernate.metamodel.source.annotation.xml.XMLOrderColumn; -import org.hibernate.metamodel.source.annotation.xml.XMLPrimaryKeyJoinColumn; -import org.hibernate.metamodel.source.annotation.xml.XMLTemporalType; import org.hibernate.metamodel.source.annotation.xml.XMLUniqueConstraint; import org.hibernate.metamodel.source.annotations.JPADotNames; @@ -62,45 +47,9 @@ abstract class AbstractMocker implements JPADotNames { this.indexBuilder = indexBuilder; } - abstract protected EntityMappingsMocker.Default getDefaults(); - /** - * @return DotName as the key for the ClassInfo object this mocker created that being push to. - */ - abstract protected DotName getTargetName(); + abstract protected AnnotationInstance push(AnnotationInstance annotationInstance); - /** - * @return Default Annotation Target for #{create} and #{mocker} methods. - */ - abstract protected AnnotationTarget getTarget(); - - protected AnnotationInstance push(AnnotationInstance annotationInstance) { - if ( annotationInstance != null ) { - indexBuilder.addAnnotationInstance( getTargetName(), annotationInstance ); - } - return annotationInstance; - } - - /** - * Create simple AnnotationInstance with empty annotation value. - * AnnotationInstance's target is get from #{getTarget} - * - * @param name annotation name - * - * @return annotationInstance which name is , target is from #{getTarget}, and has no annotation values. - */ - protected AnnotationInstance create(DotName name) { - return create( name, MockHelper.EMPTY_ANNOTATION_VALUE_ARRAY ); - } - - protected AnnotationInstance create(DotName name, AnnotationValue[] annotationValues) { - return create( name, getTarget(), annotationValues ); - - } - - protected AnnotationInstance create(DotName name, List annotationValueList) { - return create( name, getTarget(), annotationValueList ); - } protected AnnotationInstance create(DotName name, AnnotationTarget target) { return create( name, target, MockHelper.EMPTY_ANNOTATION_VALUE_ARRAY ); @@ -113,335 +62,24 @@ abstract class AbstractMocker implements JPADotNames { protected AnnotationInstance create(DotName name, AnnotationTarget target, AnnotationValue[] annotationValues) { AnnotationInstance annotationInstance = MockHelper.create( name, target, annotationValues ); - if ( target != null ) { - push( annotationInstance ); - } + push( annotationInstance ); return annotationInstance; } - //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - //@Column - protected AnnotationInstance parserColumn(XMLColumn column, AnnotationTarget target) { - if ( column == null ) { + + protected AnnotationInstance parserAccessType(XMLAccessType accessType, AnnotationTarget target) { + if ( accessType == null ) { return null; } - List annotationValueList = new ArrayList(); - MockHelper.stringValue( "name", column.getName(), annotationValueList ); - MockHelper.stringValue( "columnDefinition", column.getColumnDefinition(), annotationValueList ); - MockHelper.stringValue( "table", column.getTable(), annotationValueList ); - MockHelper.booleanValue( "unique", column.isUnique(), annotationValueList ); - MockHelper.booleanValue( "nullable", column.isNullable(), annotationValueList ); - MockHelper.booleanValue( "insertable", column.isInsertable(), annotationValueList ); - MockHelper.booleanValue( "updatable", column.isUpdatable(), annotationValueList ); - MockHelper.integerValue( "length", column.getLength(), annotationValueList ); - MockHelper.integerValue( "precision", column.getPrecision(), annotationValueList ); - MockHelper.integerValue( "scale", column.getScale(), annotationValueList ); - return create( COLUMN, target, annotationValueList ); - } - - protected AnnotationInstance parserTemporalType(XMLTemporalType temporalType, AnnotationTarget target) { - if ( temporalType == null ) { - return null; - } - return create( TEMPORAL, target, MockHelper.enumValueArray( "value", TEMPORAL_TYPE, temporalType ) ); - } - - protected AnnotationInstance parserEnumType(XMLEnumType enumerated, AnnotationTarget target) { - if ( enumerated == null ) { - return null; - } - return create( ENUMERATED, target, MockHelper.enumValueArray( "value", ENUM_TYPE, enumerated ) ); - } - - //@AttributeOverride - private AnnotationInstance parserAttributeOverride(XMLAttributeOverride attributeOverride, AnnotationTarget target) { - if ( attributeOverride == null ) { - return null; - } - List annotationValueList = new ArrayList(); - MockHelper.stringValue( "name", attributeOverride.getName(), annotationValueList ); - if ( attributeOverride instanceof XMLAttributeOverrideProxy ) { - XMLAttributeOverrideProxy proxy = (XMLAttributeOverrideProxy) attributeOverride; - MockHelper.addToCollectionIfNotNull( annotationValueList, proxy.getColumnAnnotationValue() ); - } - else { - MockHelper.nestedAnnotationValue( - "column", parserColumn( attributeOverride.getColumn(), null ), annotationValueList - ); - } - return - create( - ATTRIBUTE_OVERRIDE, target, annotationValueList - - ); - } - - protected AnnotationInstance parserPrimaryKeyJoinColumn(XMLPrimaryKeyJoinColumn primaryKeyJoinColumn, AnnotationTarget target) { - if ( primaryKeyJoinColumn == null ) { - return null; - } - List annotationValueList = new ArrayList(); - MockHelper.stringValue( "name", primaryKeyJoinColumn.getName(), annotationValueList ); - MockHelper.stringValue( - "referencedColumnName", primaryKeyJoinColumn.getReferencedColumnName(), annotationValueList - ); - MockHelper.stringValue( - "columnDefinition", primaryKeyJoinColumn.getColumnDefinition(), annotationValueList - ); - return - create( - PRIMARY_KEY_JOIN_COLUMN, target, annotationValueList - - ); - } - - protected AnnotationInstance parserPrimaryKeyJoinColumnList(List primaryKeyJoinColumnList, AnnotationTarget target) { - if ( MockHelper.isNotEmpty( primaryKeyJoinColumnList ) ) { - if ( primaryKeyJoinColumnList.size() == 1 ) { - return parserPrimaryKeyJoinColumn( primaryKeyJoinColumnList.get( 0 ), target ); - } - else { - return create( - PRIMARY_KEY_JOIN_COLUMNS, - target, - nestedPrimaryKeyJoinColumnList( "value", primaryKeyJoinColumnList, null ) - ); - } - } - - return null; - - } - - protected AnnotationValue[] nestedPrimaryKeyJoinColumnList(String name, List constraints, List annotationValueList) { - if ( MockHelper.isNotEmpty( constraints ) ) { - AnnotationValue[] values = new AnnotationValue[constraints.size()]; - for ( int i = 0; i < constraints.size(); i++ ) { - AnnotationInstance annotationInstance = parserPrimaryKeyJoinColumn( constraints.get( i ), null ); - values[i] = MockHelper.nestedAnnotationValue( - "", annotationInstance - ); - } - MockHelper.addToCollectionIfNotNull( - annotationValueList, AnnotationValue.createArrayValue( name, values ) - ); - return values; - } - return MockHelper.EMPTY_ANNOTATION_VALUE_ARRAY; - - } - - protected void getAnnotationInstanceByTarget(DotName annName, AnnotationTarget target, Operation operation) { - Map> annotatedMap = indexBuilder.getIndexedAnnotations( getTargetName() ); - if ( !annotatedMap.containsKey( annName ) ) { - return; - } - List annotationInstanceList = annotatedMap.get( annName ); - if ( MockHelper.isNotEmpty( annotationInstanceList ) ) { - for ( AnnotationInstance annotationInstance : annotationInstanceList ) { - AnnotationTarget annotationTarget = annotationInstance.target(); - if ( MockHelper.targetEquals( target, annotationTarget ) ) { - if ( operation.process( annotationInstance ) ) { - return; - } - } - } - } - } - - protected interface Operation { - boolean process(AnnotationInstance annotationInstance); - } - - class ContainerOperation implements Operation { - private Operation child; - - ContainerOperation(Operation child) { - this.child = child; - } - - @Override - public boolean process(AnnotationInstance annotationInstance) { - AnnotationValue value = annotationInstance.value(); - AnnotationInstance[] indexedAttributeOverridesValues = value.asNestedArray(); - for ( AnnotationInstance ai : indexedAttributeOverridesValues ) { - child.process( ai ); - } - return true; - } - } - - class AttributeOverrideOperation implements Operation { - private Set names; - private List attributeOverrides; - - AttributeOverrideOperation(Set names, List attributeOverrides) { - this.names = names; - this.attributeOverrides = attributeOverrides; - } - - @Override - public boolean process(AnnotationInstance annotationInstance) { - String name = annotationInstance.value( "name" ).asString(); - if ( !names.contains( name ) ) { - XMLAttributeOverrideProxy attributeOverride = new XMLAttributeOverrideProxy(); - attributeOverride.setName( name ); - attributeOverride.setColumnAnnotationValue( annotationInstance.value( "column" ) ); - attributeOverrides.add( attributeOverride ); - } - return false; - } - } - - protected AnnotationInstance parserAttributeOverrides(List attributeOverrides, AnnotationTarget target) { - if ( target == null ) { - throw new AssertionFailure( "target can not be null" ); - } - if ( attributeOverrides == null || attributeOverrides.isEmpty() ) { - return null; - } - Set names = new HashSet(); - for ( XMLAttributeOverride attributeOverride : attributeOverrides ) { - names.add( attributeOverride.getName() ); - } - Operation operation = new AttributeOverrideOperation( names, attributeOverrides ); - getAnnotationInstanceByTarget( - ATTRIBUTE_OVERRIDES, target, new ContainerOperation( operation ) - ); - getAnnotationInstanceByTarget( - ATTRIBUTE_OVERRIDE, target, operation - ); - if ( attributeOverrides.size() == 1 ) { - return parserAttributeOverride( attributeOverrides.get( 0 ), target ); - } - else { - AnnotationValue[] values = new AnnotationValue[attributeOverrides.size()]; - for ( int i = 0; i < values.length; i++ ) { - values[i] = MockHelper.nestedAnnotationValue( - "", parserAttributeOverride( attributeOverrides.get( i ), null ) - ); - } - return create( - ATTRIBUTE_OVERRIDES, - target, - new AnnotationValue[] { AnnotationValue.createArrayValue( "value", values ) } - ); - } - } - - class AssociationOverrideOperation implements Operation { - private Set names; - private List associationOverrides; - - AssociationOverrideOperation(Set names, List associationOverrides) { - this.names = names; - this.associationOverrides = associationOverrides; - } - - @Override - public boolean process(AnnotationInstance annotationInstance) { - String name = annotationInstance.value( "name" ).asString(); - if ( !names.contains( name ) ) { - XMLAssociationOverrideProxy associationOverride = new XMLAssociationOverrideProxy(); - associationOverride.setName( name ); - associationOverride.setJoinColumnsAnnotationValue( annotationInstance.value( "joinColumns" ) ); - associationOverride.setJoinTableAnnotationValue( annotationInstance.value( "joinTable" ) ); - associationOverrides.add( associationOverride ); - } - return false; - } - - } - - protected AnnotationInstance parserAssociationOverrides(List associationOverrides, AnnotationTarget target) { - if ( target == null ) { - throw new AssertionFailure( "target can not be null" ); - } - if ( associationOverrides == null || associationOverrides.isEmpty() ) { - return null; - } - - Set names = new HashSet(); - for ( XMLAssociationOverride associationOverride : associationOverrides ) { - names.add( associationOverride.getName() ); - } - Operation operation = new AssociationOverrideOperation( names, associationOverrides ); - getAnnotationInstanceByTarget( - ASSOCIATION_OVERRIDES, target, new ContainerOperation( operation ) - ); - getAnnotationInstanceByTarget( - ASSOCIATION_OVERRIDE, target, operation - ); - - - if ( associationOverrides.size() == 1 ) { - return parserAssociationOverride( associationOverrides.get( 0 ), target ); - } - else { - AnnotationValue[] values = new AnnotationValue[associationOverrides.size()]; - for ( int i = 0; i < values.length; i++ ) { - values[i] = MockHelper.nestedAnnotationValue( - "", parserAssociationOverride( associationOverrides.get( i ), null ) - ); - } - return create( - ASSOCIATION_OVERRIDES, - target, - new AnnotationValue[] { AnnotationValue.createArrayValue( "value", values ) } - ); - } - - } - - //@AssociationOverride - private AnnotationInstance parserAssociationOverride(XMLAssociationOverride associationOverride, AnnotationTarget target) { - if ( associationOverride == null ) { - return null; - } - List annotationValueList = new ArrayList(); - MockHelper.stringValue( "name", associationOverride.getName(), annotationValueList ); - if ( associationOverride instanceof XMLAssociationOverrideProxy ) { - XMLAssociationOverrideProxy proxy = (XMLAssociationOverrideProxy) associationOverride; - MockHelper.addToCollectionIfNotNull( annotationValueList, proxy.getJoinColumnsAnnotationValue() ); - MockHelper.addToCollectionIfNotNull( annotationValueList, proxy.getJoinTableAnnotationValue() ); - } - else { - nestedJoinColumnList( - "joinColumns", associationOverride.getJoinColumn(), annotationValueList - ); - MockHelper.nestedAnnotationValue( - "joinTable", parserJoinTable( associationOverride.getJoinTable(), null ), annotationValueList - ); - } - return create( ASSOCIATION_OVERRIDE, target, annotationValueList ); - } - - //@JoinTable - protected AnnotationInstance parserJoinTable(XMLJoinTable joinTable, AnnotationTarget target) { - if ( joinTable == null ) { - return null; - } - MockHelper.updateSchema( new SchemaAware.JoinTableSchemaAware( joinTable ), getDefaults() ); - List annotationValueList = new ArrayList(); - MockHelper.stringValue( "name", joinTable.getName(), annotationValueList ); - MockHelper.stringValue( "catalog", joinTable.getCatalog(), annotationValueList ); - MockHelper.stringValue( "schema", joinTable.getSchema(), annotationValueList ); - nestedJoinColumnList( "joinColumns", joinTable.getJoinColumn(), annotationValueList ); - nestedJoinColumnList( - "inverseJoinColumns", joinTable.getInverseJoinColumn(), annotationValueList - ); - nestedUniqueConstraintList( - "uniqueConstraints", joinTable.getUniqueConstraint(), annotationValueList - ); - return create( JOIN_TABLE, target, annotationValueList ); + return create( ACCESS, target, MockHelper.enumValueArray( "value", ACCESS_TYPE, accessType ) ); } protected void nestedUniqueConstraintList(String name, List constraints, List annotationValueList) { if ( MockHelper.isNotEmpty( constraints ) ) { AnnotationValue[] values = new AnnotationValue[constraints.size()]; for ( int i = 0; i < constraints.size(); i++ ) { - AnnotationInstance annotationInstance = parserUniqueConstraint( constraints.get( i ) ); + AnnotationInstance annotationInstance = parserUniqueConstraint( constraints.get( i ), null ); values[i] = MockHelper.nestedAnnotationValue( "", annotationInstance ); @@ -454,7 +92,7 @@ abstract class AbstractMocker implements JPADotNames { } //@UniqueConstraint - protected AnnotationInstance parserUniqueConstraint(XMLUniqueConstraint uniqueConstraint) { + protected AnnotationInstance parserUniqueConstraint(XMLUniqueConstraint uniqueConstraint, AnnotationTarget target) { if ( uniqueConstraint == null ) { return null; } @@ -463,105 +101,10 @@ abstract class AbstractMocker implements JPADotNames { MockHelper.stringArrayValue( "columnNames", uniqueConstraint.getColumnName(), annotationValueList ); return create( - UNIQUE_CONSTRAINT, + UNIQUE_CONSTRAINT, target, annotationValueList ); } - - protected AnnotationInstance parserAccessType(XMLAccessType accessType, AnnotationTarget target) { - if ( accessType == null ) { - return null; - } - return create( ACCESS, target, MockHelper.enumValueArray( "value", ACCESS_TYPE, accessType ) ); - } - - protected AnnotationInstance parserCollectionTable(XMLCollectionTable collectionTable, AnnotationTarget target) { - if ( collectionTable == null ) { - return null; - } - MockHelper.updateSchema( new SchemaAware.CollectionTableSchemaAware( collectionTable ), getDefaults() ); - List annotationValueList = new ArrayList(); - MockHelper.stringValue( "name", collectionTable.getName(), annotationValueList ); - MockHelper.stringValue( "catalog", collectionTable.getCatalog(), annotationValueList ); - MockHelper.stringValue( "schema", collectionTable.getSchema(), annotationValueList ); - nestedJoinColumnList( "joinColumns", collectionTable.getJoinColumn(), annotationValueList ); - nestedUniqueConstraintList( "uniqueConstraints", collectionTable.getUniqueConstraint(), annotationValueList ); - return create( COLLECTION_TABLE, target, annotationValueList ); - } - - private AnnotationValue[] nestedJoinColumnList(String name, List columns, List annotationValueList) { - if ( MockHelper.isNotEmpty( columns ) ) { - AnnotationValue[] values = new AnnotationValue[columns.size()]; - for ( int i = 0; i < columns.size(); i++ ) { - AnnotationInstance annotationInstance = parserJoinColumn( columns.get( i ), null ); - values[i] = MockHelper.nestedAnnotationValue( - "", annotationInstance - ); - } - MockHelper.addToCollectionIfNotNull( - annotationValueList, AnnotationValue.createArrayValue( name, values ) - ); - return values; - } - return MockHelper.EMPTY_ANNOTATION_VALUE_ARRAY; - } - - protected AnnotationInstance parserJoinColumnList(List joinColumnList, AnnotationTarget target) { - if ( MockHelper.isNotEmpty( joinColumnList ) ) { - if ( joinColumnList.size() == 1 ) { - return parserJoinColumn( joinColumnList.get( 0 ), target ); - } - else { - AnnotationValue[] values = nestedJoinColumnList( "value", joinColumnList, null ); - return create( - JOIN_COLUMNS, - target, - values - ); - } - } - return null; - - } - - protected AnnotationInstance parserOrderColumn(XMLOrderColumn orderColumn, AnnotationTarget target) { - if ( orderColumn == null ) { - return null; - } - List annotationValueList = new ArrayList(); - MockHelper.stringValue( "name", orderColumn.getName(), annotationValueList ); - MockHelper.stringValue( "columnDefinition", orderColumn.getColumnDefinition(), annotationValueList ); - MockHelper.booleanValue( "nullable", orderColumn.isNullable(), annotationValueList ); - MockHelper.booleanValue( "insertable", orderColumn.isInsertable(), annotationValueList ); - MockHelper.booleanValue( "updatable", orderColumn.isUpdatable(), annotationValueList ); - return create( ORDER_COLUMN, target, annotationValueList ); - } - - //@JoinColumn - protected AnnotationInstance parserJoinColumn(XMLJoinColumn column, AnnotationTarget target) { - if ( column == null ) { - return null; - } - List annotationValueList = new ArrayList(); - MockHelper.stringValue( "name", column.getName(), annotationValueList ); - MockHelper.stringValue( "columnDefinition", column.getColumnDefinition(), annotationValueList ); - MockHelper.stringValue( "table", column.getTable(), annotationValueList ); - MockHelper.stringValue( - "referencedColumnName", column.getReferencedColumnName(), annotationValueList - ); - MockHelper.booleanValue( "unique", column.isUnique(), annotationValueList ); - MockHelper.booleanValue( "nullable", column.isNullable(), annotationValueList ); - MockHelper.booleanValue( "insertable", column.isInsertable(), annotationValueList ); - MockHelper.booleanValue( "updatable", column.isUpdatable(), annotationValueList ); - return create( JOIN_COLUMN, target, annotationValueList ); - } - - protected AnnotationInstance parserLob(XMLLob lob, AnnotationTarget target) { - if ( lob == null ) { - return null; - } - return create( LOB, target ); - } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/mocker/AnnotationMocker.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/mocker/AnnotationMocker.java index b891f581e1..bae5ebc54e 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/mocker/AnnotationMocker.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/mocker/AnnotationMocker.java @@ -23,6 +23,30 @@ */ package org.hibernate.metamodel.source.annotations.xml.mocker; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.jboss.jandex.AnnotationInstance; +import org.jboss.jandex.AnnotationTarget; +import org.jboss.jandex.AnnotationValue; +import org.jboss.jandex.DotName; + +import org.hibernate.AssertionFailure; +import org.hibernate.metamodel.source.annotation.xml.XMLAssociationOverride; +import org.hibernate.metamodel.source.annotation.xml.XMLAttributeOverride; +import org.hibernate.metamodel.source.annotation.xml.XMLCollectionTable; +import org.hibernate.metamodel.source.annotation.xml.XMLColumn; +import org.hibernate.metamodel.source.annotation.xml.XMLEnumType; +import org.hibernate.metamodel.source.annotation.xml.XMLJoinColumn; +import org.hibernate.metamodel.source.annotation.xml.XMLJoinTable; +import org.hibernate.metamodel.source.annotation.xml.XMLLob; +import org.hibernate.metamodel.source.annotation.xml.XMLOrderColumn; +import org.hibernate.metamodel.source.annotation.xml.XMLPrimaryKeyJoinColumn; +import org.hibernate.metamodel.source.annotation.xml.XMLTemporalType; + /** * @author Strong Liu */ @@ -36,7 +60,6 @@ abstract class AnnotationMocker extends AbstractMocker { abstract void process(); - @Override protected EntityMappingsMocker.Default getDefaults() { return defaults; } @@ -44,4 +67,452 @@ abstract class AnnotationMocker extends AbstractMocker { protected boolean isDefaultCascadePersist() { return defaults.getCascadePersist() != null && defaults.getCascadePersist(); } + + //@JoinTable + protected AnnotationInstance parserJoinTable(XMLJoinTable joinTable, AnnotationTarget target) { + if ( joinTable == null ) { + return null; + } + MockHelper.updateSchema( new SchemaAware.JoinTableSchemaAware( joinTable ), getDefaults() ); + List annotationValueList = new ArrayList(); + MockHelper.stringValue( "name", joinTable.getName(), annotationValueList ); + MockHelper.stringValue( "catalog", joinTable.getCatalog(), annotationValueList ); + MockHelper.stringValue( "schema", joinTable.getSchema(), annotationValueList ); + nestedJoinColumnList( "joinColumns", joinTable.getJoinColumn(), annotationValueList ); + nestedJoinColumnList( + "inverseJoinColumns", joinTable.getInverseJoinColumn(), annotationValueList + ); + nestedUniqueConstraintList( + "uniqueConstraints", joinTable.getUniqueConstraint(), annotationValueList + ); + return create( JOIN_TABLE, target, annotationValueList ); + } + + //@AssociationOverride + private AnnotationInstance parserAssociationOverride(XMLAssociationOverride associationOverride, AnnotationTarget target) { + if ( associationOverride == null ) { + return null; + } + List annotationValueList = new ArrayList(); + MockHelper.stringValue( "name", associationOverride.getName(), annotationValueList ); + if ( associationOverride instanceof XMLAssociationOverrideProxy ) { + XMLAssociationOverrideProxy proxy = (XMLAssociationOverrideProxy) associationOverride; + MockHelper.addToCollectionIfNotNull( annotationValueList, proxy.getJoinColumnsAnnotationValue() ); + MockHelper.addToCollectionIfNotNull( annotationValueList, proxy.getJoinTableAnnotationValue() ); + } + else { + nestedJoinColumnList( + "joinColumns", associationOverride.getJoinColumn(), annotationValueList + ); + MockHelper.nestedAnnotationValue( + "joinTable", parserJoinTable( associationOverride.getJoinTable(), null ), annotationValueList + ); + } + return create( ASSOCIATION_OVERRIDE, target, annotationValueList ); + } + + private AnnotationValue[] nestedJoinColumnList(String name, List columns, List annotationValueList) { + if ( MockHelper.isNotEmpty( columns ) ) { + AnnotationValue[] values = new AnnotationValue[columns.size()]; + for ( int i = 0; i < columns.size(); i++ ) { + AnnotationInstance annotationInstance = parserJoinColumn( columns.get( i ), null ); + values[i] = MockHelper.nestedAnnotationValue( + "", annotationInstance + ); + } + MockHelper.addToCollectionIfNotNull( + annotationValueList, AnnotationValue.createArrayValue( name, values ) + ); + return values; + } + return MockHelper.EMPTY_ANNOTATION_VALUE_ARRAY; + } + + //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + //@Column + protected AnnotationInstance parserColumn(XMLColumn column, AnnotationTarget target) { + if ( column == null ) { + return null; + } + List annotationValueList = new ArrayList(); + MockHelper.stringValue( "name", column.getName(), annotationValueList ); + MockHelper.stringValue( "columnDefinition", column.getColumnDefinition(), annotationValueList ); + MockHelper.stringValue( "table", column.getTable(), annotationValueList ); + MockHelper.booleanValue( "unique", column.isUnique(), annotationValueList ); + MockHelper.booleanValue( "nullable", column.isNullable(), annotationValueList ); + MockHelper.booleanValue( "insertable", column.isInsertable(), annotationValueList ); + MockHelper.booleanValue( "updatable", column.isUpdatable(), annotationValueList ); + MockHelper.integerValue( "length", column.getLength(), annotationValueList ); + MockHelper.integerValue( "precision", column.getPrecision(), annotationValueList ); + MockHelper.integerValue( "scale", column.getScale(), annotationValueList ); + return create( COLUMN, target, annotationValueList ); + } + + //@AttributeOverride + private AnnotationInstance parserAttributeOverride(XMLAttributeOverride attributeOverride, AnnotationTarget target) { + if ( attributeOverride == null ) { + return null; + } + List annotationValueList = new ArrayList(); + MockHelper.stringValue( "name", attributeOverride.getName(), annotationValueList ); + if ( attributeOverride instanceof XMLAttributeOverrideProxy ) { + XMLAttributeOverrideProxy proxy = (XMLAttributeOverrideProxy) attributeOverride; + MockHelper.addToCollectionIfNotNull( annotationValueList, proxy.getColumnAnnotationValue() ); + } + else { + MockHelper.nestedAnnotationValue( + "column", parserColumn( attributeOverride.getColumn(), null ), annotationValueList + ); + } + return + create( + ATTRIBUTE_OVERRIDE, target, annotationValueList + + ); + } + + + protected AnnotationInstance parserOrderColumn(XMLOrderColumn orderColumn, AnnotationTarget target) { + if ( orderColumn == null ) { + return null; + } + List annotationValueList = new ArrayList(); + MockHelper.stringValue( "name", orderColumn.getName(), annotationValueList ); + MockHelper.stringValue( "columnDefinition", orderColumn.getColumnDefinition(), annotationValueList ); + MockHelper.booleanValue( "nullable", orderColumn.isNullable(), annotationValueList ); + MockHelper.booleanValue( "insertable", orderColumn.isInsertable(), annotationValueList ); + MockHelper.booleanValue( "updatable", orderColumn.isUpdatable(), annotationValueList ); + return create( ORDER_COLUMN, target, annotationValueList ); + } + + //@JoinColumn + protected AnnotationInstance parserJoinColumn(XMLJoinColumn column, AnnotationTarget target) { + if ( column == null ) { + return null; + } + List annotationValueList = new ArrayList(); + MockHelper.stringValue( "name", column.getName(), annotationValueList ); + MockHelper.stringValue( "columnDefinition", column.getColumnDefinition(), annotationValueList ); + MockHelper.stringValue( "table", column.getTable(), annotationValueList ); + MockHelper.stringValue( + "referencedColumnName", column.getReferencedColumnName(), annotationValueList + ); + MockHelper.booleanValue( "unique", column.isUnique(), annotationValueList ); + MockHelper.booleanValue( "nullable", column.isNullable(), annotationValueList ); + MockHelper.booleanValue( "insertable", column.isInsertable(), annotationValueList ); + MockHelper.booleanValue( "updatable", column.isUpdatable(), annotationValueList ); + return create( JOIN_COLUMN, target, annotationValueList ); + } + + protected AnnotationInstance parserLob(XMLLob lob, AnnotationTarget target) { + if ( lob == null ) { + return null; + } + return create( LOB, target ); + } + + protected AnnotationInstance parserTemporalType(XMLTemporalType temporalType, AnnotationTarget target) { + if ( temporalType == null ) { + return null; + } + return create( TEMPORAL, target, MockHelper.enumValueArray( "value", TEMPORAL_TYPE, temporalType ) ); + } + + protected AnnotationInstance parserEnumType(XMLEnumType enumerated, AnnotationTarget target) { + if ( enumerated == null ) { + return null; + } + return create( ENUMERATED, target, MockHelper.enumValueArray( "value", ENUM_TYPE, enumerated ) ); + } + + + protected AnnotationInstance parserPrimaryKeyJoinColumn(XMLPrimaryKeyJoinColumn primaryKeyJoinColumn, AnnotationTarget target) { + if ( primaryKeyJoinColumn == null ) { + return null; + } + List annotationValueList = new ArrayList(); + MockHelper.stringValue( "name", primaryKeyJoinColumn.getName(), annotationValueList ); + MockHelper.stringValue( + "referencedColumnName", primaryKeyJoinColumn.getReferencedColumnName(), annotationValueList + ); + MockHelper.stringValue( + "columnDefinition", primaryKeyJoinColumn.getColumnDefinition(), annotationValueList + ); + return + create( + PRIMARY_KEY_JOIN_COLUMN, target, annotationValueList + + ); + } + + protected AnnotationInstance parserPrimaryKeyJoinColumnList(List primaryKeyJoinColumnList, AnnotationTarget target) { + if ( MockHelper.isNotEmpty( primaryKeyJoinColumnList ) ) { + if ( primaryKeyJoinColumnList.size() == 1 ) { + return parserPrimaryKeyJoinColumn( primaryKeyJoinColumnList.get( 0 ), target ); + } + else { + return create( + PRIMARY_KEY_JOIN_COLUMNS, + target, + nestedPrimaryKeyJoinColumnList( "value", primaryKeyJoinColumnList, null ) + ); + } + } + + return null; + + } + + protected AnnotationValue[] nestedPrimaryKeyJoinColumnList(String name, List constraints, List annotationValueList) { + if ( MockHelper.isNotEmpty( constraints ) ) { + AnnotationValue[] values = new AnnotationValue[constraints.size()]; + for ( int i = 0; i < constraints.size(); i++ ) { + AnnotationInstance annotationInstance = parserPrimaryKeyJoinColumn( constraints.get( i ), null ); + values[i] = MockHelper.nestedAnnotationValue( + "", annotationInstance + ); + } + MockHelper.addToCollectionIfNotNull( + annotationValueList, AnnotationValue.createArrayValue( name, values ) + ); + return values; + } + return MockHelper.EMPTY_ANNOTATION_VALUE_ARRAY; + + } + + protected void getAnnotationInstanceByTarget(DotName annName, AnnotationTarget target, Operation operation) { + Map> annotatedMap = indexBuilder.getIndexedAnnotations( getTargetName() ); + if ( !annotatedMap.containsKey( annName ) ) { + return; + } + List annotationInstanceList = annotatedMap.get( annName ); + if ( MockHelper.isNotEmpty( annotationInstanceList ) ) { + for ( AnnotationInstance annotationInstance : annotationInstanceList ) { + AnnotationTarget annotationTarget = annotationInstance.target(); + if ( MockHelper.targetEquals( target, annotationTarget ) ) { + if ( operation.process( annotationInstance ) ) { + return; + } + } + } + } + } + + + protected AnnotationInstance parserAttributeOverrides(List attributeOverrides, AnnotationTarget target) { + if ( target == null ) { + throw new AssertionFailure( "target can not be null" ); + } + if ( attributeOverrides == null || attributeOverrides.isEmpty() ) { + return null; + } + Set names = new HashSet(); + for ( XMLAttributeOverride attributeOverride : attributeOverrides ) { + names.add( attributeOverride.getName() ); + } + Operation operation = new AttributeOverrideOperation( names, attributeOverrides ); + getAnnotationInstanceByTarget( + ATTRIBUTE_OVERRIDES, target, new ContainerOperation( operation ) + ); + getAnnotationInstanceByTarget( + ATTRIBUTE_OVERRIDE, target, operation + ); + if ( attributeOverrides.size() == 1 ) { + return parserAttributeOverride( attributeOverrides.get( 0 ), target ); + } + else { + AnnotationValue[] values = new AnnotationValue[attributeOverrides.size()]; + for ( int i = 0; i < values.length; i++ ) { + values[i] = MockHelper.nestedAnnotationValue( + "", parserAttributeOverride( attributeOverrides.get( i ), null ) + ); + } + return create( + ATTRIBUTE_OVERRIDES, + target, + new AnnotationValue[] { AnnotationValue.createArrayValue( "value", values ) } + ); + } + } + + protected AnnotationInstance parserAssociationOverrides(List associationOverrides, AnnotationTarget target) { + if ( target == null ) { + throw new AssertionFailure( "target can not be null" ); + } + if ( associationOverrides == null || associationOverrides.isEmpty() ) { + return null; + } + + Set names = new HashSet(); + for ( XMLAssociationOverride associationOverride : associationOverrides ) { + names.add( associationOverride.getName() ); + } + Operation operation = new AssociationOverrideOperation( names, associationOverrides ); + getAnnotationInstanceByTarget( + ASSOCIATION_OVERRIDES, target, new ContainerOperation( operation ) + ); + getAnnotationInstanceByTarget( + ASSOCIATION_OVERRIDE, target, operation + ); + + + if ( associationOverrides.size() == 1 ) { + return parserAssociationOverride( associationOverrides.get( 0 ), target ); + } + else { + AnnotationValue[] values = new AnnotationValue[associationOverrides.size()]; + for ( int i = 0; i < values.length; i++ ) { + values[i] = MockHelper.nestedAnnotationValue( + "", parserAssociationOverride( associationOverrides.get( i ), null ) + ); + } + return create( + ASSOCIATION_OVERRIDES, + target, + new AnnotationValue[] { AnnotationValue.createArrayValue( "value", values ) } + ); + } + + } + + protected AnnotationInstance parserCollectionTable(XMLCollectionTable collectionTable, AnnotationTarget target) { + if ( collectionTable == null ) { + return null; + } + MockHelper.updateSchema( new SchemaAware.CollectionTableSchemaAware( collectionTable ), getDefaults() ); + List annotationValueList = new ArrayList(); + MockHelper.stringValue( "name", collectionTable.getName(), annotationValueList ); + MockHelper.stringValue( "catalog", collectionTable.getCatalog(), annotationValueList ); + MockHelper.stringValue( "schema", collectionTable.getSchema(), annotationValueList ); + nestedJoinColumnList( "joinColumns", collectionTable.getJoinColumn(), annotationValueList ); + nestedUniqueConstraintList( "uniqueConstraints", collectionTable.getUniqueConstraint(), annotationValueList ); + return create( COLLECTION_TABLE, target, annotationValueList ); + } + + + protected AnnotationInstance parserJoinColumnList(List joinColumnList, AnnotationTarget target) { + if ( MockHelper.isNotEmpty( joinColumnList ) ) { + if ( joinColumnList.size() == 1 ) { + return parserJoinColumn( joinColumnList.get( 0 ), target ); + } + else { + AnnotationValue[] values = nestedJoinColumnList( "value", joinColumnList, null ); + return create( + JOIN_COLUMNS, + target, + values + ); + } + } + return null; + + } + + protected interface Operation { + boolean process(AnnotationInstance annotationInstance); + } + + class ContainerOperation implements Operation { + private Operation child; + + ContainerOperation(Operation child) { + this.child = child; + } + + @Override + public boolean process(AnnotationInstance annotationInstance) { + AnnotationValue value = annotationInstance.value(); + AnnotationInstance[] indexedAttributeOverridesValues = value.asNestedArray(); + for ( AnnotationInstance ai : indexedAttributeOverridesValues ) { + child.process( ai ); + } + return true; + } + } + + class AttributeOverrideOperation implements Operation { + private Set names; + private List attributeOverrides; + + AttributeOverrideOperation(Set names, List attributeOverrides) { + this.names = names; + this.attributeOverrides = attributeOverrides; + } + + @Override + public boolean process(AnnotationInstance annotationInstance) { + String name = annotationInstance.value( "name" ).asString(); + if ( !names.contains( name ) ) { + XMLAttributeOverrideProxy attributeOverride = new XMLAttributeOverrideProxy(); + attributeOverride.setName( name ); + attributeOverride.setColumnAnnotationValue( annotationInstance.value( "column" ) ); + attributeOverrides.add( attributeOverride ); + } + return false; + } + } + + + class AssociationOverrideOperation implements Operation { + private Set names; + private List associationOverrides; + + AssociationOverrideOperation(Set names, List associationOverrides) { + this.names = names; + this.associationOverrides = associationOverrides; + } + + @Override + public boolean process(AnnotationInstance annotationInstance) { + String name = annotationInstance.value( "name" ).asString(); + if ( !names.contains( name ) ) { + XMLAssociationOverrideProxy associationOverride = new XMLAssociationOverrideProxy(); + associationOverride.setName( name ); + associationOverride.setJoinColumnsAnnotationValue( annotationInstance.value( "joinColumns" ) ); + associationOverride.setJoinTableAnnotationValue( annotationInstance.value( "joinTable" ) ); + associationOverrides.add( associationOverride ); + } + return false; + } + + } + + /** + * Create simple AnnotationInstance with empty annotation value. + * AnnotationInstance's target is get from #{getTarget} + * + * @param name annotation name + * + * @return annotationInstance which name is , target is from #{getTarget}, and has no annotation values. + */ + protected AnnotationInstance create(DotName name) { + return create( name, MockHelper.EMPTY_ANNOTATION_VALUE_ARRAY ); + } + + protected AnnotationInstance create(DotName name, AnnotationValue[] annotationValues) { + return create( name, getTarget(), annotationValues ); + + } + + protected AnnotationInstance create(DotName name, List annotationValueList) { + return create( name, getTarget(), annotationValueList ); + } + + /** + * @return DotName as the key for the ClassInfo object this mocker created that being push to. + */ + abstract protected DotName getTargetName(); + + /** + * @return Default Annotation Target for #{create} and #{mocker} methods. + */ + abstract protected AnnotationTarget getTarget(); + + protected AnnotationInstance push(AnnotationInstance annotationInstance) { + if ( annotationInstance != null && annotationInstance.target() != null ) { + indexBuilder.addAnnotationInstance( getTargetName(), annotationInstance ); + } + return annotationInstance; + } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/mocker/EntityMappingsMocker.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/mocker/EntityMappingsMocker.java index 794dd40141..a93057771d 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/mocker/EntityMappingsMocker.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/mocker/EntityMappingsMocker.java @@ -55,7 +55,8 @@ public class EntityMappingsMocker { //todo delimited-identifier private Default globalDefaults; final private IndexBuilder indexBuilder; - //todo + //todo default entity listeners + //todo default access type private XMLEntityListeners defaultEntityListeners; final private GlobalAnnotations globalAnnotations; @@ -73,7 +74,7 @@ public class EntityMappingsMocker { public Index mockNewIndex() { processPersistenceUnitMetadata( entityMappingsList ); processEntityMappings( entityMappingsList ); - processGlobalConfiguration(); + processGlobalAnnotations(); return indexBuilder.build( globalDefaults ); } @@ -101,6 +102,7 @@ public class EntityMappingsMocker { globalDefaults.setCascadePersist( pud.getCascadePersist() != null ); globalDefaults.setDelimitedIdentifiers( pud.getDelimitedIdentifiers() != null ); defaultEntityListeners = pud.getEntityListeners(); + new PersistenceMetadataMocker( indexBuilder, pud ).process(); } else { LOG.duplicateMetadata(); @@ -139,12 +141,12 @@ public class EntityMappingsMocker { } } - private void processGlobalConfiguration() { + private void processGlobalAnnotations() { if ( globalAnnotations.hasGlobalConfiguration() ) { indexBuilder.collectGlobalConfigurationFromIndex( globalAnnotations ); - new GlobalConfigurationMocker( + new GlobalAnnotationMocker( indexBuilder, globalAnnotations - ).parser(); + ).process(); } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/mocker/GlobalConfigurationMocker.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/mocker/GlobalAnnotationMocker.java similarity index 90% rename from hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/mocker/GlobalConfigurationMocker.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/mocker/GlobalAnnotationMocker.java index 0136343c3f..1723db0748 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/mocker/GlobalConfigurationMocker.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/mocker/GlobalAnnotationMocker.java @@ -29,9 +29,7 @@ import java.util.Iterator; import java.util.List; import org.jboss.jandex.AnnotationInstance; -import org.jboss.jandex.AnnotationTarget; import org.jboss.jandex.AnnotationValue; -import org.jboss.jandex.DotName; import org.hibernate.metamodel.source.annotation.xml.XMLColumnResult; import org.hibernate.metamodel.source.annotation.xml.XMLEntityResult; @@ -44,35 +42,18 @@ import org.hibernate.metamodel.source.annotation.xml.XMLSqlResultSetMapping; import org.hibernate.metamodel.source.annotation.xml.XMLTableGenerator; /** - * note: Global Configurations {@link org.hibernate.metamodel.source.annotations.xml.filter.IndexedAnnotationFilter.GLOBAL_ANNOTATIONS} - * in the new build Index has no target - * * @author Strong Liu */ -class GlobalConfigurationMocker extends AbstractMocker { +class GlobalAnnotationMocker extends AbstractMocker { private GlobalAnnotations globalAnnotations; - GlobalConfigurationMocker(IndexBuilder indexBuilder, GlobalAnnotations globalAnnotations) { + GlobalAnnotationMocker(IndexBuilder indexBuilder, GlobalAnnotations globalAnnotations) { super( indexBuilder ); this.globalAnnotations = globalAnnotations; } - @Override - protected EntityMappingsMocker.Default getDefaults() { - return null; - } - @Override - protected DotName getTargetName() { - return null; - } - - @Override - protected AnnotationTarget getTarget() { - return null; - } - - void parser() { + void process() { if ( !globalAnnotations.getTableGeneratorMap().isEmpty() ) { for ( XMLTableGenerator generator : globalAnnotations.getTableGeneratorMap().values() ) { parserTableGenerator( generator ); @@ -117,7 +98,7 @@ class GlobalConfigurationMocker extends AbstractMocker { ); } return create( - SQL_RESULT_SET_MAPPINGS, + SQL_RESULT_SET_MAPPINGS, null, new AnnotationValue[] { AnnotationValue.createArrayValue( "values", values ) } ); @@ -133,7 +114,7 @@ class GlobalConfigurationMocker extends AbstractMocker { nestedColumnResultList( "columns", mapping.getColumnResult(), annotationValueList ); return create( - SQL_RESULT_SET_MAPPING, annotationValueList + SQL_RESULT_SET_MAPPING, null, annotationValueList ); } @@ -226,7 +207,7 @@ class GlobalConfigurationMocker extends AbstractMocker { ); } return create( - NAMED_NATIVE_QUERIES, + NAMED_NATIVE_QUERIES, null, new AnnotationValue[] { AnnotationValue.createArrayValue( "values", values ) } ); @@ -246,7 +227,7 @@ class GlobalConfigurationMocker extends AbstractMocker { nestedQueryHintList( "hints", namedNativeQuery.getHint(), annotationValueList ); return create( - NAMED_NATIVE_QUERY, annotationValueList + NAMED_NATIVE_QUERY, null, annotationValueList ); } @@ -262,7 +243,7 @@ class GlobalConfigurationMocker extends AbstractMocker { ); } return create( - NAMED_QUERIES, + NAMED_QUERIES, null, new AnnotationValue[] { AnnotationValue.createArrayValue( "values", values ) } ); @@ -276,7 +257,7 @@ class GlobalConfigurationMocker extends AbstractMocker { MockHelper.stringValue( "query", namedQuery.getQuery(), annotationValueList ); MockHelper.enumValue( "lockMode", LOCK_MODE_TYPE, namedQuery.getLockMode(), annotationValueList ); nestedQueryHintList( "hints", namedQuery.getHint(), annotationValueList ); - return create( NAMED_QUERY, annotationValueList ); + return create( NAMED_QUERY, null, annotationValueList ); } //@QueryHint @@ -315,7 +296,7 @@ class GlobalConfigurationMocker extends AbstractMocker { MockHelper.integerValue( "allocationSize", generator.getAllocationSize(), annotationValueList ); return create( - SEQUENCE_GENERATOR, annotationValueList + SEQUENCE_GENERATOR, null, annotationValueList ); } @@ -335,19 +316,16 @@ class GlobalConfigurationMocker extends AbstractMocker { nestedUniqueConstraintList( "uniqueConstraints", generator.getUniqueConstraint(), annotationValueList ); return create( - TABLE_GENERATOR, annotationValueList + TABLE_GENERATOR, null, annotationValueList ); } @Override protected AnnotationInstance push(AnnotationInstance annotationInstance) { - return globalAnnotations.push( annotationInstance.name(), annotationInstance ); - } - - @Override - protected AnnotationInstance create(DotName name, AnnotationTarget target, AnnotationValue[] annotationValues) { - AnnotationInstance annotationInstance = MockHelper.create( name, target, annotationValues ); - return push( annotationInstance ); + if ( annotationInstance != null ) { + return globalAnnotations.push( annotationInstance.name(), annotationInstance ); + } + return null; } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/mocker/ListenerMocker.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/mocker/ListenerMocker.java index e6d5d9d80e..2975175ceb 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/mocker/ListenerMocker.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/mocker/ListenerMocker.java @@ -30,7 +30,6 @@ 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.hibernate.metamodel.source.annotation.xml.XMLEntityListener; import org.hibernate.metamodel.source.annotation.xml.XMLEntityListeners; @@ -67,13 +66,13 @@ class ListenerMocker extends AbstractMocker { } MockHelper.classArrayValue( "value", clazzNameList, annotationValueList, indexBuilder.getServiceRegistry() ); - return create( ENTITY_LISTENERS, annotationValueList ); + return create( ENTITY_LISTENERS, classInfo, annotationValueList ); } private void parser(XMLEntityListener listener) { String clazz = listener.getClazz(); ClassInfo tempClassInfo = indexBuilder.createClassInfo( clazz ); - ListenerMocker builder = new ListenerMocker( indexBuilder, tempClassInfo ); + ListenerMocker builder = createListenerMocker( indexBuilder, tempClassInfo ); builder.parser( listener.getPostLoad() ); builder.parser( listener.getPostPersist() ); builder.parser( listener.getPostRemove() ); @@ -83,6 +82,9 @@ class ListenerMocker extends AbstractMocker { builder.parser( listener.getPreUpdate() ); indexBuilder.finishEntityObject( tempClassInfo.name(), null ); } + protected ListenerMocker createListenerMocker(IndexBuilder indexBuilder, ClassInfo classInfo){ + return new ListenerMocker( indexBuilder, classInfo ); + } //@PrePersist AnnotationInstance parser(XMLPrePersist callback) { @@ -147,17 +149,10 @@ class ListenerMocker extends AbstractMocker { } @Override - protected EntityMappingsMocker.Default getDefaults() { - return null; - } - - @Override - protected DotName getTargetName() { - return classInfo.name(); - } - - @Override - protected AnnotationTarget getTarget() { - return classInfo; + protected AnnotationInstance push(AnnotationInstance annotationInstance) { + if ( annotationInstance != null && annotationInstance.target() != null ) { + indexBuilder.addAnnotationInstance( classInfo.name(), annotationInstance ); + } + return annotationInstance; } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/mocker/PersistenceMetadataMocker.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/mocker/PersistenceMetadataMocker.java new file mode 100644 index 0000000000..195d39e9d6 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/mocker/PersistenceMetadataMocker.java @@ -0,0 +1,94 @@ +package org.hibernate.metamodel.source.annotations.xml.mocker; + +import java.util.HashMap; +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.hibernate.metamodel.source.annotation.xml.XMLPersistenceUnitDefaults; +import org.hibernate.metamodel.source.annotations.xml.MockedNames; + +/** + * @author Strong Liu + */ +class PersistenceMetadataMocker extends AbstractMocker { + private XMLPersistenceUnitDefaults persistenceUnitDefaults; + private GlobalAnnotations globalAnnotations = new GlobalAnnotations(); + private static Map nameDotNameMap = new HashMap(); + + static { + nameDotNameMap.put( ACCESS, MockedNames.DEFAULT_ACCESS ); + nameDotNameMap.put( ENTITY_LISTENERS, MockedNames.DEFAULT_ENTITY_LISTENERS ); + nameDotNameMap.put( POST_LOAD, MockedNames.DEFAULT_POST_LOAD ); + nameDotNameMap.put( POST_REMOVE, MockedNames.DEFAULT_POST_REMOVE ); + nameDotNameMap.put( POST_UPDATE, MockedNames.DEFAULT_POST_UPDATE ); + nameDotNameMap.put( POST_PERSIST, MockedNames.DEFAULT_POST_PERSIST ); + nameDotNameMap.put( PRE_REMOVE, MockedNames.DEFAULT_PRE_REMOVE ); + nameDotNameMap.put( PRE_UPDATE, MockedNames.DEFAULT_PRE_UPDATE ); + nameDotNameMap.put( PRE_PERSIST, MockedNames.DEFAULT_PRE_PERSIST ); + nameDotNameMap.put( MockedNames.DEFAULT_DELIMITED_IDENTIFIERS, MockedNames.DEFAULT_DELIMITED_IDENTIFIERS ); + } + + PersistenceMetadataMocker(IndexBuilder indexBuilder, XMLPersistenceUnitDefaults persistenceUnitDefaults) { + super( indexBuilder ); + this.persistenceUnitDefaults = persistenceUnitDefaults; + + + } + + @Override + protected AnnotationInstance push(AnnotationInstance annotationInstance) { + if ( annotationInstance != null ) { + return globalAnnotations.push( annotationInstance.name(), annotationInstance ); + } + return null; + } + + final void process() { + parserAccessType( persistenceUnitDefaults.getAccess(), null ); + if ( persistenceUnitDefaults.getDelimitedIdentifiers() != null ) { + create( MockedNames.DEFAULT_DELIMITED_IDENTIFIERS, null ); + } + if ( persistenceUnitDefaults.getEntityListeners() != null ) { + new DefaultListenerMocker( indexBuilder, null ).parser( persistenceUnitDefaults.getEntityListeners() ); + } + indexBuilder.finishGlobalConfigurationMocking( globalAnnotations ); + } + + @Override + protected AnnotationInstance create(DotName name, AnnotationTarget target, AnnotationValue[] annotationValues) { + DotName defaultName = nameDotNameMap.get( name ); + if ( defaultName == null ) { + return null; + } + return super.create( defaultName, target, annotationValues ); + + } + + class DefaultListenerMocker extends ListenerMocker { + DefaultListenerMocker(IndexBuilder indexBuilder, ClassInfo classInfo) { + super( indexBuilder, classInfo ); + } + + @Override + protected AnnotationInstance push(AnnotationInstance annotationInstance) { + return PersistenceMetadataMocker.this.push( annotationInstance ); + } + + @Override + protected AnnotationInstance create(DotName name, AnnotationTarget target, AnnotationValue[] annotationValues) { + return PersistenceMetadataMocker.this.create( name,target,annotationValues ); + } + + @Override + protected ListenerMocker createListenerMocker(IndexBuilder indexBuilder, ClassInfo classInfo) { + return new DefaultListenerMocker( indexBuilder, classInfo ); + } + } + + +} diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/xml/mocker/PersistenceMetadataMockerTest.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/xml/mocker/PersistenceMetadataMockerTest.java new file mode 100644 index 0000000000..81dacff01e --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/xml/mocker/PersistenceMetadataMockerTest.java @@ -0,0 +1,15 @@ +package org.hibernate.metamodel.source.annotations.xml.mocker; + +import org.jboss.jandex.Index; +import org.junit.Test; + +/** + * @author Strong Liu + */ +public class PersistenceMetadataMockerTest extends AbstractMockerTest { + @Test + public void testPersistenceMetadata() { + Index index = getMockedIndex( "persistence-metadata.xml" ); + index.printAnnotations(); + } +} diff --git a/hibernate-core/src/test/resources/org/hibernate/metamodel/source/annotations/xml/mocker/listener.xml b/hibernate-core/src/test/resources/org/hibernate/metamodel/source/annotations/xml/mocker/listener.xml index 6bdef51ae6..44fe5bbbc4 100644 --- a/hibernate-core/src/test/resources/org/hibernate/metamodel/source/annotations/xml/mocker/listener.xml +++ b/hibernate-core/src/test/resources/org/hibernate/metamodel/source/annotations/xml/mocker/listener.xml @@ -3,6 +3,18 @@ + + + + FIELD + + + + + + + + org.hibernate.metamodel.source.annotations.xml.mocker diff --git a/hibernate-core/src/test/resources/org/hibernate/metamodel/source/annotations/xml/mocker/persistence-metadata.xml b/hibernate-core/src/test/resources/org/hibernate/metamodel/source/annotations/xml/mocker/persistence-metadata.xml new file mode 100644 index 0000000000..6148c06247 --- /dev/null +++ b/hibernate-core/src/test/resources/org/hibernate/metamodel/source/annotations/xml/mocker/persistence-metadata.xml @@ -0,0 +1,18 @@ + + + + + + + FIELD + + + + + + + + +