Skip to content

Commit 28b7e04

Browse files
committed
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.
1 parent 1b9cdc9 commit 28b7e04

File tree

2 files changed

+52
-45
lines changed

2 files changed

+52
-45
lines changed

src/main/java/org/springframework/data/convert/BytecodeGeneratingEntityInstantiator.java

+35-25
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,13 @@
4242
import org.springframework.util.ClassUtils;
4343

4444
/**
45-
* An {@link EntityInstantiator} that can generate byte code to speed-up dynamic object instantiation.
46-
* <p>
47-
* Uses the {@link PersistentEntity}'s {@link PreferredConstructor} to instantiate an instance of the entity by
48-
* dynamically generating factory methods with appropriate constructor invocations via ASM.
49-
* <p>
50-
* If we cannot generate byte code for a type, we gracefully fall-back to the {@link ReflectionEntityInstantiator}.
45+
* An {@link EntityInstantiator} that can generate byte code to speed-up dynamic object instantiation. Uses the
46+
* {@link PersistentEntity}'s {@link PreferredConstructor} to instantiate an instance of the entity by dynamically
47+
* generating factory methods with appropriate constructor invocations via ASM. If we cannot generate byte code for a
48+
* type, we gracefully fall-back to the {@link ReflectionEntityInstantiator}.
5149
*
5250
* @author Thomas Darimont
51+
* @author Oliver Gierke
5352
* @since 1.10
5453
*/
5554
public enum BytecodeGeneratingEntityInstantiator implements EntityInstantiator {
@@ -68,15 +67,13 @@ public enum BytecodeGeneratingEntityInstantiator implements EntityInstantiator {
6867
public <T, E extends PersistentEntity<? extends T, P>, P extends PersistentProperty<P>> T createInstance(E entity,
6968
ParameterValueProvider<P> provider) {
7069

71-
EntityInstantiator ei = this.entityInstantiators.get(entity.getTypeInformation());
70+
EntityInstantiator instantiator = this.entityInstantiators.get(entity.getTypeInformation());
7271

73-
if (ei != null) {
74-
return ei.createInstance(entity, provider);
72+
if (instantiator == null) {
73+
instantiator = potentiallyCreateAndRegisterEntityInstantiator(entity);
7574
}
7675

77-
ei = potentiallyCreateAndRegisterEntityInstantiator(entity);
78-
79-
return ei.createInstance(entity, provider);
76+
return instantiator.createInstance(entity, provider);
8077
}
8178

8279
/**
@@ -86,20 +83,20 @@ public <T, E extends PersistentEntity<? extends T, P>, P extends PersistentPrope
8683
private synchronized EntityInstantiator potentiallyCreateAndRegisterEntityInstantiator(PersistentEntity<?, ?> entity) {
8784

8885
Map<TypeInformation<?>, EntityInstantiator> map = this.entityInstantiators;
86+
EntityInstantiator instantiator = map.get(entity.getTypeInformation());
8987

90-
EntityInstantiator ei = map.get(entity.getTypeInformation());
91-
if (ei != null) {
92-
return ei;
88+
if (instantiator != null) {
89+
return instantiator;
9390
}
9491

95-
ei = createEntityInstantiator(entity);
92+
instantiator = createEntityInstantiator(entity);
9693

9794
map = new HashMap<TypeInformation<?>, EntityInstantiator>(map);
98-
map.put(entity.getTypeInformation(), ei);
95+
map.put(entity.getTypeInformation(), instantiator);
9996

10097
this.entityInstantiators = map;
10198

102-
return ei;
99+
return instantiator;
103100
}
104101

105102
/**
@@ -194,7 +191,10 @@ public Class<?> loadClass(String name, byte[] bytes) {
194191
}
195192

196193
/**
194+
* Adapter to forward an invocation of the {@link EntityInstantiator} API to an {@link ObjectInstantiator}.
195+
*
197196
* @author Thomas Darimont
197+
* @author Oliver Gierke
198198
*/
199199
private static class EntityInstantiatorAdapter implements EntityInstantiator {
200200

@@ -203,18 +203,25 @@ private static class EntityInstantiatorAdapter implements EntityInstantiator {
203203
private final ObjectInstantiator instantiator;
204204

205205
/**
206-
* @param instantiator
206+
* Creates a new {@link EntityInstantiatorAdapter} for the given {@link ObjectInstantiator}.
207+
*
208+
* @param instantiator must not be {@literal null}.
207209
*/
208210
public EntityInstantiatorAdapter(ObjectInstantiator instantiator) {
209211
this.instantiator = instantiator;
210212
}
211213

214+
/*
215+
* (non-Javadoc)
216+
* @see org.springframework.data.convert.EntityInstantiator#createInstance(org.springframework.data.mapping.PersistentEntity, org.springframework.data.mapping.model.ParameterValueProvider)
217+
*/
212218
@Override
213219
@SuppressWarnings("unchecked")
214220
public <T, E extends PersistentEntity<? extends T, P>, P extends PersistentProperty<P>> T createInstance(E entity,
215221
ParameterValueProvider<P> provider) {
216222

217-
Object[] params = extractInvocationArguments(provider, entity.getPersistenceConstructor());
223+
Object[] params = extractInvocationArguments(entity.getPersistenceConstructor(), provider);
224+
218225
try {
219226
return (T) instantiator.newInstance(params);
220227
} catch (Exception e) {
@@ -223,19 +230,22 @@ public <T, E extends PersistentEntity<? extends T, P>, P extends PersistentPrope
223230
}
224231

225232
/**
226-
* @param provider
227-
* @param prefCtor
233+
* Extracts the arguments required to invoce the given constructor from the given {@link ParameterValueProvider}.
234+
*
235+
* @param constructor can be {@literal null}.
236+
* @param provider can be {@literal null}.
228237
* @return
229238
*/
230239
private <P extends PersistentProperty<P>, T> Object[] extractInvocationArguments(
231-
ParameterValueProvider<P> provider, PreferredConstructor<? extends T, P> prefCtor) {
240+
PreferredConstructor<? extends T, P> constructor, ParameterValueProvider<P> provider) {
232241

233-
if (provider == null || prefCtor == null || !prefCtor.hasParameters()) {
242+
if (provider == null || constructor == null || !constructor.hasParameters()) {
234243
return EMPTY_ARRAY;
235244
}
236245

237246
List<Object> params = new ArrayList<Object>();
238-
for (Parameter<?, P> parameter : prefCtor.getParameters()) {
247+
248+
for (Parameter<?, P> parameter : constructor.getParameters()) {
239249
params.add(provider.getParameterValue(parameter));
240250
}
241251

src/test/java/org/springframework/data/convert/BytecodeGeneratingEntityInstantiatorUnitTests.java

+17-20
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
* Unit tests for {@link BytecodeGeneratingEntityInstantiator}.
4848
*
4949
* @author Thomas Darimont
50+
* @author Oliver Gierke
5051
*/
5152
@RunWith(MockitoJUnitRunner.class)
5253
public class BytecodeGeneratingEntityInstantiatorUnitTests<P extends PersistentProperty<P>> {
@@ -88,7 +89,7 @@ public void instantiatesTypeWithPreferredConstructorUsingParameterValueProvider(
8889
}
8990

9091
/**
91-
* @see DATACMNS-300
92+
* @see DATACMNS-300, DATACMNS-578
9293
*/
9394
@Test(expected = MappingInstantiationException.class)
9495
@SuppressWarnings({ "unchecked", "rawtypes" })
@@ -101,7 +102,7 @@ public void throwsExceptionOnBeanInstantiationException() {
101102
}
102103

103104
/**
104-
* @see DATACMNS-134
105+
* @see DATACMNS-134, DATACMNS-578
105106
*/
106107
@Test
107108
public void createsInnerClassInstanceCorrectly() {
@@ -129,7 +130,7 @@ public void doWith(Field field) throws IllegalArgumentException, IllegalAccessEx
129130
}
130131

131132
/**
132-
* @see DATACMNS-283
133+
* @see DATACMNS-283, DATACMNS-578
133134
*/
134135
@Test
135136
@SuppressWarnings({ "unchecked", "rawtypes" })
@@ -237,15 +238,15 @@ public void instantiateObjCtor2ParamStringString() {
237238

238239
for (int i = 0; i < 2; i++) {
239240
when(provider.getParameterValue(Mockito.any(Parameter.class))).thenReturn("FOO").thenReturn("BAR");
240-
241+
241242
Object instance = INSTANCE.createInstance(entity, provider);
242243
assertTrue(instance instanceof ObjCtor2ParamStringString);
243244
assertTrue(((ObjCtor2ParamStringString) instance).ctorInvoked);
244245
assertThat(((ObjCtor2ParamStringString) instance).param1, is("FOO"));
245246
assertThat(((ObjCtor2ParamStringString) instance).param2, is("BAR"));
246247
}
247248
}
248-
249+
249250
/**
250251
* @see DATACMNS-578
251252
*/
@@ -260,14 +261,15 @@ public void instantiateObjectCtor1ParamInt() {
260261
when(entity.getPersistenceConstructor()).thenReturn(constructor);
261262

262263
for (int i = 0; i < 2; i++) {
264+
263265
when(provider.getParameterValue(Mockito.any(Parameter.class))).thenReturn(42);
264-
266+
265267
Object instance = INSTANCE.createInstance(entity, provider);
266268
assertTrue(instance instanceof ObjectCtor1ParamInt);
267269
assertTrue("matches", ((ObjectCtor1ParamInt) instance).param1 == 42);
268270
}
269271
}
270-
272+
271273
/**
272274
* @see DATACMNS-578
273275
*/
@@ -282,8 +284,9 @@ public void instantiateObjectCtor7ParamsString5IntsString() {
282284
when(entity.getPersistenceConstructor()).thenReturn(constructor);
283285

284286
for (int i = 0; i < 2; i++) {
285-
when(provider.getParameterValue(Mockito.any(Parameter.class))).thenReturn("A").thenReturn(1).thenReturn(2).thenReturn(3).thenReturn(4).thenReturn(5).thenReturn("B");
286-
287+
when(provider.getParameterValue(Mockito.any(Parameter.class))).thenReturn("A").thenReturn(1).thenReturn(2)
288+
.thenReturn(3).thenReturn(4).thenReturn(5).thenReturn("B");
289+
287290
Object instance = INSTANCE.createInstance(entity, provider);
288291
assertTrue(instance instanceof ObjectCtor7ParamsString5IntsString);
289292
assertThat(((ObjectCtor7ParamsString5IntsString) instance).param1, is("A"));
@@ -295,7 +298,7 @@ public void instantiateObjectCtor7ParamsString5IntsString() {
295298
assertThat(((ObjectCtor7ParamsString5IntsString) instance).param7, is("B"));
296299
}
297300
}
298-
301+
299302
static class Foo {
300303

301304
Foo(String foo) {
@@ -324,13 +327,11 @@ public Sample(Long id, String name) {
324327

325328
/**
326329
* @author Thomas Darimont
327-
*
328330
*/
329331
public static class ObjCtorDefault {}
330332

331333
/**
332334
* @author Thomas Darimont
333-
*
334335
*/
335336
public static class ObjCtorNoArgs {
336337

@@ -343,7 +344,6 @@ public ObjCtorNoArgs() {
343344

344345
/**
345346
* @author Thomas Darimont
346-
*
347347
*/
348348
public static class ObjCtor1ParamString {
349349

@@ -358,7 +358,6 @@ public ObjCtor1ParamString(String param1) {
358358

359359
/**
360360
* @author Thomas Darimont
361-
*
362361
*/
363362
public static class ObjCtor2ParamStringString {
364363

@@ -372,25 +371,23 @@ public ObjCtor2ParamStringString(String param1, String param2) {
372371
this.param2 = param2;
373372
}
374373
}
375-
374+
376375
/**
377376
* @author Thomas Darimont
378-
*
379377
*/
380-
public static class ObjectCtor1ParamInt{
378+
public static class ObjectCtor1ParamInt {
381379

382380
public int param1;
383381

384382
public ObjectCtor1ParamInt(int param1) {
385383
this.param1 = param1;
386384
}
387385
}
388-
386+
389387
/**
390388
* @author Thomas Darimont
391-
*
392389
*/
393-
public static class ObjectCtor7ParamsString5IntsString{
390+
public static class ObjectCtor7ParamsString5IntsString {
394391

395392
public String param1;
396393
public int param2;

0 commit comments

Comments
 (0)