Skip to content

Commit ac9d116

Browse files
committed
Polishing.
Remove isProxyTypeInformation from TypeInformation. Use computeIfAbsent to fall back to user type information when attempting to add a entity to the MappingContext. See #2485 Original pull request: #2486.
1 parent c30f3fc commit ac9d116

File tree

3 files changed

+16
-150
lines changed

3 files changed

+16
-150
lines changed

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

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

355355
read.lock();
356356

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

359360
if (persistentEntity != null) {
360361
return persistentEntity;
361362
}
362-
363-
if(typeInformation.isProxyTypeInformation()) {
364-
365-
TypeInformation<?> userTypeInformation = typeInformation.getUserTypeInformation();
366-
persistentEntity = persistentEntities.get(userTypeInformation);
367-
368-
if (persistentEntity != null) {
369-
persistentEntities.put(typeInformation, persistentEntity);
370-
return persistentEntity;
371-
}
372-
}
373-
374363
} finally {
375364
read.unlock();
376365
}
@@ -380,11 +369,13 @@ protected Optional<E> addPersistentEntity(TypeInformation<?> typeInformation) {
380369
try {
381370

382371
write.lock();
372+
373+
TypeInformation<?> typeInformationToUse = typeInformation.getUserTypeInformation();
383374
entity = doAddPersistentEntity(typeInformation.getUserTypeInformation());
384-
if(typeInformation.isProxyTypeInformation()) {
375+
376+
if (!typeInformationToUse.equals(typeInformation)) {
385377
persistentEntities.put(typeInformation, Optional.of(entity));
386378
}
387-
388379
} catch (BeansException e) {
389380
throw new MappingException(e.getMessage(), e);
390381
} finally {

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,6 @@ default TypeInformation<?> getUserTypeInformation() {
162162
return userType.equals(getType()) ? this : ClassTypeInformation.from(userType);
163163
}
164164

165-
/**
166-
* Returns if {@link #getType()} refers to a proxy or user class.
167-
*
168-
* @return true if type is a proxy.
169-
* @since 2.6
170-
*/
171-
default boolean isProxyTypeInformation() {
172-
return !this.equals(getUserTypeInformation());
173-
}
174-
175165
/**
176166
* Returns a {@link ClassTypeInformation} to represent the {@link TypeInformation} of the raw type of the current
177167
* instance.

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

Lines changed: 10 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,11 @@
3737
import java.util.TreeMap;
3838
import java.util.function.Supplier;
3939

40-
import org.aopalliance.aop.Advice;
4140
import org.junit.jupiter.api.BeforeEach;
4241
import org.junit.jupiter.api.Test;
43-
import org.springframework.aop.Advisor;
42+
4443
import org.springframework.aop.SpringProxy;
45-
import org.springframework.aop.TargetSource;
4644
import org.springframework.aop.framework.Advised;
47-
import org.springframework.aop.framework.AopConfigException;
4845
import org.springframework.context.ApplicationContext;
4946
import org.springframework.context.ApplicationEvent;
5047
import org.springframework.data.annotation.Id;
@@ -60,7 +57,6 @@
6057
import org.springframework.data.util.ClassTypeInformation;
6158
import org.springframework.data.util.StreamUtils;
6259
import org.springframework.data.util.TypeInformation;
63-
import org.springframework.lang.Nullable;
6460

6561
/**
6662
* Unit test for {@link AbstractMappingContext}.
@@ -334,13 +330,15 @@ void shouldNotCreatePersistentEntityForMapButItsGenericTypeArguments() {
334330
void contextSeesUserTypeBeforeProxy() {
335331

336332
SampleMappingContext context = new SampleMappingContext();
337-
BasicPersistentEntity<Object, SamplePersistentProperty> persistentEntity = context.getPersistentEntity(Base.class);
333+
BasicPersistentEntity<Object, SamplePersistentProperty> persistentEntity = context
334+
.getRequiredPersistentEntity(Base.class);
338335
persistentEntity.getTypeInformation().getType().equals(Base.class);
339336

340337
assertThat(context.hasPersistentEntityFor(Base.class)).isTrue();
341338
assertThat(context.hasPersistentEntityFor(Base$$SpringProxy$873fa2e.class)).isFalse();
342339

343-
BasicPersistentEntity<Object, SamplePersistentProperty> persistentEntityForProxy = context.getPersistentEntity(Base$$SpringProxy$873fa2e.class);
340+
BasicPersistentEntity<Object, SamplePersistentProperty> persistentEntityForProxy = context
341+
.getRequiredPersistentEntity(Base$$SpringProxy$873fa2e.class);
344342
persistentEntityForProxy.getTypeInformation().getType().equals(Base.class);
345343
assertThat(context.hasPersistentEntityFor(Base$$SpringProxy$873fa2e.class)).isTrue();
346344

@@ -352,13 +350,15 @@ void contextSeesProxyBeforeUserType() {
352350

353351
SampleMappingContext context = new SampleMappingContext();
354352

355-
BasicPersistentEntity<Object, SamplePersistentProperty> persistentEntityForProxy = context.getPersistentEntity(Base$$SpringProxy$873fa2e.class);
353+
BasicPersistentEntity<Object, SamplePersistentProperty> persistentEntityForProxy = context
354+
.getRequiredPersistentEntity(Base$$SpringProxy$873fa2e.class);
356355
persistentEntityForProxy.getTypeInformation().getType().equals(Base.class);
357356

358357
assertThat(context.hasPersistentEntityFor(Base$$SpringProxy$873fa2e.class)).isTrue();
359358
assertThat(context.hasPersistentEntityFor(Base.class)).isTrue();
360359

361-
BasicPersistentEntity<Object, SamplePersistentProperty> persistentEntity = context.getPersistentEntity(Base.class);
360+
BasicPersistentEntity<Object, SamplePersistentProperty> persistentEntity = context
361+
.getRequiredPersistentEntity(Base.class);
362362
persistentEntity.getTypeInformation().getType().equals(Base.class);
363363

364364
assertThat(context.getPersistentEntities()).hasSize(1); // only one distinct instance
@@ -546,123 +546,8 @@ class WithMap {
546546
Map<MapKey, Integer> mapOfKeyToPerson;
547547
}
548548

549-
static class Base$$SpringProxy$873fa2e extends Base implements SpringProxy, Advised {
550-
551-
@Override
552-
public boolean isFrozen() {
553-
return false;
554-
}
555-
556-
@Override
557-
public boolean isProxyTargetClass() {
558-
return false;
559-
}
560-
561-
@Override
562-
public Class<?>[] getProxiedInterfaces() {
563-
return new Class[0];
564-
}
565-
566-
@Override
567-
public boolean isInterfaceProxied(Class<?> intf) {
568-
return false;
569-
}
570-
571-
@Override
572-
public void setTargetSource(TargetSource targetSource) {
573-
574-
}
575-
576-
@Override
577-
public TargetSource getTargetSource() {
578-
return null;
579-
}
580-
581-
@Override
582-
public void setExposeProxy(boolean exposeProxy) {
583-
584-
}
585-
586-
@Override
587-
public boolean isExposeProxy() {
588-
return false;
589-
}
590-
591-
@Override
592-
public void setPreFiltered(boolean preFiltered) {
593-
594-
}
595-
596-
@Override
597-
public boolean isPreFiltered() {
598-
return false;
599-
}
600-
601-
@Override
602-
public Advisor[] getAdvisors() {
603-
return new Advisor[0];
604-
}
605-
606-
@Override
607-
public void addAdvisor(Advisor advisor) throws AopConfigException {
549+
static abstract class Base$$SpringProxy$873fa2e extends Base implements SpringProxy, Advised {
608550

609-
}
610-
611-
@Override
612-
public void addAdvisor(int pos, Advisor advisor) throws AopConfigException {
613-
614-
}
615-
616-
@Override
617-
public boolean removeAdvisor(Advisor advisor) {
618-
return false;
619-
}
620-
621-
@Override
622-
public void removeAdvisor(int index) throws AopConfigException {
623-
624-
}
625-
626-
@Override
627-
public int indexOf(Advisor advisor) {
628-
return 0;
629-
}
630-
631-
@Override
632-
public boolean replaceAdvisor(Advisor a, Advisor b) throws AopConfigException {
633-
return false;
634-
}
635-
636-
@Override
637-
public void addAdvice(Advice advice) throws AopConfigException {
638-
639-
}
640-
641-
@Override
642-
public void addAdvice(int pos, Advice advice) throws AopConfigException {
643-
644-
}
645-
646-
@Override
647-
public boolean removeAdvice(Advice advice) {
648-
return false;
649-
}
650-
651-
@Override
652-
public int indexOf(Advice advice) {
653-
return 0;
654-
}
655-
656-
@Override
657-
public String toProxyConfigString() {
658-
return null;
659-
}
660-
661-
@Nullable
662-
@Override
663-
public Class<?> getTargetClass() {
664-
return null;
665-
}
666551
}
667552

668553
}

0 commit comments

Comments
 (0)