Restore proper code generation for types with nested generics
This commit aligns code generation to recent improvement in the core container regarding type detection. Now that nested types are properly resolved, our code generation that uses hasResolvableGenerics() is taking the wrong decision if only a nested type has an unresolved generics. Previously, this was hidden by the fact that the core container would not resolve them recursively. A new hasResolvableGenerics() method allows to verify that at least one direct generic type is resolved. This restore our intent of checking at the first level only and let recursive invocations figure out if they have to write the raw type or the type with generics. Closes gh-33069
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
* Copyright 2002-2024 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.
|
||||
@@ -290,6 +290,34 @@ class ValueCodeGeneratorTests {
|
||||
+ "ResolvableType.forClassWithGenerics(List.class, String.class))");
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateWhenUnresolvedGenericType() throws NoSuchFieldException {
|
||||
ResolvableType resolvableType = ResolvableType
|
||||
.forField(SampleTypes.class.getField("genericList"));
|
||||
assertThat(resolve(generateCode(resolvableType)))
|
||||
.hasImport(ResolvableType.class, List.class)
|
||||
.hasValueCode("ResolvableType.forClass(List.class)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateWhenUnresolvedNestedGenericType() throws NoSuchFieldException {
|
||||
ResolvableType resolvableType = ResolvableType
|
||||
.forField(SampleTypes.class.getField("mapWithNestedGenericInValueType"));
|
||||
assertThat(resolve(generateCode(resolvableType)))
|
||||
.hasImport(ResolvableType.class, List.class)
|
||||
.hasValueCode("""
|
||||
ResolvableType.forClassWithGenerics(Map.class, ResolvableType.forClass(String.class), \
|
||||
ResolvableType.forClass(List.class))""");
|
||||
}
|
||||
|
||||
static class SampleTypes<A> {
|
||||
|
||||
public List<A> genericList;
|
||||
|
||||
public Map<String, List<A>> mapWithNestedGenericInValueType;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Nested
|
||||
|
||||
@@ -1289,6 +1289,30 @@ class ResolvableTypeTests {
|
||||
assertThat(narrow.getGeneric().resolve()).isEqualTo(String.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void hasResolvableGenerics() throws Exception {
|
||||
ResolvableType type = ResolvableType.forField(Fields.class.getField("stringList"));
|
||||
assertThat(type.hasResolvableGenerics()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void hasResolvableGenericsWithSingleBoundedWildcard() throws Exception {
|
||||
ResolvableType type = ResolvableType.forField(Fields.class.getField("wildcardType"));
|
||||
assertThat(type.hasResolvableGenerics()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void hasResolvableGenericsWithSingleParameterizedType() throws Exception {
|
||||
ResolvableType type = ResolvableType.forField(Fields.class.getField("parameterizedType"));
|
||||
assertThat(type.hasResolvableGenerics()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void hasResolvableGenericsWithSingleWildcard() throws Exception {
|
||||
ResolvableType type = ResolvableType.forField(Fields.class.getField("anyListElement"));
|
||||
assertThat(type.hasResolvableGenerics()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void hasUnresolvableGenerics() throws Exception {
|
||||
ResolvableType type = ResolvableType.forField(Fields.class.getField("stringList"));
|
||||
@@ -1466,6 +1490,8 @@ class ResolvableTypeTests {
|
||||
|
||||
public List<String>[][][] genericMultiArrayType;
|
||||
|
||||
public List<?> anyListElement;
|
||||
|
||||
public List<? extends Number> wildcardType;
|
||||
|
||||
public List<? super Number> wildcardSuperType = new ArrayList<Object>();
|
||||
|
||||
Reference in New Issue
Block a user