Fix handling of reflection target name in TypeReference
This commit adds a `getName` to `TypeReference` that provides a way to generate the reflection target name of a type. This typically handle primitives (omitting the `java.lang` packages) and arrays. Closes gh-28347
This commit is contained in:
@@ -16,7 +16,12 @@
|
||||
|
||||
package org.springframework.aot.generator;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import org.springframework.aot.hint.TypeReference;
|
||||
import org.springframework.javapoet.ClassName;
|
||||
@@ -30,6 +35,19 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*/
|
||||
class GeneratedTypeReferenceTests {
|
||||
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("reflectionTargetNames")
|
||||
void hasSuitableReflectionTargetName(TypeReference typeReference, String binaryName) {
|
||||
assertThat(typeReference.getName()).isEqualTo(binaryName);
|
||||
}
|
||||
|
||||
static Stream<Arguments> reflectionTargetNames() {
|
||||
return Stream.of(
|
||||
Arguments.of(GeneratedTypeReference.of(ClassName.get("com.example", "Test")), "com.example.Test"),
|
||||
Arguments.of(GeneratedTypeReference.of(ClassName.get("com.example", "Test", "Inner")), "com.example.Test$Inner"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createWithClassName() {
|
||||
GeneratedTypeReference typeReference = GeneratedTypeReference.of(
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* 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.aot.hint;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link ReflectionTypeReference}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
class ReflectionTypeReferenceTests {
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("reflectionTargetNames")
|
||||
void typeReferenceFromClasHasSuitableReflectionTargetName(TypeReference typeReference, String binaryName) {
|
||||
assertThat(typeReference.getName()).isEqualTo(binaryName);
|
||||
}
|
||||
|
||||
static Stream<Arguments> reflectionTargetNames() {
|
||||
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[]"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,8 +16,12 @@
|
||||
|
||||
package org.springframework.aot.hint;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -30,6 +34,48 @@ import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
*/
|
||||
class SimpleTypeReferenceTests {
|
||||
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("primitivesAndPrimitivesArray")
|
||||
void primitivesAreHandledProperly(TypeReference typeReference, String expectedName) {
|
||||
assertThat(typeReference.getName()).isEqualTo(expectedName);
|
||||
assertThat(typeReference.getCanonicalName()).isEqualTo(expectedName);
|
||||
assertThat(typeReference.getPackageName()).isEqualTo("java.lang");
|
||||
}
|
||||
|
||||
static Stream<Arguments> primitivesAndPrimitivesArray() {
|
||||
return Stream.of(
|
||||
Arguments.of(SimpleTypeReference.of("boolean"), "boolean"),
|
||||
Arguments.of(SimpleTypeReference.of("byte"), "byte"),
|
||||
Arguments.of(SimpleTypeReference.of("short"), "short"),
|
||||
Arguments.of(SimpleTypeReference.of("int"), "int"),
|
||||
Arguments.of(SimpleTypeReference.of("long"), "long"),
|
||||
Arguments.of(SimpleTypeReference.of("char"), "char"),
|
||||
Arguments.of(SimpleTypeReference.of("float"), "float"),
|
||||
Arguments.of(SimpleTypeReference.of("double"), "double"),
|
||||
Arguments.of(SimpleTypeReference.of("boolean[]"), "boolean[]"),
|
||||
Arguments.of(SimpleTypeReference.of("byte[]"), "byte[]"),
|
||||
Arguments.of(SimpleTypeReference.of("short[]"), "short[]"),
|
||||
Arguments.of(SimpleTypeReference.of("int[]"), "int[]"),
|
||||
Arguments.of(SimpleTypeReference.of("long[]"), "long[]"),
|
||||
Arguments.of(SimpleTypeReference.of("char[]"), "char[]"),
|
||||
Arguments.of(SimpleTypeReference.of("float[]"), "float[]"),
|
||||
Arguments.of(SimpleTypeReference.of("double[]"), "double[]"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("arrays")
|
||||
void arraysHaveSuitableReflectionTargetName(TypeReference typeReference, String expectedName) {
|
||||
assertThat(typeReference.getName()).isEqualTo(expectedName);
|
||||
}
|
||||
|
||||
static Stream<Arguments> arrays() {
|
||||
return Stream.of(
|
||||
Arguments.of(SimpleTypeReference.of("java.lang.Object[]"), "java.lang.Object[]"),
|
||||
Arguments.of(SimpleTypeReference.of("java.lang.Integer[]"), "java.lang.Integer[]"),
|
||||
Arguments.of(SimpleTypeReference.of("com.example.Test[]"), "com.example.Test[]"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void typeReferenceInRootPackage() {
|
||||
TypeReference type = SimpleTypeReference.of("MyRootClass");
|
||||
|
||||
@@ -30,6 +30,7 @@ class TypeReferenceTests {
|
||||
@Test
|
||||
void typeReferenceWithClassName() {
|
||||
TypeReference type = TypeReference.of("java.lang.String");
|
||||
assertThat(type.getName()).isEqualTo("java.lang.String");
|
||||
assertThat(type.getCanonicalName()).isEqualTo("java.lang.String");
|
||||
assertThat(type.getPackageName()).isEqualTo("java.lang");
|
||||
assertThat(type.getSimpleName()).isEqualTo("String");
|
||||
@@ -39,6 +40,7 @@ class TypeReferenceTests {
|
||||
@Test
|
||||
void typeReferenceWithInnerClassName() {
|
||||
TypeReference type = TypeReference.of("com.example.Example$Inner");
|
||||
assertThat(type.getName()).isEqualTo("com.example.Example$Inner");
|
||||
assertThat(type.getCanonicalName()).isEqualTo("com.example.Example.Inner");
|
||||
assertThat(type.getPackageName()).isEqualTo("com.example");
|
||||
assertThat(type.getSimpleName()).isEqualTo("Inner");
|
||||
@@ -53,6 +55,7 @@ class TypeReferenceTests {
|
||||
@Test
|
||||
void typeReferenceWithNestedInnerClassName() {
|
||||
TypeReference type = TypeReference.of("com.example.Example$Inner$Nested");
|
||||
assertThat(type.getName()).isEqualTo("com.example.Example$Inner$Nested");
|
||||
assertThat(type.getCanonicalName()).isEqualTo("com.example.Example.Inner.Nested");
|
||||
assertThat(type.getPackageName()).isEqualTo("com.example");
|
||||
assertThat(type.getSimpleName()).isEqualTo("Nested");
|
||||
|
||||
Reference in New Issue
Block a user