📄 ejb3overridenannotationreader.java
字号:
// );// }// }// }// } } } if ( elementsForProperty.size() == 0 && defaults.canUseJavaAnnotations() ) { Annotation annotation = getJavaAnnotation( EmbeddedId.class ); if ( annotation != null ) { annotationList.add( annotation ); annotation = getJavaAnnotation( Column.class ); addIfNotNull( annotationList, annotation ); annotation = getJavaAnnotation( Columns.class ); addIfNotNull( annotationList, annotation ); annotation = getJavaAnnotation( GeneratedValue.class ); addIfNotNull( annotationList, annotation ); annotation = getJavaAnnotation( Temporal.class ); addIfNotNull( annotationList, annotation ); annotation = getJavaAnnotation( TableGenerator.class ); addIfNotNull( annotationList, annotation ); annotation = getJavaAnnotation( SequenceGenerator.class ); addIfNotNull( annotationList, annotation ); annotation = getJavaAnnotation( AttributeOverride.class ); addIfNotNull( annotationList, annotation ); annotation = getJavaAnnotation( AttributeOverrides.class ); addIfNotNull( annotationList, annotation ); annotation = getJavaAnnotation( AssociationOverride.class ); addIfNotNull( annotationList, annotation ); annotation = getJavaAnnotation( AssociationOverrides.class ); addIfNotNull( annotationList, annotation ); } } } private void preCalculateElementsForProperty(Element tree) { elementsForProperty = new ArrayList<Element>(); Element element = tree != null ? tree.element( "attributes" ) : null; //put entity.attributes elements if ( element != null ) { for (Element subelement : (List<Element>) element.elements()) { if ( propertyName.equals( subelement.attributeValue( "name" ) ) ) { elementsForProperty.add( subelement ); } } } //add pre-* etc from entity and pure entity listener classes if ( tree != null ) { for (Element subelement : (List<Element>) tree.elements()) { if ( propertyName.equals( subelement.attributeValue( "method-name" ) ) ) { elementsForProperty.add( subelement ); } } } } private void getId(List<Annotation> annotationList, XMLContext.Default defaults) { for (Element element : elementsForProperty) { if ( "id".equals( element.getName() ) ) { boolean processId = isProcessingId( defaults ); if ( processId ) { Annotation annotation = buildColumns( element ); addIfNotNull( annotationList, annotation ); annotation = buildGeneratedValue( element ); addIfNotNull( annotationList, annotation ); getTemporal( annotationList, element ); //FIXME: fix the priority of xml over java for generator names annotation = getTableGenerator( element, defaults ); addIfNotNull( annotationList, annotation ); annotation = getSequenceGenerator( element, defaults ); addIfNotNull( annotationList, annotation ); AnnotationDescriptor id = new AnnotationDescriptor( Id.class ); annotationList.add( AnnotationFactory.create( id ) ); }// else {// if ( defaults.canUseJavaAnnotations() ) {// if ( ! properOverridingOnMetadataNonComplete ) {// //check that id exists on the other attribute// //TODO EmbeddedId too?// if ( mirroredAttribute == null || ! mirroredAttribute.isAnnotationPresent( Id.class ) ) {// throw new AnnotationException(// "Cannot override a property with <id> it does not have an @Id already"// );// }// }// }// } } } if ( elementsForProperty.size() == 0 && defaults.canUseJavaAnnotations() ) { Annotation annotation = getJavaAnnotation( Id.class ); if ( annotation != null ) { annotationList.add( annotation ); annotation = getJavaAnnotation( Column.class ); addIfNotNull( annotationList, annotation ); annotation = getJavaAnnotation( Columns.class ); addIfNotNull( annotationList, annotation ); annotation = getJavaAnnotation( GeneratedValue.class ); addIfNotNull( annotationList, annotation ); annotation = getJavaAnnotation( Temporal.class ); addIfNotNull( annotationList, annotation ); annotation = getJavaAnnotation( TableGenerator.class ); addIfNotNull( annotationList, annotation ); annotation = getJavaAnnotation( SequenceGenerator.class ); addIfNotNull( annotationList, annotation ); annotation = getJavaAnnotation( AttributeOverride.class ); addIfNotNull( annotationList, annotation ); annotation = getJavaAnnotation( AttributeOverrides.class ); addIfNotNull( annotationList, annotation ); annotation = getJavaAnnotation( AssociationOverride.class ); addIfNotNull( annotationList, annotation ); annotation = getJavaAnnotation( AssociationOverrides.class ); addIfNotNull( annotationList, annotation ); } } } private boolean isProcessingId(XMLContext.Default defaults) { boolean isExplicit = defaults.getAccess() != null; boolean correctAccess = ( PropertyType.PROPERTY.equals( propertyType ) && "property".equals( defaults.getAccess() ) ) || ( PropertyType.FIELD.equals( propertyType ) && "field".equals( defaults.getAccess() ) ); boolean hasId = defaults.canUseJavaAnnotations() && ( isJavaAnnotationPresent( Id.class ) || isJavaAnnotationPresent( EmbeddedId.class ) ); //if ( properAccessOnMetadataComplete || properOverridingOnMetadataNonComplete ) { boolean mirrorAttributeIsId = defaults.canUseJavaAnnotations() && ( mirroredAttribute != null && ( mirroredAttribute.isAnnotationPresent( Id.class ) || mirroredAttribute.isAnnotationPresent( EmbeddedId.class ) ) ); boolean propertyIsDefault = PropertyType.PROPERTY.equals( propertyType ) && !mirrorAttributeIsId; return correctAccess || ( !isExplicit && hasId ) || ( !isExplicit && propertyIsDefault ); } private Columns buildColumns(Element element) { List<Element> subelements = element.elements( "column" ); List<Column> columns = new ArrayList<Column>( subelements.size() ); for (Element subelement : subelements) { columns.add( getColumn( subelement, false, element ) ); } if ( columns.size() > 0 ) { AnnotationDescriptor columnsDescr = new AnnotationDescriptor( Columns.class ); columnsDescr.setValue( "columns", columns.toArray( new Column[columns.size()] ) ); return AnnotationFactory.create( columnsDescr ); } else { return null; } } private GeneratedValue buildGeneratedValue(Element element) { Element subElement = element != null ? element.element( "generated-value" ) : null; if ( subElement != null ) { AnnotationDescriptor ad = new AnnotationDescriptor( GeneratedValue.class ); String strategy = subElement.attributeValue( "strategy" ); if ( "TABLE".equalsIgnoreCase( strategy ) ) { ad.setValue( "strategy", GenerationType.TABLE ); } else if ( "SEQUENCE".equalsIgnoreCase( strategy ) ) { ad.setValue( "strategy", GenerationType.SEQUENCE ); } else if ( "IDENTITY".equalsIgnoreCase( strategy ) ) { ad.setValue( "strategy", GenerationType.IDENTITY ); } else if ( "AUTO".equalsIgnoreCase( strategy ) ) { ad.setValue( "strategy", GenerationType.AUTO ); } else if ( StringHelper.isNotEmpty( strategy ) ) { throw new AnnotationException( "Unknown GenerationType: " + strategy + ". " + SCHEMA_VALIDATION ); } copyStringAttribute( ad, subElement, "generator", false ); return AnnotationFactory.create( ad ); } else { return null; } } private void getTemporal(List<Annotation> annotationList, Element element) { Element subElement = element != null ? element.element( "temporal" ) : null; if ( subElement != null ) { AnnotationDescriptor ad = new AnnotationDescriptor( Temporal.class ); String temporal = subElement.getTextTrim(); if ( "DATE".equalsIgnoreCase( temporal ) ) { ad.setValue( "value", TemporalType.DATE ); } else if ( "TIME".equalsIgnoreCase( temporal ) ) { ad.setValue( "value", TemporalType.TIME ); } else if ( "TIMESTAMP".equalsIgnoreCase( temporal ) ) { ad.setValue( "value", TemporalType.TIMESTAMP ); } else if ( StringHelper.isNotEmpty( temporal ) ) { throw new AnnotationException( "Unknown TemporalType: " + temporal + ". " + SCHEMA_VALIDATION ); } annotationList.add( AnnotationFactory.create( ad ) ); } } private AssociationOverrides getAssociationOverrides(Element tree, XMLContext.Default defaults) { List<AssociationOverride> attributes = (List<AssociationOverride>) buildAssociationOverrides( tree ); if ( defaults.canUseJavaAnnotations() ) { AssociationOverride annotation = getJavaAnnotation( AssociationOverride.class ); addAssociationOverrideIfNeeded( annotation, attributes ); AssociationOverrides annotations = getJavaAnnotation( AssociationOverrides.class ); if ( annotations != null ) { for (AssociationOverride current : annotations.value()) { addAssociationOverrideIfNeeded( current, attributes ); } } } if ( attributes.size() > 0 ) { AnnotationDescriptor ad = new AnnotationDescriptor( AssociationOverrides.class ); ad.setValue( "value", attributes.toArray( new AssociationOverride[attributes.size()] ) ); return AnnotationFactory.create( ad ); } else { return null; } } private List<AssociationOverride> buildAssociationOverrides(Element element) { List<Element> subelements = element == null ? null : element.elements( "association-override" ); List<AssociationOverride> overrides = new ArrayList<AssociationOverride>(); if ( subelements != null && subelements.size() > 0 ) { for (Element current : subelements) { AnnotationDescriptor override = new AnnotationDescriptor( AssociationOverride.class ); copyStringAttribute( override, current, "name", true ); override.setValue( "joinColumns", getJoinColumns( current, false ) ); overrides.add( (AssociationOverride) AnnotationFactory.create( override ) ); } } return overrides; } private JoinColumn[] getJoinColumns(Element element, boolean isInverse) { List<Element> subelements = element != null ? element.elements( isInverse ? "inverse-join-column" : "join-column" ) : null; List<JoinColumn> joinColumns = new ArrayList<JoinColumn>(); if ( subelements != null ) { for (Element subelement : subelements) { AnnotationDescriptor column = new AnnotationDescriptor( JoinColumn.class ); copyStringAttribute( column, subelement, "name", false ); copyStringAttribute( column, subelement, "referenced-column-name", false ); copyBooleanAttribute( column, subelement, "unique" ); copyBooleanAttribute( column, subelement, "nullable" ); copyBooleanAttribute( column, subelement, "insertable" ); copyBooleanAttribute( column, subelement, "updatable" ); copyStringAttribute( column, subelement, "column-definition", false ); copyStringAttribute( column, subelement, "table", false ); joinColumns.add( (JoinColumn) AnnotationFactory.create( column ) ); } } return joinColumns.toArray( new JoinColumn[joinColumns.size()] ); } private void addAssociationOverrideIfNeeded(AssociationOverride annotation, List<AssociationOverride> overrides) { if ( annotation != null ) { String overrideName = annotation.name(); boolean present = false; for (AssociationOverride current : overrides) { if ( current.name().equals( overrideName ) ) { present = true; break; } } if ( !present ) overrides.add( annotation ); } } private AttributeOverrides getAttributeOverrides(Element tree, XMLContext.Default defaults) { List<AttributeOverride> attributes = buildAttributeOverrides( tree ); return mergeAttributeOverrides( defaults, attributes ); } private AttributeOverrides getAttributeOverrides(List<Element> elements, XMLContext.Default defaults) { List<AttributeOverride> attributes = new ArrayList<AttributeOverride>(); for (Element element : elements) { attributes.addAll( buildAttributeOverrides( element ) ); } return mergeAttributeOverrides( defaults, attributes ); } private AttributeOverrides mergeAttributeOverrides(XMLContext.Default defaults, List<AttributeOverride> attributes) { if ( defaults.canUseJavaAnnotations() ) { AttributeOverride annotation = getJavaAnnotation( AttributeOverride.class ); addAttributeOverrideIfNeeded( annotation, attributes ); AttributeOverrides annotations = getJavaAnnotation( AttributeOverrides.class ); if ( annotations != null ) { for (AttributeOverride current : annotations.value()) { addAttributeOverrideIfNeeded( current, attributes ); } } } if ( attributes.size() > 0 ) { AnnotationDescriptor ad = new AnnotationDescriptor( AttributeOverrides.class ); ad.setValue( "value", attributes.toArray( new AttributeOverride[attributes.size()] ) ); return AnnotationFactory.create( ad ); } else { return null; } } private List<AttributeOverride> buildAttributeOverrides(Element element) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -