Fix TypeReference#getName for CGLIB proxies

This commit makes sure that the double '$' sign typically used by
generated cglib proxies are not used as an inner class separator.

Closes gh-29252
This commit is contained in:
Stephane Nicoll 2022-10-04 15:57:20 +02:00
parent 9820e3341d
commit f2e9d112b1
3 changed files with 33 additions and 1 deletions

View File

@ -49,7 +49,7 @@ final class SimpleTypeReference extends AbstractTypeReference {
if (!className.contains("$")) {
return createTypeReference(className);
}
String[] elements = className.split("\\$");
String[] elements = className.split("(?<!\\$)\\$(?!\\$)");
SimpleTypeReference typeReference = createTypeReference(elements[0]);
for (int i = 1; i < elements.length; i++) {
typeReference = new SimpleTypeReference(typeReference.getPackageName(), elements[i], typeReference);

View File

@ -73,6 +73,23 @@ class GeneratedTypeReferenceTests {
});
}
@Test
void nameOfCglibProxy() {
TypeReference reference = GeneratedTypeReference.of(
ClassName.get("com.example", "Test$$SpringCGLIB$$0"));
assertThat(reference.getSimpleName()).isEqualTo("Test$$SpringCGLIB$$0");
assertThat(reference.getEnclosingType()).isNull();
}
@Test
void nameOfNestedCglibProxy() {
TypeReference reference = GeneratedTypeReference.of(
ClassName.get("com.example", "Test").nestedClass("Another$$SpringCGLIB$$0"));
assertThat(reference.getSimpleName()).isEqualTo("Another$$SpringCGLIB$$0");
assertThat(reference.getEnclosingType()).isNotNull();
assertThat(reference.getEnclosingType().getSimpleName()).isEqualTo("Test");
}
@Test
void equalsWithIdenticalCanonicalNameIsTrue() {
assertThat(GeneratedTypeReference.of(ClassName.get("java.lang", "String")))

View File

@ -76,6 +76,21 @@ class SimpleTypeReferenceTests {
Arguments.of(SimpleTypeReference.of("com.example.Test[]"), "com.example.Test[]"));
}
@Test
void nameOfCglibProxy() {
TypeReference reference = TypeReference.of("com.example.Test$$SpringCGLIB$$0");
assertThat(reference.getSimpleName()).isEqualTo("Test$$SpringCGLIB$$0");
assertThat(reference.getEnclosingType()).isNull();
}
@Test
void nameOfNestedCglibProxy() {
TypeReference reference = TypeReference.of("com.example.Test$Another$$SpringCGLIB$$0");
assertThat(reference.getSimpleName()).isEqualTo("Another$$SpringCGLIB$$0");
assertThat(reference.getEnclosingType()).isNotNull();
assertThat(reference.getEnclosingType().getSimpleName()).isEqualTo("Test");
}
@Test
void typeReferenceInRootPackage() {
TypeReference type = SimpleTypeReference.of("MyRootClass");