📄 ejb3overridenannotationreader.java
字号:
for (Annotation ann : this.annotations) { annotationsMap.put( ann.annotationType(), ann ); } checkForOrphanProperties( tree ); } else if ( className != null ) { //&& propertyName != null ) { //always true but less confusing Element tree = xmlContext.getXMLTree( className, propertyName ); Annotation[] annotations = getJavaAnnotations(); List<Annotation> annotationList = new ArrayList<Annotation>( annotations.length + 5 ); annotationsMap = new HashMap<Class, Annotation>( annotations.length + 5 ); for (Annotation annotation : annotations) { if ( !annotationToXml.containsKey( annotation.annotationType() ) ) { //unknown annotations are left over annotationList.add( annotation ); } } preCalculateElementsForProperty( tree ); Transient transientAnn = getTransient( defaults ); if ( transientAnn != null ) { annotationList.add( transientAnn ); } else { if ( defaults.canUseJavaAnnotations() ) { Annotation annotation = getJavaAnnotation( AccessType.class ); addIfNotNull( annotationList, annotation ); } getId( annotationList, defaults ); getEmbeddedId( annotationList, defaults ); getEmbedded( annotationList, defaults ); getBasic( annotationList, defaults ); getVersion( annotationList, defaults ); getAssociation( ManyToOne.class, annotationList, defaults ); getAssociation( OneToOne.class, annotationList, defaults ); getAssociation( OneToMany.class, annotationList, defaults ); getAssociation( ManyToMany.class, annotationList, defaults ); addIfNotNull( annotationList, getSequenceGenerator( elementsForProperty, defaults ) ); addIfNotNull( annotationList, getTableGenerator( elementsForProperty, defaults ) ); addIfNotNull( annotationList, getAttributeOverrides( elementsForProperty, defaults ) ); } processEventAnnotations( annotationList, defaults ); //FIXME use annotationsMap rather than annotationList this will be faster since the annotation type is usually known at put() time this.annotations = annotationList.toArray( new Annotation[annotationList.size()] ); for (Annotation ann : this.annotations) { annotationsMap.put( ann.annotationType(), ann ); } } else { this.annotations = getJavaAnnotations(); annotationsMap = new HashMap<Class, Annotation>( annotations.length + 5 ); for (Annotation ann : this.annotations) { annotationsMap.put( ann.annotationType(), ann ); } } } } private void checkForOrphanProperties(Element tree) { Class clazz; try { clazz = ReflectHelper.classForName( className, this.getClass() ); } catch (ClassNotFoundException e) { return; //a primitive type most likely } Element element = tree != null ? tree.element( "attributes" ) : null; //put entity.attributes elements if ( element != null ) { //precompute the list of properties //TODO is it really useful... Set<String> properties = new HashSet<String>(); for (Field field : clazz.getFields()) { properties.add( field.getName() ); } for (Method method : clazz.getMethods()) { String name = method.getName(); if ( name.startsWith( "get" ) ) { properties.add( Introspector.decapitalize( name.substring( "get".length() ) ) ); } else if ( name.startsWith( "is" ) ) { properties.add( Introspector.decapitalize( name.substring( "is".length() ) ) ); } } for (Element subelement : (List<Element>) element.elements()) { String propertyName = subelement.attributeValue( "name" ); if ( !properties.contains( propertyName ) ) { log.warn( "Property " + StringHelper.qualify( className, propertyName ) + " not found in class" + " but described in <mapping-file/> (possible typo error)" ); } } } } /** * Addes the Annotation to the list (only if it's not null) and then returns it. */ private Annotation addIfNotNull(List<Annotation> annotationList, Annotation element) { if ( element != null ) { annotationList.add( element ); } return element; } //TODO mutualize the next 2 methods private Annotation getTableGenerator(List<Element> elementsForProperty, XMLContext.Default defaults) { for (Element element : elementsForProperty) { Element subelement = element != null ? element.element( annotationToXml.get( TableGenerator.class ) ) : null; if ( subelement != null ) { return buildTableGeneratorAnnotation( subelement, defaults ); } } if ( elementsForProperty.size() == 0 && defaults.canUseJavaAnnotations() ) { return getJavaAnnotation( TableGenerator.class ); } else { return null; } } private Annotation getSequenceGenerator(List<Element> elementsForProperty, XMLContext.Default defaults) { for (Element element : elementsForProperty) { Element subelement = element != null ? element.element( annotationToXml.get( SequenceGenerator.class ) ) : null; if ( subelement != null ) { return buildSequenceGeneratorAnnotation( subelement ); } } if ( elementsForProperty.size() == 0 && defaults.canUseJavaAnnotations() ) { return getJavaAnnotation( SequenceGenerator.class ); } else { return null; } } private void processEventAnnotations(List<Annotation> annotationList, XMLContext.Default defaults) { boolean eventElement = false; for (Element element : elementsForProperty) { String elementName = element.getName(); if ( "pre-persist".equals( elementName ) ) { AnnotationDescriptor ad = new AnnotationDescriptor( PrePersist.class ); annotationList.add( AnnotationFactory.create( ad ) ); eventElement = true; } else if ( "pre-remove".equals( elementName ) ) { AnnotationDescriptor ad = new AnnotationDescriptor( PreRemove.class ); annotationList.add( AnnotationFactory.create( ad ) ); eventElement = true; } else if ( "pre-update".equals( elementName ) ) { AnnotationDescriptor ad = new AnnotationDescriptor( PreUpdate.class ); annotationList.add( AnnotationFactory.create( ad ) ); eventElement = true; } else if ( "post-persist".equals( elementName ) ) { AnnotationDescriptor ad = new AnnotationDescriptor( PostPersist.class ); annotationList.add( AnnotationFactory.create( ad ) ); eventElement = true; } else if ( "post-remove".equals( elementName ) ) { AnnotationDescriptor ad = new AnnotationDescriptor( PostRemove.class ); annotationList.add( AnnotationFactory.create( ad ) ); eventElement = true; } else if ( "post-update".equals( elementName ) ) { AnnotationDescriptor ad = new AnnotationDescriptor( PostUpdate.class ); annotationList.add( AnnotationFactory.create( ad ) ); eventElement = true; } else if ( "post-load".equals( elementName ) ) { AnnotationDescriptor ad = new AnnotationDescriptor( PostLoad.class ); annotationList.add( AnnotationFactory.create( ad ) ); eventElement = true; } } if ( !eventElement && defaults.canUseJavaAnnotations() ) { Annotation ann = getJavaAnnotation( PrePersist.class ); addIfNotNull( annotationList, ann ); ann = getJavaAnnotation( PreRemove.class ); addIfNotNull( annotationList, ann ); ann = getJavaAnnotation( PreUpdate.class ); addIfNotNull( annotationList, ann ); ann = getJavaAnnotation( PostPersist.class ); addIfNotNull( annotationList, ann ); ann = getJavaAnnotation( PostRemove.class ); addIfNotNull( annotationList, ann ); ann = getJavaAnnotation( PostUpdate.class ); addIfNotNull( annotationList, ann ); ann = getJavaAnnotation( PostLoad.class ); addIfNotNull( annotationList, ann ); } } private EntityListeners getEntityListeners(Element tree, XMLContext.Default defaults) { Element element = tree != null ? tree.element( "entity-listeners" ) : null; if ( element != null ) { List<Class> entityListenerClasses = new ArrayList<Class>(); for (Element subelement : (List<Element>) element.elements( "entity-listener" )) { String className = subelement.attributeValue( "class" ); try { entityListenerClasses.add( ReflectHelper.classForName( XMLContext.buildSafeClassName( className, defaults ), this.getClass() ) ); } catch (ClassNotFoundException e) { throw new AnnotationException( "Unable to find " + element.getPath() + ".class: " + className, e ); } } AnnotationDescriptor ad = new AnnotationDescriptor( EntityListeners.class ); ad.setValue( "value", entityListenerClasses.toArray( new Class[entityListenerClasses.size()] ) ); return AnnotationFactory.create( ad ); } else if ( defaults.canUseJavaAnnotations() ) { return getJavaAnnotation( EntityListeners.class ); } else { return null; } } private JoinTable overridesDefaultsInJoinTable(Annotation annotation, XMLContext.Default defaults) { //no element but might have some default or some annotation boolean defaultToJoinTable = !( isJavaAnnotationPresent( JoinColumn.class ) || isJavaAnnotationPresent( JoinColumns.class ) ); final Class<? extends Annotation> annotationClass = annotation.annotationType(); defaultToJoinTable = defaultToJoinTable && ( ( annotationClass == ManyToMany.class && StringHelper.isEmpty( ( (ManyToMany) annotation ).mappedBy() ) ) || ( annotationClass == OneToMany.class && StringHelper.isEmpty( ( (OneToMany) annotation ).mappedBy() ) ) || ( annotationClass == CollectionOfElements.class ) ); final Class<JoinTable> annotationType = JoinTable.class; if ( defaultToJoinTable && ( StringHelper.isNotEmpty( defaults.getCatalog() ) || StringHelper.isNotEmpty( defaults.getSchema() ) ) ) { AnnotationDescriptor ad = new AnnotationDescriptor( annotationType ); if ( defaults.canUseJavaAnnotations() ) { JoinTable table = getJavaAnnotation( annotationType ); if ( table != null ) { ad.setValue( "name", table.name() ); ad.setValue( "schema", table.schema() ); ad.setValue( "catalog", table.catalog() ); ad.setValue( "uniqueConstraints", table.uniqueConstraints() ); ad.setValue( "joinColumns", table.joinColumns() ); ad.setValue( "inverseJoinColumns", table.inverseJoinColumns() ); } } if ( StringHelper.isEmpty( (String) ad.valueOf( "schema" ) ) && StringHelper.isNotEmpty( defaults.getSchema() ) ) { ad.setValue( "schema", defaults.getSchema() ); } if ( StringHelper.isEmpty( (String) ad.valueOf( "catalog" ) ) && StringHelper.isNotEmpty( defaults.getCatalog() ) ) { ad.setValue( "catalog", defaults.getCatalog() ); } return AnnotationFactory.create( ad ); } else if ( defaults.canUseJavaAnnotations() ) { return getJavaAnnotation( annotationType ); } else { return null; } } /* * no partial overriding possible */ private void getJoinTable(List<Annotation> annotationList, Element tree, XMLContext.Default defaults) { Element subelement = tree == null ? null : tree.element( "join-table" ); final Class<JoinTable> annotationType = JoinTable.class; if ( subelement != null ) { //ignore java annotation, an element is defined AnnotationDescriptor annotation = new AnnotationDescriptor( annotationType ); copyStringAttribute( annotation, subelement, "name", false ); copyStringAttribute( annotation, subelement, "catalog", false ); if ( StringHelper.isNotEmpty( defaults.getCatalog() ) && StringHelper.isEmpty( (String) annotation.valueOf( "catalog" ) ) ) { annotation.setValue( "catalog", defaults.getCatalog() ); } copyStringAttribute( annotation, subelement, "schema", false ); if ( StringHelper.isNotEmpty( defaults.getSchema() ) && StringHelper.isEmpty( (String) annotation.valueOf( "schema" ) ) ) { annotation.setValue( "schema", defaults.getSchema() ); } buildUniqueConstraints( annotation, subelement ); annotation.setValue( "joinColumns", getJoinColumns( subelement, false ) ); annotation.setValue( "inverseJoinColumns", getJoinColumns( subelement, true ) ); annotationList.add( AnnotationFactory.create( annotation ) ); } } private void getAssociation( Class<? extends Annotation> annotationType, List<Annotation> annotationList, XMLContext.Default defaults ) { String xmlName = annotationToXml.get( annotationType ); for (Element element : elementsForProperty) { if ( xmlName.equals( element.getName() ) ) { AnnotationDescriptor ad = new AnnotationDescriptor( annotationType ); String className = element.attributeValue( "target-entity" ); if ( className != null ) { Class clazz; try {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -