More robust type parameter detection

- now handles '<List<Integer>>'
This commit is contained in:
Trustin Lee 2013-02-10 01:50:49 +09:00
parent 27190fcb7f
commit 8f895a7e9a

View File

@ -18,7 +18,10 @@ package io.netty.util.internal;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
public abstract class TypeParameterMatcher {
@ -45,12 +48,25 @@ public abstract class TypeParameterMatcher {
for (;;) {
if (currentClass.getSuperclass() == parameterizedSuperClass) {
Type[] types = ((ParameterizedType) currentClass.getGenericSuperclass()).getActualTypeArguments();
if (types.length - 1 < typeParamIndex || !(types[typeParamIndex] instanceof Class)) {
if (types.length - 1 < typeParamIndex) {
List<Type> typeList = new ArrayList<Type>(types.length);
Collections.addAll(typeList, types);
throw new IllegalStateException(
"cannot determine the type of the type parameter of " + thisClass.getSimpleName());
"invalid typeParamIndex: " + typeParamIndex + " (typeParams: " + typeList + ')');
}
Type t = types[typeParamIndex];
Class<?> messageType;
if (t instanceof Class) {
messageType = (Class<?>) t;
} else if (t instanceof ParameterizedType) {
messageType = (Class<?>) ((ParameterizedType) t).getRawType();
} else {
throw new IllegalStateException(
"cannot determine the type of the type parameter of " +
thisClass.getSimpleName() + ": " + t);
}
Class<?> messageType = (Class<?>) types[typeParamIndex];
if (messageType == Object.class) {
matcher = NOOP;
} else if (PlatformDependent.hasJavassist()) {