Collect constructor type information via Constructor.getParameters().

This commit fixes an issue where we fail to detect all type arguments from a given constructor. calling getGenericParameterTypes in some cases does not include all Types, we now explicitly iterate over the parameters and extract the parameterized type that is used for creating the TypeInformation.

Closes: #2313
Original pull request: #2314.
This commit is contained in:
Christoph Strobl
2021-02-25 10:24:32 +01:00
committed by Mark Paluch
parent f3dfd0b52c
commit 8917115ba9
2 changed files with 48 additions and 7 deletions

View File

@@ -33,6 +33,7 @@ import org.springframework.data.util.ClassTypeInformation;
* @author Oliver Gierke
* @author Roman Rodov
* @author Mark Paluch
* @author Christoph Strobl
*/
public class PreferredConstructorDiscovererUnitTests<P extends PersistentProperty<P>> {
@@ -112,6 +113,30 @@ public class PreferredConstructorDiscovererUnitTests<P extends PersistentPropert
});
}
@Test // GH-2313
void capturesEnclosingTypeParameterOfNonStaticInnerClass() {
assertThat(PreferredConstructorDiscoverer.discover(NonStaticWithGenericTypeArgUsedInCtor.class))
.satisfies(ctor -> {
assertThat(ctor.getParameters()).hasSize(2);
assertThat(ctor.getParameters().get(0).getName()).isEqualTo("this$0");
assertThat(ctor.getParameters().get(1).getName()).isEqualTo("value");
});
}
@Test // GH-2313
void capturesSuperClassEnclosingTypeParameterOfNonStaticInnerClass() {
assertThat(PreferredConstructorDiscoverer.discover(NonStaticInnerWithGenericArgUsedInCtor.class))
.satisfies(ctor -> {
assertThat(ctor.getParameters()).hasSize(2);
assertThat(ctor.getParameters().get(0).getName()).isEqualTo("this$0");
assertThat(ctor.getParameters().get(1).getName()).isEqualTo("value");
});
}
static class SyntheticConstructor {
@PersistenceConstructor
private SyntheticConstructor(String x) {}
@@ -164,4 +189,22 @@ public class PreferredConstructorDiscovererUnitTests<P extends PersistentPropert
}
}
static class GenericTypeArgUsedInCtor<T> {
protected GenericTypeArgUsedInCtor(T value) {}
}
class NonStaticWithGenericTypeArgUsedInCtor<T> {
protected NonStaticWithGenericTypeArgUsedInCtor(T value) {}
}
class NonStaticInnerWithGenericArgUsedInCtor<T> extends GenericTypeArgUsedInCtor<T> {
public NonStaticInnerWithGenericArgUsedInCtor(T value) {
super(value);
}
}
}