Reuse generated entity instantiators from ClassGeneratingEntityInstantiator instances.

We now reuse generated ObjectInstantiator classes instead of attempting to redefine classes with the same name. Redefinition leads to LinkageError and thus to the use of reflection-based instantiators and an increased allocation rate due to the failed attempt of class redeclaration.

Closes: #2446.
This commit is contained in:
Mark Paluch
2021-09-09 15:18:30 +02:00
parent 242a71ca56
commit f4805503c8
2 changed files with 46 additions and 3 deletions

View File

@@ -23,6 +23,7 @@ import static org.springframework.data.util.ClassTypeInformation.from;
import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;
import org.junit.jupiter.api.Test;
@@ -40,6 +41,8 @@ import org.springframework.data.mapping.PreferredConstructor.Parameter;
import org.springframework.data.mapping.model.ClassGeneratingEntityInstantiator.ObjectInstantiator;
import org.springframework.data.mapping.model.ClassGeneratingEntityInstantiatorUnitTests.Outer.Inner;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.ReflectionUtils;
/**
@@ -211,6 +214,9 @@ class ClassGeneratingEntityInstantiatorUnitTests<P extends PersistentProperty<P>
assertThat(instance).isInstanceOf(ObjCtorNoArgs.class);
assertThat(((ObjCtorNoArgs) instance).ctorInvoked).isTrue();
});
ClassGeneratingEntityInstantiator classGeneratingEntityInstantiator = new ClassGeneratingEntityInstantiator();
classGeneratingEntityInstantiator.createInstance(entity, provider);
}
@Test // DATACMNS-578, DATACMNS-1126
@@ -361,6 +367,26 @@ class ClassGeneratingEntityInstantiatorUnitTests<P extends PersistentProperty<P>
assertThat(this.instance.shouldUseReflectionEntityInstantiator(entity)).isTrue();
}
@Test // GH-2446
void shouldReuseGeneratedClasses() {
prepareMocks(ProtectedInnerClass.class);
this.instance.createInstance(entity, provider);
ClassGeneratingEntityInstantiator instantiator = new ClassGeneratingEntityInstantiator();
instantiator.createInstance(entity, provider);
Map<TypeInformation<?>, EntityInstantiator> first = (Map<TypeInformation<?>, EntityInstantiator>) ReflectionTestUtils
.getField(this.instance, "entityInstantiators");
Map<TypeInformation<?>, EntityInstantiator> second = (Map<TypeInformation<?>, EntityInstantiator>) ReflectionTestUtils
.getField(instantiator, "entityInstantiators");
assertThat(first.get(null)).isNotNull().isNotInstanceOf(Enum.class);
assertThat(second.get(null)).isNotNull().isNotInstanceOf(Enum.class);
}
@Test // DATACMNS-1422
void shouldUseReflectionIfFrameworkTypesNotVisible() throws Exception {