Skip to content

Commit 6b0e774

Browse files
mp911deodrotbohm
authored andcommitted
DATACMNS-1364 - Polishing.
Use weak references in annotation and property annotation cache to retain references until the last GC root is cleared. Remove trailing whitespaces. Reformat. Original pull request: #304.
1 parent 9edf2e5 commit 6b0e774

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java

+10-7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.Serializable;
2222
import java.lang.annotation.Annotation;
2323
import java.util.*;
24+
import java.util.concurrent.ConcurrentHashMap;
2425
import java.util.stream.Collectors;
2526

2627
import org.springframework.core.annotation.AnnotatedElementUtils;
@@ -38,6 +39,7 @@
3839
import org.springframework.util.Assert;
3940
import org.springframework.util.CollectionUtils;
4041
import org.springframework.util.ConcurrentReferenceHashMap;
42+
import org.springframework.util.ConcurrentReferenceHashMap.ReferenceType;
4143
import org.springframework.util.MultiValueMap;
4244
import org.springframework.util.StringUtils;
4345

@@ -105,8 +107,9 @@ public BasicPersistentEntity(TypeInformation<T> information, @Nullable Comparato
105107
this.associations = comparator == null ? new HashSet<>() : new TreeSet<>(new AssociationComparator<>(comparator));
106108

107109
this.propertyCache = new ConcurrentHashMap<>();
108-
this.annotationCache = new ConcurrentReferenceHashMap<>();
109-
this.propertyAnnotationCache = CollectionUtils.toMultiValueMap(new ConcurrentReferenceHashMap<>());
110+
this.annotationCache = new ConcurrentReferenceHashMap<>(16, ReferenceType.WEAK);
111+
this.propertyAnnotationCache = CollectionUtils
112+
.toMultiValueMap(new ConcurrentReferenceHashMap<>(16, ReferenceType.WEAK));
110113
this.propertyAccessorFactory = BeanWrapperPropertyAccessorFactory.INSTANCE;
111114
this.typeAlias = Lazy.of(() -> getAliasFromAnnotation(getType()));
112115
this.isNewStrategy = Lazy.of(() -> Persistable.class.isAssignableFrom(information.getType()) //
@@ -236,7 +239,7 @@ public void addPersistentProperty(P property) {
236239
}
237240
}
238241

239-
/*
242+
/*
240243
* (non-Javadoc)
241244
* @see org.springframework.data.mapping.model.MutablePersistentEntity#setEvaluationContextProvider(org.springframework.data.spel.EvaluationContextProvider)
242245
*/
@@ -469,7 +472,7 @@ public IdentifierAccessor getIdentifierAccessor(Object bean) {
469472
return hasIdProperty() ? new IdPropertyIdentifierAccessor(this, bean) : new AbsentIdentifierAccessor(bean);
470473
}
471474

472-
/*
475+
/*
473476
* (non-Javadoc)
474477
* @see org.springframework.data.mapping.PersistentEntity#isNew(java.lang.Object)
475478
*/
@@ -481,7 +484,7 @@ public boolean isNew(Object bean) {
481484
return isNewStrategy.get().isNew(bean);
482485
}
483486

484-
/*
487+
/*
485488
* (non-Javadoc)
486489
* @see org.springframework.data.mapping.PersistentEntity#isImmutable()
487490
*/
@@ -516,7 +519,7 @@ protected EvaluationContext getEvaluationContext(Object rootObject) {
516519
* Returns the default {@link IsNewStrategy} to be used. Will be a {@link PersistentEntityIsNewStrategy} by default.
517520
* Note, that this strategy only gets used if the entity doesn't implement {@link Persistable} as this indicates the
518521
* user wants to be in control over whether an entity is new or not.
519-
*
522+
*
520523
* @return
521524
* @since 2.1
522525
*/
@@ -526,7 +529,7 @@ protected IsNewStrategy getFallbackIsNewStrategy() {
526529

527530
/**
528531
* Verifies the given bean type to no be {@literal null} and of the type of the current {@link PersistentEntity}.
529-
*
532+
*
530533
* @param bean must not be {@literal null}.
531534
*/
532535
private final void verifyBeanType(Object bean) {

0 commit comments

Comments
 (0)