51
51
* {@link GenericArrayType#getGenericComponentType()}) will be automatically wrapped.
52
52
*
53
53
* @author Phillip Webb
54
+ * @author Juergen Hoeller
54
55
* @since 4.0
55
56
*/
56
57
abstract class SerializableTypeWrapper {
@@ -210,7 +211,7 @@ public Object getSource() {
210
211
211
212
212
213
/**
213
- * {@link Serializable} {@link InvocationHandler} used by the Proxied {@link Type}.
214
+ * {@link Serializable} {@link InvocationHandler} used by the proxied {@link Type}.
214
215
* Provides serialization support and enhances any methods that return {@code Type}
215
216
* or {@code Type[]}.
216
217
*/
@@ -373,21 +374,27 @@ static class MethodInvokeTypeProvider implements TypeProvider {
373
374
374
375
private final int index ;
375
376
376
- private transient Object result ;
377
+ private transient Method method ;
378
+
379
+ private transient volatile Object result ;
377
380
378
381
public MethodInvokeTypeProvider (TypeProvider provider , Method method , int index ) {
379
382
this .provider = provider ;
380
383
this .methodName = method .getName ();
381
384
this .index = index ;
382
- this .result = ReflectionUtils . invokeMethod ( method , provider . getType ()) ;
385
+ this .method = method ;
383
386
}
384
387
385
388
@ Override
386
389
public Type getType () {
387
- if (this .result instanceof Type || this .result == null ) {
388
- return (Type ) this .result ;
390
+ Object result = this .result ;
391
+ if (result == null ) {
392
+ // Lazy invocation of the target method
393
+ result = ReflectionUtils .invokeMethod (this .method , this .provider .getType ());
394
+ // Cache the result for further calls
395
+ this .result = result ;
389
396
}
390
- return (( Type [])this . result )[this .index ];
397
+ return (result instanceof Type [] ? (( Type []) result )[this .index ] : ( Type ) result ) ;
391
398
}
392
399
393
400
@ Override
@@ -397,8 +404,8 @@ public Object getSource() {
397
404
398
405
private void readObject (ObjectInputStream inputStream ) throws IOException , ClassNotFoundException {
399
406
inputStream .defaultReadObject ();
400
- Method method = ReflectionUtils .findMethod (this .provider .getType ().getClass (), this .methodName );
401
- this .result = ReflectionUtils . invokeMethod ( method , this .provider . getType () );
407
+ this . method = ReflectionUtils .findMethod (this .provider .getType ().getClass (), this .methodName );
408
+ Assert . state ( this .method . getReturnType () == Type . class || this .method . getReturnType () == Type []. class );
402
409
}
403
410
}
404
411
0 commit comments