Fix a bug where TypeParameterMatcher fails when a type parameter is an array

- Fixes #1103
This commit is contained in:
Trustin Lee 2013-02-28 10:29:03 -08:00
parent 6246825fda
commit b712b030fa
2 changed files with 20 additions and 2 deletions

View File

@ -16,6 +16,8 @@
package io.netty.util.internal;
import java.lang.reflect.Array;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.GenericDeclaration;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
@ -102,11 +104,20 @@ public abstract class TypeParameterMatcher {
((ParameterizedType) currentClass.getGenericSuperclass()).getActualTypeArguments();
Type actualTypeParam = actualTypeParams[typeParamIndex];
if (actualTypeParam instanceof ParameterizedType) {
actualTypeParam = ((ParameterizedType) actualTypeParam).getRawType();
}
if (actualTypeParam instanceof Class) {
return (Class<?>) actualTypeParam;
}
if (actualTypeParam instanceof ParameterizedType) {
return (Class<?>) ((ParameterizedType) actualTypeParam).getRawType();
if (actualTypeParam instanceof GenericArrayType) {
Type componentType = ((GenericArrayType) actualTypeParam).getGenericComponentType();
if (componentType instanceof ParameterizedType) {
componentType = ((ParameterizedType) componentType).getRawType();
}
if (componentType instanceof Class) {
return Array.newInstance((Class<?>) componentType, 0).getClass();
}
}
if (actualTypeParam instanceof TypeVariable) {
// Resolved type parameter points to another type parameter.

View File

@ -105,4 +105,11 @@ public class TypeParameterMatcherTest {
@SuppressWarnings("ClassMayBeInterface")
private static class T { }
private static class U<E> { E a; }
@Test
public void testArrayAsTypeParam() throws Exception {
TypeParameterMatcher m = TypeParameterMatcher.find(new U<byte[]>() { }, U.class, "E");
assertFalse(m.match(new Object()));
assertTrue(m.match(new byte[1]));
}
}