Skip to content

Commit c866b42

Browse files
committed
Polishing.
A few further tweaks: ClassTypeInformation now never unwraps the user type to consistently work with the type originally presented in the lookup. That allows us to get rid of the custom equals(…)/hashCode() methods and removes the need to deal with original and user type in the same CTI instance. The augmented caching of PersistentEntities in AbstractMappingContext now primarily happens in doAddPersistentEntity(…). We still need to implement a cache manipulation for the case that a user type has caused the creation of a PersistentEntity first and we see a lookup for the proxy type later. Before actually processing the TypeInformation we now try to lookup an already existing PersistentEntity for the user type and augment the cache if one was found. See #2485 Original pull request: #2486.
1 parent ac9d116 commit c866b42

File tree

3 files changed

+27
-35
lines changed

3 files changed

+27
-35
lines changed

src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -354,12 +354,12 @@ protected Optional<E> addPersistentEntity(TypeInformation<?> typeInformation) {
354354

355355
read.lock();
356356

357-
Optional<E> persistentEntity = persistentEntities.computeIfAbsent(typeInformation,
358-
it -> persistentEntities.get(typeInformation.getUserTypeInformation()));
357+
Optional<E> persistentEntity = persistentEntities.get(typeInformation);
359358

360359
if (persistentEntity != null) {
361360
return persistentEntity;
362361
}
362+
363363
} finally {
364364
read.unlock();
365365
}
@@ -370,12 +370,15 @@ protected Optional<E> addPersistentEntity(TypeInformation<?> typeInformation) {
370370

371371
write.lock();
372372

373-
TypeInformation<?> typeInformationToUse = typeInformation.getUserTypeInformation();
374-
entity = doAddPersistentEntity(typeInformation.getUserTypeInformation());
373+
Optional<E> userTypeEntity = persistentEntities.get(typeInformation.getUserTypeInformation());
375374

376-
if (!typeInformationToUse.equals(typeInformation)) {
377-
persistentEntities.put(typeInformation, Optional.of(entity));
375+
if (userTypeEntity != null) {
376+
persistentEntities.put(typeInformation, userTypeEntity);
377+
return userTypeEntity;
378378
}
379+
380+
entity = doAddPersistentEntity(typeInformation);
381+
379382
} catch (BeansException e) {
380383
throw new MappingException(e.getMessage(), e);
381384
} finally {
@@ -392,19 +395,26 @@ protected Optional<E> addPersistentEntity(TypeInformation<?> typeInformation) {
392395

393396
private E doAddPersistentEntity(TypeInformation<?> typeInformation) {
394397

398+
TypeInformation<?> userTypeInformation = typeInformation.getUserTypeInformation();
399+
395400
try {
396401

397-
Class<?> type = typeInformation.getType();
402+
Class<?> type = userTypeInformation.getType();
398403

399-
E entity = createPersistentEntity(typeInformation);
404+
E entity = createPersistentEntity(userTypeInformation);
400405
entity.setEvaluationContextProvider(evaluationContextProvider);
401406

402407
// Eagerly cache the entity as we might have to find it during recursive lookups.
403-
persistentEntities.put(typeInformation, Optional.of(entity));
408+
persistentEntities.put(userTypeInformation, Optional.of(entity));
404409

405-
PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(type);
410+
// Cache original TypeInformation as well.
411+
if (!userTypeInformation.equals(typeInformation)) {
412+
persistentEntities.put(typeInformation, Optional.of(entity));
413+
}
406414

415+
PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(type);
407416
Map<String, PropertyDescriptor> descriptors = new HashMap<>();
417+
408418
for (PropertyDescriptor descriptor : pds) {
409419
descriptors.put(descriptor.getName(), descriptor);
410420
}
@@ -420,8 +430,12 @@ private E doAddPersistentEntity(TypeInformation<?> typeInformation) {
420430
}
421431

422432
return entity;
433+
423434
} catch (RuntimeException e) {
435+
436+
persistentEntities.remove(userTypeInformation);
424437
persistentEntities.remove(typeInformation);
438+
425439
throw e;
426440
}
427441
}

src/main/java/org/springframework/data/util/ClassTypeInformation.java

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.springframework.util.Assert;
3333
import org.springframework.util.ConcurrentReferenceHashMap;
3434
import org.springframework.util.ConcurrentReferenceHashMap.ReferenceType;
35-
import org.springframework.util.ObjectUtils;
3635

3736
/**
3837
* {@link TypeInformation} for a plain {@link Class}.
@@ -91,7 +90,7 @@ public static <S> TypeInformation<S> fromReturnTypeOf(Method method) {
9190
* @param type
9291
*/
9392
ClassTypeInformation(Class<S> type) {
94-
super(ProxyUtils.getUserClass(type), getTypeVariableMap(type));
93+
super(type, getTypeVariableMap(type));
9594
this.type = type;
9695
}
9796

@@ -178,26 +177,4 @@ public TypeInformation<? extends S> specialize(ClassTypeInformation<?> type) {
178177
public String toString() {
179178
return type.getName();
180179
}
181-
182-
@Override
183-
public boolean equals(Object o) {
184-
if (this == o) {
185-
return true;
186-
}
187-
188-
if (!super.equals(o)) {
189-
return false;
190-
}
191-
192-
ClassTypeInformation<?> that = (ClassTypeInformation<?>) o;
193-
return ObjectUtils.nullSafeEquals(type, that.type);
194-
}
195-
196-
@Override
197-
public int hashCode() {
198-
199-
int result = super.hashCode();
200-
result = 31 * result + ObjectUtils.nullSafeHashCode(type);
201-
return result;
202-
}
203180
}

src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939

4040
import org.junit.jupiter.api.BeforeEach;
4141
import org.junit.jupiter.api.Test;
42-
4342
import org.springframework.aop.SpringProxy;
4443
import org.springframework.aop.framework.Advised;
4544
import org.springframework.context.ApplicationContext;
@@ -343,6 +342,7 @@ void contextSeesUserTypeBeforeProxy() {
343342
assertThat(context.hasPersistentEntityFor(Base$$SpringProxy$873fa2e.class)).isTrue();
344343

345344
assertThat(context.getPersistentEntities()).hasSize(1); // only one distinct instance
345+
assertThat(persistentEntity).isSameAs(persistentEntityForProxy);
346346
}
347347

348348
@Test // GH-2485
@@ -362,6 +362,7 @@ void contextSeesProxyBeforeUserType() {
362362
persistentEntity.getTypeInformation().getType().equals(Base.class);
363363

364364
assertThat(context.getPersistentEntities()).hasSize(1); // only one distinct instance
365+
assertThat(persistentEntity).isSameAs(persistentEntityForProxy);
365366
}
366367

367368
private static void assertHasEntityFor(Class<?> type, SampleMappingContext context, boolean expected) {

0 commit comments

Comments
 (0)