1
1
/*
2
- * Copyright 2002-2023 the original author or authors.
2
+ * Copyright 2002-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
46
46
import org .springframework .beans .factory .support .DefaultListableBeanFactory ;
47
47
import org .springframework .beans .factory .support .InstanceSupplier ;
48
48
import org .springframework .beans .factory .support .RegisteredBean ;
49
+ import org .springframework .beans .factory .support .RegisteredBean .InstantiationDescriptor ;
49
50
import org .springframework .core .KotlinDetector ;
50
51
import org .springframework .core .MethodParameter ;
51
52
import org .springframework .core .ResolvableType ;
@@ -121,13 +122,26 @@ public InstanceSupplierCodeGenerator(GenerationContext generationContext,
121
122
* @param constructorOrFactoryMethod the executable to use to create the bean
122
123
* @return the generated code
123
124
*/
125
+ @ Deprecated (since = "6.1.7" )
124
126
public CodeBlock generateCode (RegisteredBean registeredBean , Executable constructorOrFactoryMethod ) {
127
+ return generateCode (registeredBean , new InstantiationDescriptor (
128
+ constructorOrFactoryMethod , constructorOrFactoryMethod .getDeclaringClass ()));
129
+ }
130
+
131
+ /**
132
+ * Generate the instance supplier code.
133
+ * @param registeredBean the bean to handle
134
+ * @param instantiationDescriptor the executable to use to create the bean
135
+ * @return the generated code
136
+ */
137
+ public CodeBlock generateCode (RegisteredBean registeredBean , InstantiationDescriptor instantiationDescriptor ) {
138
+ Executable constructorOrFactoryMethod = instantiationDescriptor .executable ();
125
139
registerRuntimeHintsIfNecessary (registeredBean , constructorOrFactoryMethod );
126
140
if (constructorOrFactoryMethod instanceof Constructor <?> constructor ) {
127
141
return generateCodeForConstructor (registeredBean , constructor );
128
142
}
129
143
if (constructorOrFactoryMethod instanceof Method method ) {
130
- return generateCodeForFactoryMethod (registeredBean , method );
144
+ return generateCodeForFactoryMethod (registeredBean , method , instantiationDescriptor . target () );
131
145
}
132
146
throw new IllegalStateException (
133
147
"No suitable executor found for " + registeredBean .getBeanName ());
@@ -253,21 +267,21 @@ private CodeBlock generateNewInstanceCodeForConstructor(boolean dependsOnBean,
253
267
declaringClass .getSimpleName (), args );
254
268
}
255
269
256
- private CodeBlock generateCodeForFactoryMethod (RegisteredBean registeredBean , Method factoryMethod ) {
270
+ private CodeBlock generateCodeForFactoryMethod (RegisteredBean registeredBean , Method factoryMethod , Class <?> target ) {
257
271
String beanName = registeredBean .getBeanName ();
258
- Class <?> declaringClass = ClassUtils .getUserClass (factoryMethod . getDeclaringClass () );
272
+ Class <?> targetToUse = ClassUtils .getUserClass (target );
259
273
boolean dependsOnBean = !Modifier .isStatic (factoryMethod .getModifiers ());
260
274
261
275
Visibility accessVisibility = getAccessVisibility (registeredBean , factoryMethod );
262
276
if (accessVisibility != Visibility .PRIVATE ) {
263
277
return generateCodeForAccessibleFactoryMethod (
264
- beanName , factoryMethod , declaringClass , dependsOnBean );
278
+ beanName , factoryMethod , targetToUse , dependsOnBean );
265
279
}
266
- return generateCodeForInaccessibleFactoryMethod (beanName , factoryMethod , declaringClass );
280
+ return generateCodeForInaccessibleFactoryMethod (beanName , factoryMethod , targetToUse );
267
281
}
268
282
269
283
private CodeBlock generateCodeForAccessibleFactoryMethod (String beanName ,
270
- Method factoryMethod , Class <?> declaringClass , boolean dependsOnBean ) {
284
+ Method factoryMethod , Class <?> target , boolean dependsOnBean ) {
271
285
272
286
this .generationContext .getRuntimeHints ().reflection ().registerMethod (
273
287
factoryMethod , ExecutableMode .INTROSPECT );
@@ -276,20 +290,20 @@ private CodeBlock generateCodeForAccessibleFactoryMethod(String beanName,
276
290
Class <?> suppliedType = ClassUtils .resolvePrimitiveIfNecessary (factoryMethod .getReturnType ());
277
291
CodeBlock .Builder code = CodeBlock .builder ();
278
292
code .add ("$T.<$T>forFactoryMethod($T.class, $S)" , BeanInstanceSupplier .class ,
279
- suppliedType , declaringClass , factoryMethod .getName ());
293
+ suppliedType , target , factoryMethod .getName ());
280
294
code .add (".withGenerator(($L) -> $T.$L())" , REGISTERED_BEAN_PARAMETER_NAME ,
281
- declaringClass , factoryMethod .getName ());
295
+ target , factoryMethod .getName ());
282
296
return code .build ();
283
297
}
284
298
285
299
GeneratedMethod getInstanceMethod = generateGetInstanceSupplierMethod (method ->
286
300
buildGetInstanceMethodForFactoryMethod (method , beanName , factoryMethod ,
287
- declaringClass , dependsOnBean , PRIVATE_STATIC ));
301
+ target , dependsOnBean , PRIVATE_STATIC ));
288
302
return generateReturnStatement (getInstanceMethod );
289
303
}
290
304
291
305
private CodeBlock generateCodeForInaccessibleFactoryMethod (
292
- String beanName , Method factoryMethod , Class <?> declaringClass ) {
306
+ String beanName , Method factoryMethod , Class <?> target ) {
293
307
294
308
this .generationContext .getRuntimeHints ().reflection ().registerMethod (factoryMethod , ExecutableMode .INVOKE );
295
309
GeneratedMethod getInstanceMethod = generateGetInstanceSupplierMethod (method -> {
@@ -298,19 +312,19 @@ private CodeBlock generateCodeForInaccessibleFactoryMethod(
298
312
method .addModifiers (PRIVATE_STATIC );
299
313
method .returns (ParameterizedTypeName .get (BeanInstanceSupplier .class , suppliedType ));
300
314
method .addStatement (generateInstanceSupplierForFactoryMethod (
301
- factoryMethod , suppliedType , declaringClass , factoryMethod .getName ()));
315
+ factoryMethod , suppliedType , target , factoryMethod .getName ()));
302
316
});
303
317
return generateReturnStatement (getInstanceMethod );
304
318
}
305
319
306
320
private void buildGetInstanceMethodForFactoryMethod (MethodSpec .Builder method ,
307
- String beanName , Method factoryMethod , Class <?> declaringClass ,
321
+ String beanName , Method factoryMethod , Class <?> target ,
308
322
boolean dependsOnBean , javax .lang .model .element .Modifier ... modifiers ) {
309
323
310
324
String factoryMethodName = factoryMethod .getName ();
311
325
Class <?> suppliedType = ClassUtils .resolvePrimitiveIfNecessary (factoryMethod .getReturnType ());
312
326
CodeWarnings codeWarnings = new CodeWarnings ();
313
- codeWarnings .detectDeprecation (declaringClass , factoryMethod , suppliedType )
327
+ codeWarnings .detectDeprecation (target , factoryMethod , suppliedType )
314
328
.detectDeprecation (Arrays .stream (factoryMethod .getParameters ()).map (Parameter ::getType ));
315
329
316
330
method .addJavadoc ("Get the bean instance supplier for '$L'." , beanName );
@@ -320,41 +334,41 @@ private void buildGetInstanceMethodForFactoryMethod(MethodSpec.Builder method,
320
334
321
335
CodeBlock .Builder code = CodeBlock .builder ();
322
336
code .add (generateInstanceSupplierForFactoryMethod (
323
- factoryMethod , suppliedType , declaringClass , factoryMethodName ));
337
+ factoryMethod , suppliedType , target , factoryMethodName ));
324
338
325
339
boolean hasArguments = factoryMethod .getParameterCount () > 0 ;
326
340
CodeBlock arguments = hasArguments ?
327
- new AutowiredArgumentsCodeGenerator (declaringClass , factoryMethod )
341
+ new AutowiredArgumentsCodeGenerator (target , factoryMethod )
328
342
.generateCode (factoryMethod .getParameterTypes ())
329
343
: NO_ARGS ;
330
344
331
345
CodeBlock newInstance = generateNewInstanceCodeForMethod (
332
- dependsOnBean , declaringClass , factoryMethodName , arguments );
346
+ dependsOnBean , target , factoryMethodName , arguments );
333
347
code .add (generateWithGeneratorCode (hasArguments , newInstance ));
334
348
method .addStatement (code .build ());
335
349
}
336
350
337
351
private CodeBlock generateInstanceSupplierForFactoryMethod (Method factoryMethod ,
338
- Class <?> suppliedType , Class <?> declaringClass , String factoryMethodName ) {
352
+ Class <?> suppliedType , Class <?> target , String factoryMethodName ) {
339
353
340
354
if (factoryMethod .getParameterCount () == 0 ) {
341
355
return CodeBlock .of ("return $T.<$T>forFactoryMethod($T.class, $S)" ,
342
- BeanInstanceSupplier .class , suppliedType , declaringClass , factoryMethodName );
356
+ BeanInstanceSupplier .class , suppliedType , target , factoryMethodName );
343
357
}
344
358
345
359
CodeBlock parameterTypes = generateParameterTypesCode (factoryMethod .getParameterTypes (), 0 );
346
360
return CodeBlock .of ("return $T.<$T>forFactoryMethod($T.class, $S, $L)" ,
347
- BeanInstanceSupplier .class , suppliedType , declaringClass , factoryMethodName , parameterTypes );
361
+ BeanInstanceSupplier .class , suppliedType , target , factoryMethodName , parameterTypes );
348
362
}
349
363
350
364
private CodeBlock generateNewInstanceCodeForMethod (boolean dependsOnBean ,
351
- Class <?> declaringClass , String factoryMethodName , CodeBlock args ) {
365
+ Class <?> target , String factoryMethodName , CodeBlock args ) {
352
366
353
367
if (!dependsOnBean ) {
354
- return CodeBlock .of ("$T.$L($L)" , declaringClass , factoryMethodName , args );
368
+ return CodeBlock .of ("$T.$L($L)" , target , factoryMethodName , args );
355
369
}
356
370
return CodeBlock .of ("$L.getBeanFactory().getBean($T.class).$L($L)" ,
357
- REGISTERED_BEAN_PARAMETER_NAME , declaringClass , factoryMethodName , args );
371
+ REGISTERED_BEAN_PARAMETER_NAME , target , factoryMethodName , args );
358
372
}
359
373
360
374
private CodeBlock generateReturnStatement (GeneratedMethod generatedMethod ) {
0 commit comments