Permalink
Browse files

Fixed issue 188

@captor annotation should work OK with nested parametrized type

--HG--
extra : convert_revision : svn%3Aaa2aecf3-ea3e-0410-9d70-716747e7c967/trunk%401984
  • Loading branch information...
mockitoguy committed May 16, 2010
1 parent a154b66 commit 7a647a702c8af81ccf5d37b09c11529c6c0cb1b7
@@ -17,7 +17,12 @@ public Class getGenericType(Field field) {
Type generic = field.getGenericType();
if (generic != null && generic instanceof ParameterizedType) {
Type actual = ((ParameterizedType) generic).getActualTypeArguments()[0];
return (Class) actual;
if (actual instanceof Class) {
return (Class) actual;
} else if (actual instanceof ParameterizedType) {
//in case of nested generics we don't go deep
return (Class) ((ParameterizedType) actual).getRawType();
}
}
return Object.class;
@@ -3,6 +3,7 @@
import static org.junit.Assert.*;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -17,17 +18,25 @@
Set<Integer> two;
Map<Double, String> map;
String nonGeneric;
List<Set<String>> nested;
List<Set<Collection<String>>> multiNested;
@Test
public void shouldFindGenericClass() throws Exception {
assertEquals(m.getGenericType(field("one")), String.class);
assertEquals(m.getGenericType(field("two")), Integer.class);
assertEquals(m.getGenericType(field("map")), Double.class);
assertEquals(String.class, m.getGenericType(field("one")));
assertEquals(Integer.class, m.getGenericType(field("two")));
assertEquals(Double.class, m.getGenericType(field("map")));
}
@Test
public void shouldGetObjectForNonGeneric() throws Exception {
assertEquals(m.getGenericType(field("nonGeneric")), Object.class);
assertEquals(Object.class, m.getGenericType(field("nonGeneric")));
}
@Test
public void shouldDealWithNestedGenerics() throws Exception {
assertEquals(Set.class, m.getGenericType(field("nested")));
assertEquals(Set.class, m.getGenericType(field("multiNested")));
}
private Field field(String fieldName) throws SecurityException, NoSuchFieldException {

0 comments on commit 7a647a7

Please sign in to comment.