Fix a bug where TypeParameterMatcher fails when a type parameter is an array
- Fixes #1103
This commit is contained in:
parent
6246825fda
commit
b712b030fa
@ -16,6 +16,8 @@
|
|||||||
|
|
||||||
package io.netty.util.internal;
|
package io.netty.util.internal;
|
||||||
|
|
||||||
|
import java.lang.reflect.Array;
|
||||||
|
import java.lang.reflect.GenericArrayType;
|
||||||
import java.lang.reflect.GenericDeclaration;
|
import java.lang.reflect.GenericDeclaration;
|
||||||
import java.lang.reflect.ParameterizedType;
|
import java.lang.reflect.ParameterizedType;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
@ -102,11 +104,20 @@ public abstract class TypeParameterMatcher {
|
|||||||
((ParameterizedType) currentClass.getGenericSuperclass()).getActualTypeArguments();
|
((ParameterizedType) currentClass.getGenericSuperclass()).getActualTypeArguments();
|
||||||
|
|
||||||
Type actualTypeParam = actualTypeParams[typeParamIndex];
|
Type actualTypeParam = actualTypeParams[typeParamIndex];
|
||||||
|
if (actualTypeParam instanceof ParameterizedType) {
|
||||||
|
actualTypeParam = ((ParameterizedType) actualTypeParam).getRawType();
|
||||||
|
}
|
||||||
if (actualTypeParam instanceof Class) {
|
if (actualTypeParam instanceof Class) {
|
||||||
return (Class<?>) actualTypeParam;
|
return (Class<?>) actualTypeParam;
|
||||||
}
|
}
|
||||||
if (actualTypeParam instanceof ParameterizedType) {
|
if (actualTypeParam instanceof GenericArrayType) {
|
||||||
return (Class<?>) ((ParameterizedType) actualTypeParam).getRawType();
|
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) {
|
if (actualTypeParam instanceof TypeVariable) {
|
||||||
// Resolved type parameter points to another type parameter.
|
// Resolved type parameter points to another type parameter.
|
||||||
|
@ -105,4 +105,11 @@ public class TypeParameterMatcherTest {
|
|||||||
@SuppressWarnings("ClassMayBeInterface")
|
@SuppressWarnings("ClassMayBeInterface")
|
||||||
private static class T { }
|
private static class T { }
|
||||||
private static class U<E> { E a; }
|
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]));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user