Created
May 3, 2011 04:35
-
-
Save ryoasai/952819 to your computer and use it in GitHub Desktop.
Spring's TypeDescriptor cannot resolve parameterized property type.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.ryoasai; | |
import static org.hamcrest.CoreMatchers.*; | |
import static org.junit.Assert.*; | |
import java.util.List; | |
import org.hamcrest.Matcher; | |
import org.junit.Test; | |
import org.springframework.beans.BeanWrapper; | |
import org.springframework.beans.ConfigurablePropertyAccessor; | |
import org.springframework.beans.PropertyAccessorFactory; | |
import org.springframework.core.convert.TypeDescriptor; | |
public class TypeDescriptor_ParameterizedPropertyAccess_Test { | |
public static Matcher<Class<?>> isSameClassAs(Class<?> clazz) { | |
@SuppressWarnings("unchecked") //double cast idiom | |
Matcher<Class<?>> result = (Matcher<Class<?>>)(Matcher<?>)is((Object)clazz); | |
return result; | |
} | |
static class Parent<T> { | |
private List<T> list; | |
public List<T> getList() { | |
return list; | |
} | |
} | |
static class Child extends Parent<String> { | |
/* | |
@Override | |
public List<String> getList() { | |
return super.getList(); | |
}*/ | |
} | |
@Test | |
public void testPropertyAccess() { | |
BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(new Child()); | |
TypeDescriptor typeDesc = beanWrapper.getPropertyTypeDescriptor("list"); | |
assertThat(typeDesc.getType(), isSameClassAs(List.class)); | |
assertThat(typeDesc.getElementType(), isSameClassAs(String.class)); // failed! | |
assertThat(typeDesc.asString(), is("java.util.List<java.lang.String>")); // failed! | |
assertThat(typeDesc.isCollection(), is(true)); | |
} | |
@Test | |
public void testFieldAccess() { | |
ConfigurablePropertyAccessor fieldAccessor = PropertyAccessorFactory.forDirectFieldAccess(new Child()); | |
TypeDescriptor typeDesc = fieldAccessor.getPropertyTypeDescriptor("list"); | |
assertThat(typeDesc.getType(), isSameClassAs(List.class)); | |
assertThat(typeDesc.getElementType(), isSameClassAs(String.class)); // failed! | |
assertThat(typeDesc.asString(), is("java.util.List<java.lang.String>")); // failed! | |
assertThat(typeDesc.isCollection(), is(true)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment