Browse Source

Fix enclosing class in TypeReference for inner type arrays

See gh-28664
pull/26393/head
Moritz Halbritter 2 years ago committed by Stephane Nicoll
parent
commit
c34de54d8a
  1. 10
      spring-core/src/main/java/org/springframework/aot/hint/ReflectionTypeReference.java
  2. 17
      spring-core/src/test/java/org/springframework/aot/hint/ReflectionTypeReferenceTests.java

10
spring-core/src/main/java/org/springframework/aot/hint/ReflectionTypeReference.java

@ -28,10 +28,18 @@ final class ReflectionTypeReference extends AbstractTypeReference { @@ -28,10 +28,18 @@ final class ReflectionTypeReference extends AbstractTypeReference {
private final Class<?> type;
private ReflectionTypeReference(Class<?> type) {
super(type.getPackageName(), type.getSimpleName(), safeCreate(type.getEnclosingClass()));
super(type.getPackageName(), type.getSimpleName(), safeCreate(getEnclosingClass(type)));
this.type = type;
}
@Nullable
private static Class<?> getEnclosingClass(Class<?> type) {
if (type.isArray()) {
return type.getComponentType().getEnclosingClass();
}
return type.getEnclosingClass();
}
@Nullable
private static ReflectionTypeReference safeCreate(@Nullable Class<?> type) {
return (type != null ? new ReflectionTypeReference(type) : null);

17
spring-core/src/test/java/org/springframework/aot/hint/ReflectionTypeReferenceTests.java

@ -28,6 +28,7 @@ import static org.assertj.core.api.Assertions.assertThat; @@ -28,6 +28,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link ReflectionTypeReference}.
*
* @author Stephane Nicoll
* @author Moritz Halbritter
*/
class ReflectionTypeReferenceTests {
@ -38,10 +39,22 @@ class ReflectionTypeReferenceTests { @@ -38,10 +39,22 @@ class ReflectionTypeReferenceTests {
}
static Stream<Arguments> reflectionTargetNames() {
return Stream.of(Arguments.of(ReflectionTypeReference.of(int.class), "int"),
return Stream.of(
Arguments.of(ReflectionTypeReference.of(int.class), "int"),
Arguments.of(ReflectionTypeReference.of(int[].class), "int[]"),
Arguments.of(ReflectionTypeReference.of(Integer[].class), "java.lang.Integer[]"),
Arguments.of(ReflectionTypeReference.of(Object[].class), "java.lang.Object[]"));
Arguments.of(ReflectionTypeReference.of(Object[].class), "java.lang.Object[]"),
Arguments.of(ReflectionTypeReference.of(StaticInner.class), "org.springframework.aot.hint.ReflectionTypeReferenceTests$StaticInner"),
Arguments.of(ReflectionTypeReference.of(StaticInner[].class), "org.springframework.aot.hint.ReflectionTypeReferenceTests$StaticInner[]"),
Arguments.of(ReflectionTypeReference.of(Inner.class), "org.springframework.aot.hint.ReflectionTypeReferenceTests$Inner"),
Arguments.of(ReflectionTypeReference.of(Inner[].class), "org.springframework.aot.hint.ReflectionTypeReferenceTests$Inner[]")
);
}
static class StaticInner {
}
class Inner {
}
}

Loading…
Cancel
Save