Browse Source

Stop generating generic type as Object for unresolved generics

Closes gh-29454
pull/29461/head
Stephane Nicoll 3 years ago
parent
commit
da4b539f20
  1. 2
      spring-beans/src/main/java/org/springframework/beans/factory/aot/ResolvableTypeCodeGenerator.java
  2. 14
      spring-context/src/test/java/org/springframework/context/aot/ApplicationContextAotGeneratorTests.java
  3. 30
      spring-context/src/testFixtures/java/org/springframework/context/testfixture/context/annotation/AutowiredGenericTemplate.java
  4. 23
      spring-context/src/testFixtures/java/org/springframework/context/testfixture/context/annotation/GenericTemplate.java
  5. 30
      spring-context/src/testFixtures/java/org/springframework/context/testfixture/context/annotation/GenericTemplateConfiguration.java

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

@ -45,7 +45,7 @@ final class ResolvableTypeCodeGenerator { @@ -45,7 +45,7 @@ final class ResolvableTypeCodeGenerator {
return CodeBlock.of("$T.NONE", ResolvableType.class);
}
Class<?> type = ClassUtils.getUserClass(resolvableType.toClass());
if (resolvableType.hasGenerics()) {
if (resolvableType.hasGenerics() && !resolvableType.hasUnresolvableGenerics()) {
return generateCodeWithGenerics(resolvableType, type);
}
if (allowClassResult) {

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

@ -54,8 +54,10 @@ import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor; @@ -54,8 +54,10 @@ import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
import org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.context.testfixture.context.annotation.AutowiredComponent;
import org.springframework.context.testfixture.context.annotation.AutowiredGenericTemplate;
import org.springframework.context.testfixture.context.annotation.CglibConfiguration;
import org.springframework.context.testfixture.context.annotation.ConfigurableCglibConfiguration;
import org.springframework.context.testfixture.context.annotation.GenericTemplateConfiguration;
import org.springframework.context.testfixture.context.annotation.InitDestroyComponent;
import org.springframework.context.testfixture.context.annotation.LazyAutowiredFieldComponent;
import org.springframework.context.testfixture.context.annotation.LazyAutowiredMethodComponent;
@ -114,6 +116,18 @@ class ApplicationContextAotGeneratorTests { @@ -114,6 +116,18 @@ class ApplicationContextAotGeneratorTests {
});
}
@Test
void processAheadOfTimeWhenHasAutowiringOnUnresolvedGeneric() {
GenericApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.registerBean(GenericTemplateConfiguration.class);
applicationContext.registerBean("autowiredComponent", AutowiredGenericTemplate.class);
testCompiledResult(applicationContext, (initializer, compiled) -> {
GenericApplicationContext freshApplicationContext = toFreshApplicationContext(initializer);
AutowiredGenericTemplate bean = freshApplicationContext.getBean(AutowiredGenericTemplate.class);
assertThat(bean).hasFieldOrPropertyWithValue("genericTemplate", applicationContext.getBean("genericTemplate"));
});
}
@Test
void processAheadOfTimeWhenHasLazyAutowiringOnField() {
testAutowiredComponent(LazyAutowiredFieldComponent.class, (bean, generationContext) -> {

30
spring-context/src/testFixtures/java/org/springframework/context/testfixture/context/annotation/AutowiredGenericTemplate.java

@ -0,0 +1,30 @@ @@ -0,0 +1,30 @@
/*
* 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.context.testfixture.context.annotation;
import org.springframework.beans.factory.annotation.Autowired;
public class AutowiredGenericTemplate {
private GenericTemplate<Integer> genericTemplate;
@Autowired
public void setGenericTemplate(GenericTemplate<Integer> genericTemplate) {
this.genericTemplate = genericTemplate;
}
}

23
spring-context/src/testFixtures/java/org/springframework/context/testfixture/context/annotation/GenericTemplate.java

@ -0,0 +1,23 @@ @@ -0,0 +1,23 @@
/*
* 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.context.testfixture.context.annotation;
public interface GenericTemplate<V> {
void process(V item);
}

30
spring-context/src/testFixtures/java/org/springframework/context/testfixture/context/annotation/GenericTemplateConfiguration.java

@ -0,0 +1,30 @@ @@ -0,0 +1,30 @@
/*
* 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.context.testfixture.context.annotation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration(proxyBeanMethods = false)
public class GenericTemplateConfiguration {
@Bean
public GenericTemplate<?> genericTemplate() {
return v -> {};
}
}
Loading…
Cancel
Save