From 862e652f2cfac15508251f1dd81c815174689f89 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 23 Mar 2017 07:35:58 +0100 Subject: [PATCH] DATAJPA-1064 - Improvements in JpaPersistentEntity/Property. Better use of Optional in implementations. --- .../jpa/mapping/JpaPersistentEntityImpl.java | 4 +- .../mapping/JpaPersistentPropertyImpl.java | 85 +++++++------------ .../JpaPersistentPropertyImplUnitTests.java | 12 ++- 3 files changed, 44 insertions(+), 57 deletions(-) diff --git a/src/main/java/org/springframework/data/jpa/mapping/JpaPersistentEntityImpl.java b/src/main/java/org/springframework/data/jpa/mapping/JpaPersistentEntityImpl.java index 9da1434cf..2bc449993 100644 --- a/src/main/java/org/springframework/data/jpa/mapping/JpaPersistentEntityImpl.java +++ b/src/main/java/org/springframework/data/jpa/mapping/JpaPersistentEntityImpl.java @@ -132,7 +132,9 @@ class JpaPersistentEntityImpl extends BasicPersistentEntity getIdentifier() { - return proxyIdAccessor.shouldUseAccessorFor(bean) ? Optional.ofNullable(proxyIdAccessor.getIdentifierFrom(bean)) + + return proxyIdAccessor.shouldUseAccessorFor(bean) // + ? Optional.ofNullable(proxyIdAccessor.getIdentifierFrom(bean))// : super.getIdentifier(); } } diff --git a/src/main/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImpl.java b/src/main/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImpl.java index 7213f1f08..d6a723ba6 100644 --- a/src/main/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImpl.java +++ b/src/main/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImpl.java @@ -15,9 +15,7 @@ */ package org.springframework.data.jpa.mapping; -import java.beans.PropertyDescriptor; import java.lang.annotation.Annotation; -import java.lang.reflect.Field; import java.util.Collection; import java.util.Collections; import java.util.HashSet; @@ -49,6 +47,7 @@ import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty; import org.springframework.data.mapping.model.Property; import org.springframework.data.mapping.model.SimpleTypeHolder; import org.springframework.data.util.ClassTypeInformation; +import org.springframework.data.util.Optionals; import org.springframework.data.util.TypeInformation; import org.springframework.util.Assert; @@ -91,8 +90,8 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty associationTargetType; + private final Optional usePropertyAccess; + private final Optional> associationTargetType; private final boolean updateable; private final JpaMetamodel metamodel; @@ -112,7 +111,7 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty getActualType() { - return associationTargetType == null ? super.getActualType() : associationTargetType.getType(); + return associationTargetType.isPresent() ? associationTargetType.get().getType() : super.getActualType(); } /* @@ -132,8 +131,8 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty> getPersistentEntityType() { - return associationTargetType == null ? super.getPersistentEntityType() - : Collections.singleton(associationTargetType); + return associationTargetType.isPresent() ? Collections.singleton(associationTargetType.get()) + : super.getPersistentEntityType(); } /* @@ -142,14 +141,7 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty annotation : ID_ANNOTATIONS) { - if (isAnnotationPresent(annotation)) { - return true; - } - } - - return false; + return ID_ANNOTATIONS.stream().anyMatch(it -> isAnnotationPresent(it)); } /* @@ -168,17 +160,11 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty annotationType : ASSOCIATION_ANNOTATIONS) { - if (findAnnotation(annotationType) != null) { - return true; - } - } - - if (getType().isAnnotationPresent(Embeddable.class)) { + if (ASSOCIATION_ANNOTATIONS.stream().anyMatch(it -> findAnnotation(it).isPresent())) { return true; } - return false; + return getType().isAnnotationPresent(Embeddable.class); } /* @@ -205,7 +191,7 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty super.usePropertyAccess()); } /* @@ -234,29 +220,29 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty detectPropertyAccess() { Optional accessType = findAnnotation( org.springframework.data.annotation.AccessType.class); if (accessType.isPresent()) { - return Type.PROPERTY.equals(accessType.get().value()); + return accessType.map(it -> Type.PROPERTY.equals(it.value())); } Optional access = findAnnotation(Access.class); if (access.isPresent()) { - return AccessType.PROPERTY.equals(access.get().value()); + return access.map(it -> AccessType.PROPERTY.equals(it.value())); } accessType = findPropertyOrOwnerAnnotation(org.springframework.data.annotation.AccessType.class); if (accessType.isPresent()) { - return Type.PROPERTY.equals(accessType.get().value()); + return accessType.map(it -> Type.PROPERTY.equals(it.value())); } access = findPropertyOrOwnerAnnotation(Access.class); - return access.map(t -> AccessType.PROPERTY.equals(t.value())).orElse(null); + return access.map(t -> AccessType.PROPERTY.equals(t.value())); } /** @@ -264,42 +250,31 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty detectAssociationTargetType() { + private Optional> detectAssociationTargetType() { - for (Class associationAnnotation : ASSOCIATION_ANNOTATIONS) { - - Optional annotation = findAnnotation(associationAnnotation); - if(annotation.isPresent()) { - - Object targetEntity = AnnotationUtils.getValue(annotation.get(), "targetEntity"); - - if (targetEntity != null && !void.class.equals(targetEntity)) { - return ClassTypeInformation.from((Class) targetEntity); - } - } + if (!isAssociation()) { + return Optional.empty(); } - return null; + return ASSOCIATION_ANNOTATIONS.stream()// + .flatMap(it -> Optionals.toStream(findAnnotation(it)))// + .map(it -> AnnotationUtils.getValue(it, "targetEntity"))// + .filter(it -> it != null && !void.class.equals(it))// + .map(it -> (Class) it)// + .findFirst().map(it -> (TypeInformation) ClassTypeInformation.from(it)); } /** - * Checks whether {@code updateable} attribute of any of the {@link #UPDATEABLE_ANNOTATIONS} is configured to + * Checks whether {@code updatable} attribute of any of the {@link #UPDATEABLE_ANNOTATIONS} is configured to * {@literal true}. * * @return */ private final boolean detectUpdatability() { - for (Class annotationType : UPDATEABLE_ANNOTATIONS) { - - Optional annotation = findAnnotation(annotationType); - - if (annotation.isPresent() && AnnotationUtils.getValue(annotation.get(), "updatable").equals(Boolean.FALSE)) { - return false; - } - } - - return true; + return !UPDATEABLE_ANNOTATIONS.stream()// + .flatMap(it -> Optionals.toStream(findAnnotation(it)))// + .map(it -> AnnotationUtils.getValue(it, "updatable"))// + .anyMatch(it -> it.equals(Boolean.FALSE)); } - } diff --git a/src/test/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImplUnitTests.java b/src/test/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImplUnitTests.java index 04c7e4c64..45e9bdcfd 100644 --- a/src/test/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImplUnitTests.java +++ b/src/test/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImplUnitTests.java @@ -15,8 +15,9 @@ */ package org.springframework.data.jpa.mapping; +import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; +import static org.junit.Assert.assertThat; import static org.mockito.Mockito.*; import java.util.Collections; @@ -157,6 +158,15 @@ public class JpaPersistentPropertyImplUnitTests { assertThat(getProperty(Sample.class, "other").isEntity(), is(false)); } + @Test // DATAJPA-1064 + public void simplePropertyIsNotConsideredAnAssociation() { + + JpaPersistentEntityImpl entity = context.getRequiredPersistentEntity(WithReadOnly.class); + JpaPersistentProperty property = entity.getRequiredPersistentProperty("updatable"); + + assertThat(property.isAssociation()).isFalse(); + } + private JpaPersistentProperty getProperty(Class ownerType, String propertyName) { JpaPersistentEntity entity = context.getRequiredPersistentEntity(ownerType);