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:
committed by
Mark Paluch
parent
5454b37870
commit
8f4befff83
@@ -20,6 +20,7 @@ import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.GenericArrayType;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Parameter;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.lang.reflect.TypeVariable;
|
||||
@@ -175,14 +176,11 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
|
||||
Assert.notNull(constructor, "Constructor must not be null!");
|
||||
|
||||
Type[] types = constructor.getGenericParameterTypes();
|
||||
List<TypeInformation<?>> result = new ArrayList<>(types.length);
|
||||
|
||||
for (Type parameterType : types) {
|
||||
result.add(createInfo(parameterType));
|
||||
List<TypeInformation<?>> parameterTypes = new ArrayList<>(constructor.getParameterCount());
|
||||
for(Parameter parameter : constructor.getParameters()) {
|
||||
parameterTypes.add(createInfo(parameter.getParameterizedType()));
|
||||
}
|
||||
|
||||
return result;
|
||||
return parameterTypes;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user