@@ -156,91 +156,96 @@ private void registerRuntimeHintsIfNecessary(RegisteredBean registeredBean, Exec
156156 }
157157
158158 private CodeBlock generateCodeForConstructor (RegisteredBean registeredBean , Constructor <?> constructor ) {
159- String beanName = registeredBean . getBeanName ();
160- Class <?> beanClass = registeredBean .getBeanClass ();
159+ ConstructorDescriptor descriptor = new ConstructorDescriptor (
160+ registeredBean . getBeanName (), constructor , registeredBean .getBeanClass () );
161161
162- if (KotlinDetector .isKotlinReflectPresent () && KotlinDelegate .hasConstructorWithOptionalParameter (beanClass )) {
163- return generateCodeForInaccessibleConstructor (beanName , constructor ,
164- hints -> hints .registerType (beanClass , MemberCategory .INVOKE_DECLARED_CONSTRUCTORS ));
162+ Class <?> publicType = descriptor .publicType ();
163+ if (KotlinDetector .isKotlinReflectPresent () && KotlinDelegate .hasConstructorWithOptionalParameter (publicType )) {
164+ return generateCodeForInaccessibleConstructor (descriptor ,
165+ hints -> hints .registerType (publicType , MemberCategory .INVOKE_DECLARED_CONSTRUCTORS ));
165166 }
166167
167168 if (!isVisible (constructor , constructor .getDeclaringClass ())) {
168- return generateCodeForInaccessibleConstructor (beanName , constructor ,
169+ return generateCodeForInaccessibleConstructor (descriptor ,
169170 hints -> hints .registerConstructor (constructor , ExecutableMode .INVOKE ));
170171 }
171- return generateCodeForAccessibleConstructor (beanName , constructor );
172+ return generateCodeForAccessibleConstructor (descriptor );
172173 }
173174
174- private CodeBlock generateCodeForAccessibleConstructor (String beanName , Constructor <?> constructor ) {
175+ private CodeBlock generateCodeForAccessibleConstructor (ConstructorDescriptor descriptor ) {
176+ Constructor <?> constructor = descriptor .constructor ();
175177 this .generationContext .getRuntimeHints ().reflection ().registerConstructor (
176178 constructor , ExecutableMode .INTROSPECT );
177179
178180 if (constructor .getParameterCount () == 0 ) {
179181 if (!this .allowDirectSupplierShortcut ) {
180- return CodeBlock .of ("$T.using($T::new)" , InstanceSupplier .class , constructor . getDeclaringClass ());
182+ return CodeBlock .of ("$T.using($T::new)" , InstanceSupplier .class , descriptor . actualType ());
181183 }
182184 if (!isThrowingCheckedException (constructor )) {
183- return CodeBlock .of ("$T::new" , constructor . getDeclaringClass ());
185+ return CodeBlock .of ("$T::new" , descriptor . actualType ());
184186 }
185- return CodeBlock .of ("$T.of($T::new)" , ThrowingSupplier .class , constructor . getDeclaringClass ());
187+ return CodeBlock .of ("$T.of($T::new)" , ThrowingSupplier .class , descriptor . actualType ());
186188 }
187189
188190 GeneratedMethod generatedMethod = generateGetInstanceSupplierMethod (method ->
189- buildGetInstanceMethodForConstructor (method , beanName , constructor , PRIVATE_STATIC ));
191+ buildGetInstanceMethodForConstructor (method , descriptor , PRIVATE_STATIC ));
190192 return generateReturnStatement (generatedMethod );
191193 }
192194
193- private CodeBlock generateCodeForInaccessibleConstructor (String beanName ,
194- Constructor <?> constructor , Consumer <ReflectionHints > hints ) {
195+ private CodeBlock generateCodeForInaccessibleConstructor (ConstructorDescriptor descriptor ,
196+ Consumer <ReflectionHints > hints ) {
195197
198+ Constructor <?> constructor = descriptor .constructor ();
196199 CodeWarnings codeWarnings = new CodeWarnings ();
197200 codeWarnings .detectDeprecation (constructor .getDeclaringClass (), constructor )
198201 .detectDeprecation (Arrays .stream (constructor .getParameters ()).map (Parameter ::getType ));
199202 hints .accept (this .generationContext .getRuntimeHints ().reflection ());
200203
201204 GeneratedMethod generatedMethod = generateGetInstanceSupplierMethod (method -> {
202- method .addJavadoc ("Get the bean instance supplier for '$L'." , beanName );
205+ method .addJavadoc ("Get the bean instance supplier for '$L'." , descriptor . beanName () );
203206 method .addModifiers (PRIVATE_STATIC );
204207 codeWarnings .suppress (method );
205- method .returns (ParameterizedTypeName .get (BeanInstanceSupplier .class , constructor . getDeclaringClass ()));
206- method .addStatement (generateResolverForConstructor (constructor ));
208+ method .returns (ParameterizedTypeName .get (BeanInstanceSupplier .class , descriptor . publicType ()));
209+ method .addStatement (generateResolverForConstructor (descriptor ));
207210 });
208211
209212 return generateReturnStatement (generatedMethod );
210213 }
211214
212- private void buildGetInstanceMethodForConstructor (MethodSpec .Builder method , String beanName ,
213- Constructor <?> constructor , javax .lang .model .element .Modifier ... modifiers ) {
215+ private void buildGetInstanceMethodForConstructor (MethodSpec .Builder method , ConstructorDescriptor descriptor ,
216+ javax .lang .model .element .Modifier ... modifiers ) {
214217
215- Class <?> declaringClass = constructor .getDeclaringClass ();
218+ Constructor <?> constructor = descriptor .constructor ();
219+ Class <?> publicType = descriptor .publicType ();
220+ Class <?> actualType = descriptor .actualType ();
216221
217222 CodeWarnings codeWarnings = new CodeWarnings ();
218- codeWarnings .detectDeprecation (declaringClass , constructor )
223+ codeWarnings .detectDeprecation (actualType , constructor )
219224 .detectDeprecation (Arrays .stream (constructor .getParameters ()).map (Parameter ::getType ));
220- method .addJavadoc ("Get the bean instance supplier for '$L'." , beanName );
225+ method .addJavadoc ("Get the bean instance supplier for '$L'." , descriptor . beanName () );
221226 method .addModifiers (modifiers );
222227 codeWarnings .suppress (method );
223- method .returns (ParameterizedTypeName .get (BeanInstanceSupplier .class , declaringClass ));
228+ method .returns (ParameterizedTypeName .get (BeanInstanceSupplier .class , publicType ));
224229
225230 CodeBlock .Builder code = CodeBlock .builder ();
226- code .add (generateResolverForConstructor (constructor ));
231+ code .add (generateResolverForConstructor (descriptor ));
227232 boolean hasArguments = constructor .getParameterCount () > 0 ;
228- boolean onInnerClass = ClassUtils .isInnerClass (declaringClass );
233+ boolean onInnerClass = ClassUtils .isInnerClass (actualType );
229234
230235 CodeBlock arguments = hasArguments ?
231- new AutowiredArgumentsCodeGenerator (declaringClass , constructor )
236+ new AutowiredArgumentsCodeGenerator (actualType , constructor )
232237 .generateCode (constructor .getParameterTypes (), (onInnerClass ? 1 : 0 ))
233238 : NO_ARGS ;
234239
235- CodeBlock newInstance = generateNewInstanceCodeForConstructor (declaringClass , arguments );
240+ CodeBlock newInstance = generateNewInstanceCodeForConstructor (actualType , arguments );
236241 code .add (generateWithGeneratorCode (hasArguments , newInstance ));
237242 method .addStatement (code .build ());
238243 }
239244
240- private CodeBlock generateResolverForConstructor (Constructor <?> constructor ) {
241- CodeBlock parameterTypes = generateParameterTypesCode (constructor .getParameterTypes ());
245+ private CodeBlock generateResolverForConstructor (ConstructorDescriptor descriptor ) {
246+ CodeBlock parameterTypes = generateParameterTypesCode (descriptor . constructor () .getParameterTypes ());
242247 return CodeBlock .of ("return $T.<$T>forConstructor($L)" , BeanInstanceSupplier .class ,
243- constructor . getDeclaringClass (), parameterTypes );
248+ descriptor . publicType (), parameterTypes );
244249 }
245250
246251 private CodeBlock generateNewInstanceCodeForConstructor (Class <?> declaringClass , CodeBlock args ) {
@@ -438,4 +443,11 @@ private void registerProxyIfNecessary(RuntimeHints runtimeHints, DependencyDescr
438443 }
439444 }
440445
446+ record ConstructorDescriptor (String beanName , Constructor <?> constructor , Class <?> publicType ) {
447+
448+ Class <?> actualType () {
449+ return this .constructor .getDeclaringClass ();
450+ }
451+ }
452+
441453}
0 commit comments