27
27
import java .util .stream .Collectors ;
28
28
29
29
import org .springframework .aot .hint .ExecutableMode ;
30
+ import org .springframework .beans .BeanInstantiationException ;
30
31
import org .springframework .beans .BeansException ;
31
32
import org .springframework .beans .TypeConverter ;
32
- import org .springframework .beans .factory .BeanCreationException ;
33
33
import org .springframework .beans .factory .BeanFactory ;
34
34
import org .springframework .beans .factory .InjectionPoint ;
35
35
import org .springframework .beans .factory .NoSuchBeanDefinitionException ;
@@ -97,21 +97,21 @@ public final class BeanInstanceSupplier<T> extends AutowiredElementResolver impl
97
97
private BeanInstanceSupplier (ExecutableLookup lookup ,
98
98
@ Nullable ThrowingBiFunction <RegisteredBean , AutowiredArguments , T > generator ,
99
99
@ Nullable String [] shortcuts ) {
100
+
100
101
this .lookup = lookup ;
101
102
this .generator = generator ;
102
103
this .shortcuts = shortcuts ;
103
104
}
104
105
106
+
105
107
/**
106
108
* Create a {@link BeanInstanceSupplier} that resolves
107
109
* arguments for the specified bean constructor.
108
110
* @param <T> the type of instance supplied
109
111
* @param parameterTypes the constructor parameter types
110
112
* @return a new {@link BeanInstanceSupplier} instance
111
113
*/
112
- public static <T > BeanInstanceSupplier <T > forConstructor (
113
- Class <?>... parameterTypes ) {
114
-
114
+ public static <T > BeanInstanceSupplier <T > forConstructor (Class <?>... parameterTypes ) {
115
115
Assert .notNull (parameterTypes , "'parameterTypes' must not be null" );
116
116
Assert .noNullElements (parameterTypes , "'parameterTypes' must not contain null elements" );
117
117
return new BeanInstanceSupplier <>(new ConstructorLookup (parameterTypes ), null , null );
@@ -149,11 +149,11 @@ ExecutableLookup getLookup() {
149
149
* @param generator a {@link ThrowingBiFunction} that uses the
150
150
* {@link RegisteredBean} and resolved {@link AutowiredArguments} to
151
151
* instantiate the underlying bean
152
- * @return a new {@link BeanInstanceSupplier} instance with the specified
153
- * generator
152
+ * @return a new {@link BeanInstanceSupplier} instance with the specified generator
154
153
*/
155
154
public BeanInstanceSupplier <T > withGenerator (
156
155
ThrowingBiFunction <RegisteredBean , AutowiredArguments , T > generator ) {
156
+
157
157
Assert .notNull (generator , "'generator' must not be null" );
158
158
return new BeanInstanceSupplier <>(this .lookup , generator , this .shortcuts );
159
159
}
@@ -163,11 +163,9 @@ public BeanInstanceSupplier<T> withGenerator(
163
163
* {@code generator} function to instantiate the underlying bean.
164
164
* @param generator a {@link ThrowingFunction} that uses the
165
165
* {@link RegisteredBean} to instantiate the underlying bean
166
- * @return a new {@link BeanInstanceSupplier} instance with the specified
167
- * generator
166
+ * @return a new {@link BeanInstanceSupplier} instance with the specified generator
168
167
*/
169
- public BeanInstanceSupplier <T > withGenerator (
170
- ThrowingFunction <RegisteredBean , T > generator ) {
168
+ public BeanInstanceSupplier <T > withGenerator (ThrowingFunction <RegisteredBean , T > generator ) {
171
169
Assert .notNull (generator , "'generator' must not be null" );
172
170
return new BeanInstanceSupplier <>(this .lookup ,
173
171
(registeredBean , args ) -> generator .apply (registeredBean ), this .shortcuts );
@@ -176,10 +174,8 @@ public BeanInstanceSupplier<T> withGenerator(
176
174
/**
177
175
* Return a new {@link BeanInstanceSupplier} instance that uses the specified
178
176
* {@code generator} supplier to instantiate the underlying bean.
179
- * @param generator a {@link ThrowingSupplier} to instantiate the underlying
180
- * bean
181
- * @return a new {@link BeanInstanceSupplier} instance with the specified
182
- * generator
177
+ * @param generator a {@link ThrowingSupplier} to instantiate the underlying bean
178
+ * @return a new {@link BeanInstanceSupplier} instance with the specified generator
183
179
*/
184
180
public BeanInstanceSupplier <T > withGenerator (ThrowingSupplier <T > generator ) {
185
181
Assert .notNull (generator , "'generator' must not be null" );
@@ -282,8 +278,7 @@ private MethodParameter getMethodParameter(Executable executable, int index) {
282
278
if (executable instanceof Method method ) {
283
279
return new MethodParameter (method , index );
284
280
}
285
- throw new IllegalStateException (
286
- "Unsupported executable " + executable .getClass ().getName ());
281
+ throw new IllegalStateException ("Unsupported executable: " + executable .getClass ().getName ());
287
282
}
288
283
289
284
private ConstructorArgumentValues resolveArgumentValues (
@@ -303,9 +298,7 @@ private ConstructorArgumentValues resolveArgumentValues(
303
298
return resolved ;
304
299
}
305
300
306
- private ValueHolder resolveArgumentValue (BeanDefinitionValueResolver resolver ,
307
- ValueHolder valueHolder ) {
308
-
301
+ private ValueHolder resolveArgumentValue (BeanDefinitionValueResolver resolver , ValueHolder valueHolder ) {
309
302
if (valueHolder .isConverted ()) {
310
303
return valueHolder ;
311
304
}
@@ -331,8 +324,7 @@ private Object resolveArgument(AbstractAutowireCapableBeanFactory beanFactory,
331
324
}
332
325
try {
333
326
try {
334
- return beanFactory .resolveDependency (dependencyDescriptor , beanName ,
335
- autowiredBeans , typeConverter );
327
+ return beanFactory .resolveDependency (dependencyDescriptor , beanName , autowiredBeans , typeConverter );
336
328
}
337
329
catch (NoSuchBeanDefinitionException ex ) {
338
330
if (parameterType .isArray ()) {
@@ -348,47 +340,45 @@ private Object resolveArgument(AbstractAutowireCapableBeanFactory beanFactory,
348
340
}
349
341
}
350
342
catch (BeansException ex ) {
351
- throw new UnsatisfiedDependencyException (null , beanName ,
352
- new InjectionPoint (parameter ), ex );
343
+ throw new UnsatisfiedDependencyException (null , beanName , new InjectionPoint (parameter ), ex );
353
344
}
354
345
}
355
346
356
347
@ SuppressWarnings ("unchecked" )
357
- private T instantiate (ConfigurableBeanFactory beanFactory , Executable executable ,
358
- Object [] arguments ) {
359
-
360
- try {
361
- if (executable instanceof Constructor <?> constructor ) {
362
- return (T ) instantiate (constructor , arguments );
348
+ private T instantiate (ConfigurableBeanFactory beanFactory , Executable executable , Object [] args ) {
349
+ if (executable instanceof Constructor <?> constructor ) {
350
+ try {
351
+ return (T ) instantiate (constructor , args );
363
352
}
364
- if ( executable instanceof Method method ) {
365
- return ( T ) instantiate ( beanFactory , method , arguments );
353
+ catch ( Exception ex ) {
354
+ throw new BeanInstantiationException ( constructor , ex . getMessage (), ex );
366
355
}
367
356
}
368
- catch (Exception ex ) {
369
- throw new BeanCreationException (
370
- "Unable to instantiate bean using " + executable , ex );
357
+ if (executable instanceof Method method ) {
358
+ try {
359
+ return (T ) instantiate (beanFactory , method , args );
360
+ }
361
+ catch (Exception ex ) {
362
+ throw new BeanInstantiationException (method , ex .getMessage (), ex );
363
+ }
371
364
}
372
- throw new IllegalStateException (
373
- "Unsupported executable " + executable .getClass ().getName ());
365
+ throw new IllegalStateException ("Unsupported executable " + executable .getClass ().getName ());
374
366
}
375
367
376
- private Object instantiate (Constructor <?> constructor , Object [] arguments ) throws Exception {
368
+ private Object instantiate (Constructor <?> constructor , Object [] args ) throws Exception {
377
369
Class <?> declaringClass = constructor .getDeclaringClass ();
378
370
if (ClassUtils .isInnerClass (declaringClass )) {
379
371
Object enclosingInstance = createInstance (declaringClass .getEnclosingClass ());
380
- arguments = ObjectUtils .addObjectToArray (arguments , enclosingInstance , 0 );
372
+ args = ObjectUtils .addObjectToArray (args , enclosingInstance , 0 );
381
373
}
382
374
ReflectionUtils .makeAccessible (constructor );
383
- return constructor .newInstance (arguments );
375
+ return constructor .newInstance (args );
384
376
}
385
377
386
- private Object instantiate (ConfigurableBeanFactory beanFactory , Method method ,
387
- Object [] arguments ) {
388
-
389
- ReflectionUtils .makeAccessible (method );
378
+ private Object instantiate (ConfigurableBeanFactory beanFactory , Method method , Object [] args ) throws Exception {
390
379
Object target = getFactoryMethodTarget (beanFactory , method );
391
- return ReflectionUtils .invokeMethod (method , target , arguments );
380
+ ReflectionUtils .makeAccessible (method );
381
+ return method .invoke (target , args );
392
382
}
393
383
394
384
@ Nullable
@@ -416,13 +406,13 @@ private static String toCommaSeparatedNames(Class<?>... parameterTypes) {
416
406
return Arrays .stream (parameterTypes ).map (Class ::getName ).collect (Collectors .joining (", " ));
417
407
}
418
408
409
+
419
410
/**
420
411
* Performs lookup of the {@link Executable}.
421
412
*/
422
413
static abstract class ExecutableLookup {
423
414
424
415
abstract Executable get (RegisteredBean registeredBean );
425
-
426
416
}
427
417
428
418
@@ -433,12 +423,10 @@ private static class ConstructorLookup extends ExecutableLookup {
433
423
434
424
private final Class <?>[] parameterTypes ;
435
425
436
-
437
426
ConstructorLookup (Class <?>[] parameterTypes ) {
438
427
this .parameterTypes = parameterTypes ;
439
428
}
440
429
441
-
442
430
@ Override
443
431
public Executable get (RegisteredBean registeredBean ) {
444
432
Class <?> beanClass = registeredBean .getBeanClass ();
@@ -456,10 +444,8 @@ public Executable get(RegisteredBean registeredBean) {
456
444
457
445
@ Override
458
446
public String toString () {
459
- return "Constructor with parameter types [%s]" .formatted (
460
- toCommaSeparatedNames (this .parameterTypes ));
447
+ return "Constructor with parameter types [%s]" .formatted (toCommaSeparatedNames (this .parameterTypes ));
461
448
}
462
-
463
449
}
464
450
465
451
@@ -474,23 +460,19 @@ private static class FactoryMethodLookup extends ExecutableLookup {
474
460
475
461
private final Class <?>[] parameterTypes ;
476
462
477
-
478
- FactoryMethodLookup (Class <?> declaringClass , String methodName ,
479
- Class <?>[] parameterTypes ) {
463
+ FactoryMethodLookup (Class <?> declaringClass , String methodName , Class <?>[] parameterTypes ) {
480
464
this .declaringClass = declaringClass ;
481
465
this .methodName = methodName ;
482
466
this .parameterTypes = parameterTypes ;
483
467
}
484
468
485
-
486
469
@ Override
487
470
public Executable get (RegisteredBean registeredBean ) {
488
471
return get ();
489
472
}
490
473
491
474
Method get () {
492
- Method method = ReflectionUtils .findMethod (this .declaringClass ,
493
- this .methodName , this .parameterTypes );
475
+ Method method = ReflectionUtils .findMethod (this .declaringClass , this .methodName , this .parameterTypes );
494
476
Assert .notNull (method , () -> "%s cannot be found" .formatted (this ));
495
477
return method ;
496
478
}
@@ -501,7 +483,6 @@ public String toString() {
501
483
this .methodName , toCommaSeparatedNames (this .parameterTypes ),
502
484
this .declaringClass );
503
485
}
504
-
505
486
}
506
487
507
488
}
0 commit comments