From 28b7e0439d2e521fb629e1f0f86ee39d1c43a9d7 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 21 Oct 2014 12:12:22 +0200 Subject: [PATCH] DATACMNS-578 - Polishing. Added some JavaDoc where missing. Better variable names and slightly parameter ordering changes. Formatting in the unit tests and reference to the original ticket. Original pull request: #98. --- .../BytecodeGeneratingEntityInstantiator.java | 60 +++++++++++-------- ...GeneratingEntityInstantiatorUnitTests.java | 37 ++++++------ 2 files changed, 52 insertions(+), 45 deletions(-) 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 , P extends PersistentProperty

> 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, EntityInstantiator> map = this.entityInstantiators; + EntityInstantiator instantiator = map.get(entity.getTypeInformation()); - EntityInstantiator ei = map.get(entity.getTypeInformation()); - if (ei != null) { - return ei; + if (instantiator != null) { + return instantiator; } - ei = createEntityInstantiator(entity); + instantiator = createEntityInstantiator(entity); map = new HashMap, EntityInstantiator>(map); - map.put(entity.getTypeInformation(), ei); + map.put(entity.getTypeInformation(), instantiator); this.entityInstantiators = map; - return ei; + return instantiator; } /** @@ -194,7 +191,10 @@ public enum BytecodeGeneratingEntityInstantiator implements EntityInstantiator { } /** + * Adapter to forward an invocation of the {@link EntityInstantiator} API to an {@link ObjectInstantiator}. + * * @author Thomas Darimont + * @author Oliver Gierke */ private static class EntityInstantiatorAdapter implements EntityInstantiator { @@ -203,18 +203,25 @@ public enum BytecodeGeneratingEntityInstantiator implements EntityInstantiator { private final ObjectInstantiator instantiator; /** - * @param instantiator + * Creates a new {@link EntityInstantiatorAdapter} for the given {@link ObjectInstantiator}. + * + * @param instantiator must not be {@literal null}. */ public EntityInstantiatorAdapter(ObjectInstantiator instantiator) { this.instantiator = instantiator; } + /* + * (non-Javadoc) + * @see org.springframework.data.convert.EntityInstantiator#createInstance(org.springframework.data.mapping.PersistentEntity, org.springframework.data.mapping.model.ParameterValueProvider) + */ @Override @SuppressWarnings("unchecked") public , P extends PersistentProperty

> 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 prefCtor) { + PreferredConstructor constructor, ParameterValueProvider

provider) { - if (provider == null || prefCtor == null || !prefCtor.hasParameters()) { + if (provider == null || constructor == null || !constructor.hasParameters()) { return EMPTY_ARRAY; } List params = new ArrayList(); - for (Parameter parameter : prefCtor.getParameters()) { + + for (Parameter parameter : constructor.getParameters()) { params.add(provider.getParameterValue(parameter)); } diff --git a/src/test/java/org/springframework/data/convert/BytecodeGeneratingEntityInstantiatorUnitTests.java b/src/test/java/org/springframework/data/convert/BytecodeGeneratingEntityInstantiatorUnitTests.java index f0e6a41ac..17916b70a 100644 --- a/src/test/java/org/springframework/data/convert/BytecodeGeneratingEntityInstantiatorUnitTests.java +++ b/src/test/java/org/springframework/data/convert/BytecodeGeneratingEntityInstantiatorUnitTests.java @@ -47,6 +47,7 @@ import org.springframework.util.ReflectionUtils.FieldCallback; * Unit tests for {@link BytecodeGeneratingEntityInstantiator}. * * @author Thomas Darimont + * @author Oliver Gierke */ @RunWith(MockitoJUnitRunner.class) public class BytecodeGeneratingEntityInstantiatorUnitTests

> { @@ -88,7 +89,7 @@ public class BytecodeGeneratingEntityInstantiatorUnitTests