Skip to content

Commit d6345db

Browse files
committed
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
1 parent 978cdff commit d6345db

File tree

4 files changed

+76
-9
lines changed

4 files changed

+76
-9
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/aot/DefaultBeanRegistrationCodeFragments.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.List;
2222
import java.util.function.Predicate;
2323

24+
import org.springframework.aot.generate.AccessVisibility;
2425
import org.springframework.aot.generate.GenerationContext;
2526
import org.springframework.aot.generate.MethodReference;
2627
import org.springframework.beans.factory.FactoryBean;
@@ -80,7 +81,9 @@ public Class<?> getTarget(RegisteredBean registeredBean,
8081

8182
private Class<?> extractDeclaringClass(Executable executable) {
8283
Class<?> declaringClass = ClassUtils.getUserClass(executable.getDeclaringClass());
83-
if (executable instanceof Constructor<?> && FactoryBean.class.isAssignableFrom(declaringClass)) {
84+
if (executable instanceof Constructor<?>
85+
&& AccessVisibility.forMember(executable) == AccessVisibility.PUBLIC
86+
&& FactoryBean.class.isAssignableFrom(declaringClass)) {
8487
return ResolvableType.forType(declaringClass).as(FactoryBean.class).getGeneric(0).toClass();
8588
}
8689
return executable.getDeclaringClass();

spring-beans/src/test/java/org/springframework/beans/factory/aot/DefaultBeanRegistrationCodeFragmentsTests.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,19 @@ void getTargetOnConstructor() {
5151
}
5252

5353
@Test
54-
void getTargetOnConstructorToFactoryBean() {
54+
void getTargetOnConstructorToPublicFactoryBean() {
5555
RegisteredBean registeredBean = registerTestBean(TestBean.class);
5656
assertThat(createInstance(registeredBean).getTarget(registeredBean,
5757
TestBeanFactoryBean.class.getDeclaredConstructors()[0])).isEqualTo(TestBean.class);
5858
}
5959

60+
@Test
61+
void getTargetOnConstructorToProtectedFactoryBean() {
62+
RegisteredBean registeredBean = registerTestBean(TestBean.class);
63+
assertThat(createInstance(registeredBean).getTarget(registeredBean,
64+
PrivilegedTestBeanFactoryBean.class.getDeclaredConstructors()[0])).isEqualTo(PrivilegedTestBeanFactoryBean.class);
65+
}
66+
6067
@Test
6168
void getTargetOnMethod() {
6269
RegisteredBean registeredBean = registerTestBean(TestBean.class);
@@ -135,13 +142,7 @@ static String createString() {
135142
return "Test";
136143
}
137144

138-
@SuppressWarnings("unused")
139-
static class TestBean {
140-
141-
}
142-
143-
144-
static class TestBeanFactoryBean implements FactoryBean<TestBean> {
145+
static class PrivilegedTestBeanFactoryBean implements FactoryBean<TestBean> {
145146

146147
@Override
147148
public TestBean getObject() throws Exception {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.beans.factory.aot;
18+
19+
/**
20+
* An empty test bean used by code generation.
21+
*
22+
* @author Stephane Nicoll
23+
*/
24+
public class TestBean {
25+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.beans.factory.aot;
18+
19+
import org.springframework.beans.factory.FactoryBean;
20+
21+
/**
22+
* A public {@link FactoryBean}.
23+
*
24+
* @author Stephane Nicoll
25+
*/
26+
public class TestBeanFactoryBean implements FactoryBean<TestBean> {
27+
28+
@Override
29+
public TestBean getObject() throws Exception {
30+
return new TestBean();
31+
}
32+
33+
@Override
34+
public Class<?> getObjectType() {
35+
return TestBean.class;
36+
}
37+
38+
}

0 commit comments

Comments
 (0)