DATACMNS-39 - TypeDiscoverer handles WildcardTypes now.

We now resolve the first bound from wildcard types now.
This commit is contained in:
Oliver Gierke
2011-12-02 15:34:08 +01:00
parent 6546d81b6f
commit e195abc5c9
2 changed files with 43 additions and 1 deletions

View File

@@ -25,6 +25,7 @@ import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -101,6 +102,22 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
if (fieldType instanceof GenericArrayType) {
return new GenericArrayTypeInformation((GenericArrayType) fieldType, this);
}
if (fieldType instanceof WildcardType) {
WildcardType wildcardType = (WildcardType) fieldType;
Type[] bounds = wildcardType.getLowerBounds();
if (bounds.length > 0) {
return createInfo(bounds[0]);
}
bounds = wildcardType.getUpperBounds();
if (bounds.length > 0) {
return createInfo(bounds[0]);
}
}
throw new IllegalArgumentException();
}

View File

@@ -157,7 +157,27 @@ public class ClassTypeInformationUnitTests {
TypeInformation<PropertyGetter> info = ClassTypeInformation.from(PropertyGetter.class);
assertThat(ClassTypeInformation.from(PropertyGetter.class), is(sameInstance(info)));
}
/**
* @see DATACMNS-39
*/
@Test
public void resolvesWildCardTypeCorrectly() {
TypeInformation<ClassWithWildCardBound> information = ClassTypeInformation.from(ClassWithWildCardBound.class);
TypeInformation<?> property = information.getProperty("wildcard");
assertThat(property.isCollectionLike(), is(true));
assertThat(property.getComponentType().getType(), is(typeCompatibleWith(String.class)));
property = information.getProperty("complexWildcard");
assertThat(property.isCollectionLike(), is(true));
TypeInformation<?> component = property.getComponentType();
assertThat(component.isCollectionLike(), is(true));
assertThat(component.getComponentType().getType(), is(typeCompatibleWith(String.class)));
}
static class StringMapContainer extends MapContainer<String> {
}
@@ -231,4 +251,9 @@ public class ClassTypeInformationUnitTests {
return _name.getBytes();
}
}
static class ClassWithWildCardBound {
List<? extends String> wildcard;
List<? extends Collection<? extends String>> complexWildcard;
}
}