From 00bd1de15d54adcfef7d4933bdb007bf4397d5c8 Mon Sep 17 00:00:00 2001 From: Michael Hunger Date: Thu, 13 Mar 2014 14:02:48 +0100 Subject: [PATCH] Revert "DATAGRAPH-441 - Adapt to latest changes in Spring Data Commons mapping metadata API." This reverts commit 48ab04c3da3e5f9290828537576c5c4b27ad94c0. --- .../JpaIdFieldAccessListenerFactory.java | 2 +- ...oreNodeDelegatingFieldAccessorFactory.java | 8 +- .../QueryFieldAccessorFactory.java | 2 +- .../TraversalFieldAccessorFactory.java | 4 +- ...ePropertyFieldAccessorListenerFactory.java | 5 +- .../mapping/Neo4jPersistentProperty.java | 7 ++ .../data/neo4j/mapping/RelationshipInfo.java | 14 ++-- .../support/index/IndexProviderImpl.java | 2 +- .../mapping/Neo4JPersistentPropertyImpl.java | 83 +++++++++++++------ .../support/mapping/Neo4jMappingContext.java | 12 +++ 10 files changed, 95 insertions(+), 44 deletions(-) diff --git a/spring-data-neo4j-cross-store/src/main/java/org/springframework/data/neo4j/cross_store/fieldaccess/JpaIdFieldAccessListenerFactory.java b/spring-data-neo4j-cross-store/src/main/java/org/springframework/data/neo4j/cross_store/fieldaccess/JpaIdFieldAccessListenerFactory.java index 9135e42d7..7ddf7de8f 100644 --- a/spring-data-neo4j-cross-store/src/main/java/org/springframework/data/neo4j/cross_store/fieldaccess/JpaIdFieldAccessListenerFactory.java +++ b/spring-data-neo4j-cross-store/src/main/java/org/springframework/data/neo4j/cross_store/fieldaccess/JpaIdFieldAccessListenerFactory.java @@ -36,7 +36,7 @@ public class JpaIdFieldAccessListenerFactory implements FieldAccessorListenerFac @Override public boolean accept(final Neo4jPersistentProperty property) { - return property.findAnnotation(Id.class) != null; + return property.getAnnotation(Id.class) != null; } @Override diff --git a/spring-data-neo4j-cross-store/src/main/java/org/springframework/data/neo4j/cross_store/support/node/CrossStoreNodeDelegatingFieldAccessorFactory.java b/spring-data-neo4j-cross-store/src/main/java/org/springframework/data/neo4j/cross_store/support/node/CrossStoreNodeDelegatingFieldAccessorFactory.java index 87f924f5b..5412c08b7 100644 --- a/spring-data-neo4j-cross-store/src/main/java/org/springframework/data/neo4j/cross_store/support/node/CrossStoreNodeDelegatingFieldAccessorFactory.java +++ b/spring-data-neo4j-cross-store/src/main/java/org/springframework/data/neo4j/cross_store/support/node/CrossStoreNodeDelegatingFieldAccessorFactory.java @@ -57,7 +57,7 @@ public class CrossStoreNodeDelegatingFieldAccessorFactory extends DelegatingFiel newConvertingNodePropertyFieldAccessorFactory()) { @Override public boolean accept(Neo4jPersistentProperty property) { - return property.findAnnotation(GraphProperty.class) != null && super.accept(property); + return property.getAnnotation(GraphProperty.class) != null && super.accept(property); } }, new JpaIdFieldAccessListenerFactory(template)); @@ -75,7 +75,7 @@ public class CrossStoreNodeDelegatingFieldAccessorFactory extends DelegatingFiel new RelatedToSingleFieldAccessorFactory(getTemplate()) { @Override public boolean accept(Neo4jPersistentProperty property) { - return property.findAnnotation(RelatedTo.class) != null && super.accept(property); + return property.getAnnotation(RelatedTo.class) != null && super.accept(property); } }, new RelatedToCollectionFieldAccessorFactory(getTemplate()), @@ -89,7 +89,7 @@ public class CrossStoreNodeDelegatingFieldAccessorFactory extends DelegatingFiel return new ConvertingNodePropertyFieldAccessorFactory(getTemplate()) { @Override public boolean accept(Neo4jPersistentProperty property) { - return property.findAnnotation(GraphProperty.class) != null && super.accept(property); + return property.getAnnotation(GraphProperty.class) != null && super.accept(property); } }; } @@ -98,7 +98,7 @@ public class CrossStoreNodeDelegatingFieldAccessorFactory extends DelegatingFiel return new PropertyFieldAccessorFactory(getTemplate()) { @Override public boolean accept(Neo4jPersistentProperty property) { - return property.findAnnotation(GraphProperty.class) != null && super.accept(property); + return property.getAnnotation(GraphProperty.class) != null && super.accept(property); } }; } diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/QueryFieldAccessorFactory.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/QueryFieldAccessorFactory.java index bc4588a2e..ae3a313f5 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/QueryFieldAccessorFactory.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/QueryFieldAccessorFactory.java @@ -67,7 +67,7 @@ public class QueryFieldAccessorFactory implements FieldAccessorFactory { public QueryFieldAccessor(final Neo4jPersistentProperty property, Neo4jTemplate template) { this.property = property; this.template = template; - final Query query = property.findAnnotation(Query.class); + final Query query = property.getAnnotation(Query.class); this.annotationParams = query.params(); if ((this.annotationParams.length % 2) != 0) { throw new IllegalArgumentException("Number of parameters has to be even to construct a parameter map"); diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/TraversalFieldAccessorFactory.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/TraversalFieldAccessorFactory.java index d419e4de7..5d5d3aaf3 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/TraversalFieldAccessorFactory.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/TraversalFieldAccessorFactory.java @@ -42,7 +42,7 @@ public class TraversalFieldAccessorFactory implements FieldAccessorFactory { @Override public boolean accept(final Neo4jPersistentProperty f) { - final GraphTraversal graphEntityTraversal = f.findAnnotation(GraphTraversal.class); + final GraphTraversal graphEntityTraversal = f.getAnnotation(GraphTraversal.class); return graphEntityTraversal != null && graphEntityTraversal.traversal() != FieldTraversalDescriptionBuilder.class && f.getType().equals(Iterable.class); @@ -68,7 +68,7 @@ public class TraversalFieldAccessorFactory implements FieldAccessorFactory { public TraversalFieldAccessor(final Neo4jPersistentProperty property, Neo4jTemplate template) { this.property = property; this.template = template; - final GraphTraversal graphEntityTraversal = property.findAnnotation(GraphTraversal.class); + final GraphTraversal graphEntityTraversal = property.getAnnotation(GraphTraversal.class); this.target = resolveTarget(graphEntityTraversal,property); this.params = graphEntityTraversal.params(); this.fieldTraversalDescriptionBuilder = createTraversalDescription(graphEntityTraversal); diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/ValidatingNodePropertyFieldAccessorListenerFactory.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/ValidatingNodePropertyFieldAccessorListenerFactory.java index d03660bda..b5aa0e51b 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/ValidatingNodePropertyFieldAccessorListenerFactory.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/ValidatingNodePropertyFieldAccessorListenerFactory.java @@ -45,7 +45,10 @@ class ValidatingNodePropertyFieldAccessorListenerFactory implements FieldAccesso } private boolean hasValidationAnnotation(final Neo4jPersistentProperty property) { - return property.findAnnotation(Constraint.class) != null; + for (Annotation annotation : property.getAnnotations()) { + if (annotation.annotationType().isAnnotationPresent(Constraint.class)) return true; + } + return false; } @Override diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/mapping/Neo4jPersistentProperty.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/mapping/Neo4jPersistentProperty.java index b451ebc42..322329e2e 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/mapping/Neo4jPersistentProperty.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/mapping/Neo4jPersistentProperty.java @@ -16,6 +16,9 @@ package org.springframework.data.neo4j.mapping; +import java.lang.annotation.Annotation; +import java.util.Collection; + import org.springframework.core.convert.ConversionService; import org.springframework.data.mapping.PersistentProperty; @@ -60,6 +63,10 @@ public interface Neo4jPersistentProperty extends PersistentProperty getAnnotations(); + + T getAnnotation(Class annotationType); boolean isStartNode(); diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/mapping/RelationshipInfo.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/mapping/RelationshipInfo.java index f3f7bc0a2..59f4f9f62 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/mapping/RelationshipInfo.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/mapping/RelationshipInfo.java @@ -67,13 +67,13 @@ public class RelationshipInfo { this.readonly = isCollection() && typeInformation.getType().equals(Iterable.class); } - public static RelationshipInfo fromField(String name, TypeInformation typeInformation, Neo4jMappingContext ctx) { - return new RelationshipInfo(name, Direction.OUTGOING, typeInformation,null, ctx); + public static RelationshipInfo fromField(Field field, TypeInformation typeInformation, Neo4jMappingContext ctx) { + return new RelationshipInfo(field.getName(), Direction.OUTGOING, typeInformation,null, ctx); } - public static RelationshipInfo fromField(String name, RelatedTo annotation, TypeInformation typeInformation, Neo4jMappingContext ctx) { + public static RelationshipInfo fromField(Field field, RelatedTo annotation, TypeInformation typeInformation, Neo4jMappingContext ctx) { RelationshipInfo relationshipInfo = new RelationshipInfo( - annotation.type().isEmpty() ? name : annotation.type(), + annotation.type().isEmpty() ? field.getName() : annotation.type(), annotation.direction(), typeInformation, annotation.elementClass() != Object.class ? ClassTypeInformation.from(annotation.elementClass()) : null, @@ -85,10 +85,10 @@ public class RelationshipInfo { return relationshipInfo; } - public static RelationshipInfo fromField(String name, RelatedToVia annotation, TypeInformation typeInformation, Neo4jMappingContext ctx) { + public static RelationshipInfo fromField(Field field, RelatedToVia annotation, TypeInformation typeInformation, Neo4jMappingContext ctx) { final TypeInformation elementClass = elementClass(annotation, typeInformation); RelationshipInfo relationshipInfo = new RelationshipInfo( - relationshipType(annotation, typeInformation), + relationshipType(field, annotation, typeInformation), annotation.direction(), typeInformation, elementClass, @@ -98,7 +98,7 @@ public class RelationshipInfo { return relationshipInfo; } - private static String relationshipType(RelatedToVia annotation, TypeInformation typeInformation) { + private static String relationshipType(Field field, RelatedToVia annotation, TypeInformation typeInformation) { if (!annotation.type().isEmpty()) return annotation.type(); final TypeInformation relationshipEntityType = elementClass(annotation, typeInformation); final RelationshipEntity relationshipEntity = relationshipEntityType.getType().getAnnotation(RelationshipEntity.class); diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/index/IndexProviderImpl.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/index/IndexProviderImpl.java index 8cdea0bcf..b3b3c85b3 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/index/IndexProviderImpl.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/index/IndexProviderImpl.java @@ -93,7 +93,7 @@ public class IndexProviderImpl implements IndexProvider { @Override public Index getIndex(Neo4jPersistentProperty property, final Class instanceType) { - final Indexed indexedAnnotation = property.findAnnotation(Indexed.class); + final Indexed indexedAnnotation = property.getAnnotation(Indexed.class); final Neo4jPersistentEntity declaringType = property.getOwner(); final String providedIndexName = providedIndexName(indexedAnnotation); final Indexed.Level level = indexingLevel(indexedAnnotation); diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/mapping/Neo4JPersistentPropertyImpl.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/mapping/Neo4JPersistentPropertyImpl.java index 424f3de4a..82323f609 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/mapping/Neo4JPersistentPropertyImpl.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/mapping/Neo4JPersistentPropertyImpl.java @@ -20,15 +20,19 @@ import java.beans.PropertyDescriptor; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.Modifier; +import java.util.Collection; +import java.util.IdentityHashMap; import java.util.Iterator; +import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.convert.ConversionService; +import org.springframework.data.annotation.Transient; import org.springframework.data.mapping.Association; import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty; -import org.springframework.data.mapping.model.BeanWrapper; +import org.springframework.data.mapping.model.MappingException; import org.springframework.data.mapping.model.SimpleTypeHolder; import org.springframework.data.neo4j.annotation.EndNode; import org.springframework.data.neo4j.annotation.Fetch; @@ -64,21 +68,24 @@ class Neo4jPersistentPropertyImpl extends AnnotationBasedPersistentProperty, ? extends Annotation> annotations; private Association myAssociation; private String defaultValue; private Class propertyType; private String query; private final boolean isNeo4jEntityType; - private Boolean isAssociation; + private final Boolean isAssociation; private final String neo4jPropertyName; private final int hash; public Neo4jPersistentPropertyImpl(Field field, PropertyDescriptor propertyDescriptor, PersistentEntity owner, SimpleTypeHolder simpleTypeHolder, Neo4jMappingContext ctx) { super(field, propertyDescriptor, owner, simpleTypeHolder); - this.hash = field == null ? propertyDescriptor.hashCode() : field.hashCode(); + this.hash = getField().hashCode(); this.relationshipInfo = extractRelationshipInfo(field, ctx); + this.annotations = extractAnnotations(field); this.propertyType = extractPropertyType(); + this.isAssociation = ctx.isReference(this); this.isNeo4jEntityType = isNeo4jPropertyType(getType()); this.neo4jPropertyName = createNeo4jPropertyName(); this.indexInfo = extractIndexInfo(); @@ -114,6 +121,14 @@ class Neo4jPersistentPropertyImpl extends AnnotationBasedPersistentProperty,? extends Annotation> extractAnnotations(Field field) { + Map, Annotation> result=new IdentityHashMap, Annotation>(); + for (Annotation annotation : field.getAnnotations()) { + result.put(annotation.annotationType(), annotation); + } + return result; + } + private IndexInfo extractIndexInfo() { final Indexed annotation = getAnnotation(Indexed.class); return annotation!=null ? new IndexInfo(annotation,this) : null; @@ -125,14 +140,14 @@ class Neo4jPersistentPropertyImpl extends AnnotationBasedPersistentProperty,Object> wrapper = BeanWrapper.create(entity, null); - wrapper.setProperty(this, newValue); + try { + if (!field.isAccessible()) field.setAccessible(true); + field.set(entity, newValue); + } catch (IllegalAccessException e) { + throw new MappingException("Could not access field "+field+" for setting value "+newValue+" on "+this); + } } private static boolean hasAnnotation(TypeInformation typeInformation, final Class annotationClass) { @@ -156,12 +174,7 @@ class Neo4jPersistentPropertyImpl extends AnnotationBasedPersistentProperty entityClass = (Neo4jPersistentEntity) getOwner(); + final Neo4jPersistentEntity entityClass = (Neo4jPersistentEntity) getOwner(); if (entityClass.useShortNames()) return getName(); return String.format("%s.%s", entityClass.getType().getSimpleName(), getName()); } @@ -246,18 +259,32 @@ class Neo4jPersistentPropertyImpl extends AnnotationBasedPersistentProperty getAnnotations() { + + if (annotations == null) { + + } + + return annotations.values(); + } + public Object getValue(final Object entity, final MappingPolicy mappingPolicy) { if (entity instanceof ManagedEntity && !mappingPolicy.accessField()) { - return DoReturn.unwrap(((ManagedEntity) entity).getEntityState().getValue(this, mappingPolicy)); + return DoReturn.unwrap(((ManagedEntity) entity).getEntityState().getValue(this, mappingPolicy)); } return getValueFromEntity(entity, mappingPolicy); } @Override public Object getValueFromEntity(Object entity, final MappingPolicy mappingPolicy) { - - BeanWrapper, Object> wrapper = BeanWrapper.create(entity, null); - return wrapper.getProperty(this); + try { + final Field field = getField(); + if (!field.isAccessible()) field.setAccessible(true); + return field.get(entity); + } catch (IllegalAccessException e) { + throw new MappingException("Could not access field "+field); + } } @SuppressWarnings("unchecked") @@ -322,12 +349,14 @@ class Neo4jPersistentPropertyImpl extends AnnotationBasedPersistentProperty annotationType : annotations.keySet()) { + if (annotationType.getName().equals(className)) return true; + } + return false; } @Override @@ -335,7 +364,7 @@ class Neo4jPersistentPropertyImpl extends AnnotationBasedPersistentProperty> result = super.getPersistentEntityType(); for (Iterator> it = result.iterator(); it.hasNext(); ) { final TypeInformation typeInformation = it.next(); - final Class type = typeInformation.getType(); + final Class type = typeInformation.getType(); if (isNodeEntity(type) || isRelationshipEntity(type)) continue; if (log.isInfoEnabled()) log.info("ignoring "+getName()+" "+type+" "+typeInformation.getActualType().getType()); it.remove(); diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/mapping/Neo4jMappingContext.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/mapping/Neo4jMappingContext.java index 5fd0f88b1..81322b808 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/mapping/Neo4jMappingContext.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/mapping/Neo4jMappingContext.java @@ -222,6 +222,18 @@ public class Neo4jMappingContext extends AbstractMappingContext