diff --git a/src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java b/src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java index dbea5f9dc1..fa8a83d7d7 100644 --- a/src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java +++ b/src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java @@ -37,7 +37,6 @@ import org.springframework.data.mapping.MappingException; import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.mapping.PersistentProperty; -import org.springframework.data.util.ClassTypeInformation; import org.springframework.data.util.Lazy; import org.springframework.data.util.Optionals; import org.springframework.data.util.ReflectionUtils; @@ -197,10 +196,12 @@ public boolean isTransient() { return isTransient.get(); } + @Override public boolean isIdProperty() { return isId.get(); } + @Override public boolean isVersionProperty() { return isVersion.get(); } @@ -226,6 +227,7 @@ public boolean isWritable() { * @param annotationType must not be {@literal null}. * @return {@literal null} if annotation type not found on property. */ + @Override @Nullable public A findAnnotation(Class annotationType) { @@ -262,11 +264,12 @@ public A findPropertyOrOwnerAnnotation(Class annotatio } /** - * Returns whether the property carries the an annotation of the given type. + * Returns whether the property carries the annotation of the given type. * * @param annotationType the annotation type to look up. - * @return + * @return {@literal true} if the annotation is present, {@literal false} otherwise. */ + @Override public boolean isAnnotationPresent(Class annotationType) { return doFindAnnotation(annotationType).isPresent(); } diff --git a/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java b/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java index 2e16977313..95ef669468 100644 --- a/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java +++ b/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java @@ -135,7 +135,6 @@ public BasicPersistentEntity(TypeInformation information, @Nullable Comparato @Nullable @Override - @SuppressWarnings("unchecked") public PreferredConstructor getPersistenceConstructor() { return creator instanceof PreferredConstructor ? (PreferredConstructor) creator : null; } @@ -151,36 +150,89 @@ public boolean isCreatorArgument(PersistentProperty property) { return creator != null && creator.isCreatorParameter(property); } + /** + * Calculates the {@link Alias} to be used for the given type. + * + * @param type must not be {@literal null}. + * @return the alias for {@code type} + */ + private static Alias getAliasFromAnnotation(Class type) { + + TypeAlias typeAlias = AnnotatedElementUtils.findMergedAnnotation(type, TypeAlias.class); + + if (typeAlias != null && StringUtils.hasText(typeAlias.value())) { + return Alias.of(typeAlias.value()); + } + + return Alias.empty(); + } + + @Override public boolean isIdProperty(PersistentProperty property) { return idProperty != null && idProperty.equals(property); } + @Override public boolean isVersionProperty(PersistentProperty property) { return versionProperty != null && versionProperty.equals(property); } + @Override public String getName() { return getType().getName(); } + @Override @Nullable public P getIdProperty() { return idProperty; } + @Override @Nullable public P getVersionProperty() { return versionProperty; } + @Override public boolean hasIdProperty() { return idProperty != null; } + @Override public boolean hasVersionProperty() { return versionProperty != null; } + @Override + public void setEvaluationContextProvider(EvaluationContextProvider provider) { + this.evaluationContextProvider = provider; + } + + /** + * Returns the given property if it is a better candidate for the id property than the current id property. + * + * @param property the new id property candidate, will never be {@literal null}. + * @return the given id property or {@literal null} if the given property is not an id property. + */ + @Nullable + protected P returnPropertyIfBetterIdPropertyCandidateOrNull(P property) { + + if (!property.isIdProperty()) { + return null; + } + + P idProperty = this.idProperty; + + if (idProperty != null) { + throw new MappingException(String.format("Attempt to add id property %s but already have property %s registered " + + "as id; Check your mapping configuration ", property.getField(), idProperty.getField())); + } + + return property; + } + + @Override public void addPersistentProperty(P property) { Assert.notNull(property, "Property must not be null"); @@ -230,41 +282,6 @@ public void addPersistentProperty(P property) { } } - @Override - public void setEvaluationContextProvider(EvaluationContextProvider provider) { - this.evaluationContextProvider = provider; - } - - /** - * Returns the given property if it is a better candidate for the id property than the current id property. - * - * @param property the new id property candidate, will never be {@literal null}. - * @return the given id property or {@literal null} if the given property is not an id property. - */ - @Nullable - protected P returnPropertyIfBetterIdPropertyCandidateOrNull(P property) { - - if (!property.isIdProperty()) { - return null; - } - - P idProperty = this.idProperty; - - if (idProperty != null) { - throw new MappingException(String.format("Attempt to add id property %s but already have property %s registered " - + "as id; Check your mapping configuration ", property.getField(), idProperty.getField())); - } - - return property; - } - - public void addAssociation(Association

association) { - - Assert.notNull(association, "Association must not be null"); - - associations.add(association); - } - @Override @Nullable public P getPersistentProperty(String name) { @@ -306,27 +323,29 @@ private List

doFindPersistentProperty(Class annotationT .filter(it -> it.isAnnotationPresent(annotationType)).collect(Collectors.toList()); } + @Override + public void addAssociation(Association

association) { + + Assert.notNull(association, "Association must not be null"); + + associations.add(association); + } + + @Override public Class getType() { return information.getType(); } + @Override public Alias getTypeAlias() { return typeAlias.get(); } + @Override public TypeInformation getTypeInformation() { return information; } - public void doWithProperties(PropertyHandler

handler) { - - Assert.notNull(handler, "PropertyHandler must not be null"); - - for (P property : persistentPropertiesCache) { - handler.doWithPersistentProperty(property); - } - } - @Override public void doWithProperties(SimplePropertyHandler handler) { @@ -337,20 +356,22 @@ public void doWithProperties(SimplePropertyHandler handler) { } } - public void doWithAssociations(AssociationHandler

handler) { + @Override + public void doWithProperties(PropertyHandler

handler) { - Assert.notNull(handler, "Handler must not be null"); + Assert.notNull(handler, "PropertyHandler must not be null"); - for (Association

association : associations) { - handler.doWithAssociation(association); + for (P property : persistentPropertiesCache) { + handler.doWithPersistentProperty(property); } } - public void doWithAssociations(SimpleAssociationHandler handler) { + @Override + public void doWithAssociations(AssociationHandler

handler) { Assert.notNull(handler, "Handler must not be null"); - for (Association> association : associations) { + for (Association

association : associations) { handler.doWithAssociation(association); } } @@ -373,11 +394,13 @@ private Optional doFindAnnotation(Class annotationT it -> Optional.ofNullable(AnnotatedElementUtils.findMergedAnnotation(getType(), it))); } - public void verify() { + @Override + public void doWithAssociations(SimpleAssociationHandler handler) { - if (comparator != null) { - properties.sort(comparator); - persistentPropertiesCache.sort(comparator); + Assert.notNull(handler, "Handler must not be null"); + + for (Association> association : associations) { + handler.doWithAssociation(association); } } @@ -471,16 +494,13 @@ protected EvaluationContext getEvaluationContext(Object rootObject, ExpressionDe return evaluationContextProvider.getEvaluationContext(rootObject, dependencies); } - /** - * Returns the default {@link IsNewStrategy} to be used. Will be a {@link PersistentEntityIsNewStrategy} by default. - * Note, that this strategy only gets used if the entity doesn't implement {@link Persistable} as this indicates the - * user wants to be in control over whether an entity is new or not. - * - * @return - * @since 2.1 - */ - protected IsNewStrategy getFallbackIsNewStrategy() { - return PersistentEntityIsNewStrategy.of(this); + @Override + public void verify() { + + if (comparator != null) { + properties.sort(comparator); + persistentPropertiesCache.sort(comparator); + } } /** @@ -496,20 +516,15 @@ private void verifyBeanType(Object bean) { } /** - * Calculates the {@link Alias} to be used for the given type. + * Returns the default {@link IsNewStrategy} to be used. Will be a {@link PersistentEntityIsNewStrategy} by default. + * Note, that this strategy only gets used if the entity doesn't implement {@link Persistable} as this indicates the + * user wants to be in control over whether an entity is new or not. * - * @param type must not be {@literal null}. - * @return + * @return the fallback {@link IsNewStrategy}. + * @since 2.1 */ - private static Alias getAliasFromAnnotation(Class type) { - - TypeAlias typeAlias = AnnotatedElementUtils.findMergedAnnotation(type, TypeAlias.class); - - if (typeAlias != null && StringUtils.hasText(typeAlias.value())) { - return Alias.of(typeAlias.value()); - } - - return Alias.empty(); + protected IsNewStrategy getFallbackIsNewStrategy() { + return PersistentEntityIsNewStrategy.of(this); } /** @@ -546,6 +561,7 @@ private static final class AssociationComparator

this.delegate = delegate; } + @Override public int compare(@Nullable Association

left, @Nullable Association

right) { if (left == null) { diff --git a/src/main/java/org/springframework/data/mapping/model/KotlinValueUtils.java b/src/main/java/org/springframework/data/mapping/model/KotlinValueUtils.java index 3972b3d3b4..86d867e6e2 100644 --- a/src/main/java/org/springframework/data/mapping/model/KotlinValueUtils.java +++ b/src/main/java/org/springframework/data/mapping/model/KotlinValueUtils.java @@ -287,7 +287,7 @@ public Class getParameterType() { } /** - * @return {@code true} if the value hierarchy applies boxing. + * @return {@literal true} if the value hierarchy applies boxing. */ public boolean appliesBoxing() { return applyBoxing; diff --git a/src/main/java/org/springframework/data/mapping/model/PersistentEntityParameterValueProvider.java b/src/main/java/org/springframework/data/mapping/model/PersistentEntityParameterValueProvider.java index 2124ff9c51..0a7fa385f5 100644 --- a/src/main/java/org/springframework/data/mapping/model/PersistentEntityParameterValueProvider.java +++ b/src/main/java/org/springframework/data/mapping/model/PersistentEntityParameterValueProvider.java @@ -42,7 +42,7 @@ public class PersistentEntityParameterValueProvider

entity, PropertyValueProvider

provider, - Object parent) { + @Nullable Object parent) { this.entity = entity; this.provider = provider; this.parent = parent; @@ -53,6 +53,7 @@ private static Object getTransientDefault(Class parameterType) { return parameterType.isPrimitive() ? ReflectionUtils.getPrimitiveDefault(parameterType) : null; } + @Override @Nullable @SuppressWarnings("unchecked") public T getParameterValue(Parameter parameter) {