diff --git a/src/main/java/org/springframework/data/convert/BytecodeGeneratingEntityInstantiator.java b/src/main/java/org/springframework/data/convert/BytecodeGeneratingEntityInstantiator.java index 4cd16ddf8..96152d8c4 100644 --- a/src/main/java/org/springframework/data/convert/BytecodeGeneratingEntityInstantiator.java +++ b/src/main/java/org/springframework/data/convert/BytecodeGeneratingEntityInstantiator.java @@ -42,14 +42,13 @@ import org.springframework.util.Assert; import org.springframework.util.ClassUtils; /** - * An {@link EntityInstantiator} that can generate byte code to speed-up dynamic object instantiation. - *
- * Uses the {@link PersistentEntity}'s {@link PreferredConstructor} to instantiate an instance of the entity by - * dynamically generating factory methods with appropriate constructor invocations via ASM. - *
- * If we cannot generate byte code for a type, we gracefully fall-back to the {@link ReflectionEntityInstantiator}.
+ * An {@link EntityInstantiator} that can generate byte code to speed-up dynamic object instantiation. Uses the
+ * {@link PersistentEntity}'s {@link PreferredConstructor} to instantiate an instance of the entity by dynamically
+ * generating factory methods with appropriate constructor invocations via ASM. If we cannot generate byte code for a
+ * type, we gracefully fall-back to the {@link ReflectionEntityInstantiator}.
*
* @author Thomas Darimont
+ * @author Oliver Gierke
* @since 1.10
*/
public enum BytecodeGeneratingEntityInstantiator implements EntityInstantiator {
@@ -68,15 +67,13 @@ public enum BytecodeGeneratingEntityInstantiator implements EntityInstantiator {
public > T createInstance(E entity,
ParameterValueProvider provider) {
- EntityInstantiator ei = this.entityInstantiators.get(entity.getTypeInformation());
+ EntityInstantiator instantiator = this.entityInstantiators.get(entity.getTypeInformation());
- if (ei != null) {
- return ei.createInstance(entity, provider);
+ if (instantiator == null) {
+ instantiator = potentiallyCreateAndRegisterEntityInstantiator(entity);
}
- ei = potentiallyCreateAndRegisterEntityInstantiator(entity);
-
- return ei.createInstance(entity, provider);
+ return instantiator.createInstance(entity, provider);
}
/**
@@ -86,20 +83,20 @@ public enum BytecodeGeneratingEntityInstantiator implements EntityInstantiator {
private synchronized EntityInstantiator potentiallyCreateAndRegisterEntityInstantiator(PersistentEntity, ?> entity) {
Map > T createInstance(E entity,
ParameterValueProvider provider) {
- Object[] params = extractInvocationArguments(provider, entity.getPersistenceConstructor());
+ Object[] params = extractInvocationArguments(entity.getPersistenceConstructor(), provider);
+
try {
return (T) instantiator.newInstance(params);
} catch (Exception e) {
@@ -223,19 +230,22 @@ public enum BytecodeGeneratingEntityInstantiator implements EntityInstantiator {
}
/**
- * @param provider
- * @param prefCtor
+ * Extracts the arguments required to invoce the given constructor from the given {@link ParameterValueProvider}.
+ *
+ * @param constructor can be {@literal null}.
+ * @param provider can be {@literal null}.
* @return
*/
private , T> Object[] extractInvocationArguments(
- ParameterValueProvider provider, PreferredConstructor extends T, P> prefCtor) {
+ PreferredConstructor extends T, P> constructor, ParameterValueProvider provider) {
- if (provider == null || prefCtor == null || !prefCtor.hasParameters()) {
+ if (provider == null || constructor == null || !constructor.hasParameters()) {
return EMPTY_ARRAY;
}
List