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:
parent
978cdfff46
commit
d6345db7c9
|
|
@ -21,6 +21,7 @@ import java.lang.reflect.Executable;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
|
import org.springframework.aot.generate.AccessVisibility;
|
||||||
import org.springframework.aot.generate.GenerationContext;
|
import org.springframework.aot.generate.GenerationContext;
|
||||||
import org.springframework.aot.generate.MethodReference;
|
import org.springframework.aot.generate.MethodReference;
|
||||||
import org.springframework.beans.factory.FactoryBean;
|
import org.springframework.beans.factory.FactoryBean;
|
||||||
|
|
@ -80,7 +81,9 @@ class DefaultBeanRegistrationCodeFragments extends BeanRegistrationCodeFragments
|
||||||
|
|
||||||
private Class<?> extractDeclaringClass(Executable executable) {
|
private Class<?> extractDeclaringClass(Executable executable) {
|
||||||
Class<?> declaringClass = ClassUtils.getUserClass(executable.getDeclaringClass());
|
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 ResolvableType.forType(declaringClass).as(FactoryBean.class).getGeneric(0).toClass();
|
||||||
}
|
}
|
||||||
return executable.getDeclaringClass();
|
return executable.getDeclaringClass();
|
||||||
|
|
|
||||||
|
|
@ -51,12 +51,19 @@ class DefaultBeanRegistrationCodeFragmentsTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getTargetOnConstructorToFactoryBean() {
|
void getTargetOnConstructorToPublicFactoryBean() {
|
||||||
RegisteredBean registeredBean = registerTestBean(TestBean.class);
|
RegisteredBean registeredBean = registerTestBean(TestBean.class);
|
||||||
assertThat(createInstance(registeredBean).getTarget(registeredBean,
|
assertThat(createInstance(registeredBean).getTarget(registeredBean,
|
||||||
TestBeanFactoryBean.class.getDeclaredConstructors()[0])).isEqualTo(TestBean.class);
|
TestBeanFactoryBean.class.getDeclaredConstructors()[0])).isEqualTo(TestBean.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getTargetOnConstructorToProtectedFactoryBean() {
|
||||||
|
RegisteredBean registeredBean = registerTestBean(TestBean.class);
|
||||||
|
assertThat(createInstance(registeredBean).getTarget(registeredBean,
|
||||||
|
PrivilegedTestBeanFactoryBean.class.getDeclaredConstructors()[0])).isEqualTo(PrivilegedTestBeanFactoryBean.class);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getTargetOnMethod() {
|
void getTargetOnMethod() {
|
||||||
RegisteredBean registeredBean = registerTestBean(TestBean.class);
|
RegisteredBean registeredBean = registerTestBean(TestBean.class);
|
||||||
|
|
@ -135,13 +142,7 @@ class DefaultBeanRegistrationCodeFragmentsTests {
|
||||||
return "Test";
|
return "Test";
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
static class PrivilegedTestBeanFactoryBean implements FactoryBean<TestBean> {
|
||||||
static class TestBean {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static class TestBeanFactoryBean implements FactoryBean<TestBean> {
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TestBean getObject() throws Exception {
|
public TestBean getObject() throws Exception {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,25 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2022 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.beans.factory.aot;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An empty test bean used by code generation.
|
||||||
|
*
|
||||||
|
* @author Stephane Nicoll
|
||||||
|
*/
|
||||||
|
public class TestBean {
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,38 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2022 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.beans.factory.aot;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.FactoryBean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A public {@link FactoryBean}.
|
||||||
|
*
|
||||||
|
* @author Stephane Nicoll
|
||||||
|
*/
|
||||||
|
public class TestBeanFactoryBean implements FactoryBean<TestBean> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TestBean getObject() throws Exception {
|
||||||
|
return new TestBean();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<?> getObjectType() {
|
||||||
|
return TestBean.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue