Skip to content

Commit da4b539

Browse files
committed
Stop generating generic type as Object for unresolved generics
Closes gh-29454
1 parent 70bb785 commit da4b539

File tree

5 files changed

+98
-1
lines changed

5 files changed

+98
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private static CodeBlock generateCode(ResolvableType resolvableType, boolean all
4545
return CodeBlock.of("$T.NONE", ResolvableType.class);
4646
}
4747
Class<?> type = ClassUtils.getUserClass(resolvableType.toClass());
48-
if (resolvableType.hasGenerics()) {
48+
if (resolvableType.hasGenerics() && !resolvableType.hasUnresolvableGenerics()) {
4949
return generateCodeWithGenerics(resolvableType, type);
5050
}
5151
if (allowClassResult) {

spring-context/src/test/java/org/springframework/context/aot/ApplicationContextAotGeneratorTests.java

+14
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@
5454
import org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver;
5555
import org.springframework.context.support.GenericApplicationContext;
5656
import org.springframework.context.testfixture.context.annotation.AutowiredComponent;
57+
import org.springframework.context.testfixture.context.annotation.AutowiredGenericTemplate;
5758
import org.springframework.context.testfixture.context.annotation.CglibConfiguration;
5859
import org.springframework.context.testfixture.context.annotation.ConfigurableCglibConfiguration;
60+
import org.springframework.context.testfixture.context.annotation.GenericTemplateConfiguration;
5961
import org.springframework.context.testfixture.context.annotation.InitDestroyComponent;
6062
import org.springframework.context.testfixture.context.annotation.LazyAutowiredFieldComponent;
6163
import org.springframework.context.testfixture.context.annotation.LazyAutowiredMethodComponent;
@@ -114,6 +116,18 @@ void processAheadOfTimeWhenHasAutowiring() {
114116
});
115117
}
116118

119+
@Test
120+
void processAheadOfTimeWhenHasAutowiringOnUnresolvedGeneric() {
121+
GenericApplicationContext applicationContext = new AnnotationConfigApplicationContext();
122+
applicationContext.registerBean(GenericTemplateConfiguration.class);
123+
applicationContext.registerBean("autowiredComponent", AutowiredGenericTemplate.class);
124+
testCompiledResult(applicationContext, (initializer, compiled) -> {
125+
GenericApplicationContext freshApplicationContext = toFreshApplicationContext(initializer);
126+
AutowiredGenericTemplate bean = freshApplicationContext.getBean(AutowiredGenericTemplate.class);
127+
assertThat(bean).hasFieldOrPropertyWithValue("genericTemplate", applicationContext.getBean("genericTemplate"));
128+
});
129+
}
130+
117131
@Test
118132
void processAheadOfTimeWhenHasLazyAutowiringOnField() {
119133
testAutowiredComponent(LazyAutowiredFieldComponent.class, (bean, generationContext) -> {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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.context.testfixture.context.annotation;
18+
19+
import org.springframework.beans.factory.annotation.Autowired;
20+
21+
public class AutowiredGenericTemplate {
22+
23+
private GenericTemplate<Integer> genericTemplate;
24+
25+
@Autowired
26+
public void setGenericTemplate(GenericTemplate<Integer> genericTemplate) {
27+
this.genericTemplate = genericTemplate;
28+
}
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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.context.testfixture.context.annotation;
18+
19+
public interface GenericTemplate<V> {
20+
21+
void process(V item);
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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.context.testfixture.context.annotation;
18+
19+
import org.springframework.context.annotation.Bean;
20+
import org.springframework.context.annotation.Configuration;
21+
22+
@Configuration(proxyBeanMethods = false)
23+
public class GenericTemplateConfiguration {
24+
25+
@Bean
26+
public GenericTemplate<?> genericTemplate() {
27+
return v -> {};
28+
}
29+
30+
}

0 commit comments

Comments
 (0)