Use target of the FactoryBean only if the FactoryBean is public

This commit polishes 85d4a79 so that the target type of factory bean
is only considered if the FactoryBean is accessible. If the FactoryBean
requires protected access, we still generate the code in the package of
the FactoryBean.

Those two commits combined are actually providing a fix for the use
case described in gh-28809.

Closes gh-28809
This commit is contained in:
Stephane Nicoll
2022-07-19 21:00:55 +02:00
parent 978cdfff46
commit d6345db7c9
4 changed files with 76 additions and 9 deletions

View File

@@ -21,6 +21,7 @@ import java.lang.reflect.Executable;
import java.util.List;
import java.util.function.Predicate;
import org.springframework.aot.generate.AccessVisibility;
import org.springframework.aot.generate.GenerationContext;
import org.springframework.aot.generate.MethodReference;
import org.springframework.beans.factory.FactoryBean;
@@ -80,7 +81,9 @@ class DefaultBeanRegistrationCodeFragments extends BeanRegistrationCodeFragments
private Class<?> extractDeclaringClass(Executable executable) {
Class<?> declaringClass = ClassUtils.getUserClass(executable.getDeclaringClass());
if (executable instanceof Constructor<?> && FactoryBean.class.isAssignableFrom(declaringClass)) {
if (executable instanceof Constructor<?>
&& AccessVisibility.forMember(executable) == AccessVisibility.PUBLIC
&& FactoryBean.class.isAssignableFrom(declaringClass)) {
return ResolvableType.forType(declaringClass).as(FactoryBean.class).getGeneric(0).toClass();
}
return executable.getDeclaringClass();