Skip to content

Commit

Permalink
optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
timandy committed Mar 4, 2019
1 parent 07add54 commit 978523f
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions src/main/java/com/alibaba/fastjson/util/TypeUtils.java
Expand Up @@ -2307,13 +2307,13 @@ private static Type getCollectionItemType(ParameterizedType parameterizedType) {
return getWildcardTypeUpperBounds(actualTypeArguments[0]); return getWildcardTypeUpperBounds(actualTypeArguments[0]);
} }
Class<?> rawClass = (Class<?>) rawType; Class<?> rawClass = (Class<?>) rawType;
Map<TypeVariable, Type> typeParameterMap = createTypeParameterMap(rawClass.getTypeParameters(), actualTypeArguments); Map<TypeVariable, Type> actualTypeMap = createActualTypeMap(rawClass.getTypeParameters(), actualTypeArguments);
Type superType = getCollectionSuperType(rawClass); Type superType = getCollectionSuperType(rawClass);
if (superType instanceof ParameterizedType) { if (superType instanceof ParameterizedType) {
Class<?> superClass = getRawClass(superType); Class<?> superClass = getRawClass(superType);
Type[] superClassTypeParameters = ((ParameterizedType) superType).getActualTypeArguments(); Type[] superClassTypeParameters = ((ParameterizedType) superType).getActualTypeArguments();
return superClassTypeParameters.length > 0 return superClassTypeParameters.length > 0
? getCollectionItemType(makeParameterizedType(superClass, superClassTypeParameters, typeParameterMap)) ? getCollectionItemType(makeParameterizedType(superClass, superClassTypeParameters, actualTypeMap))
: getCollectionItemType(superClass); : getCollectionItemType(superClass);
} }
return getCollectionItemType((Class<?>) superType); return getCollectionItemType((Class<?>) superType);
Expand All @@ -2333,34 +2333,33 @@ private static Type getCollectionSuperType(Class<?> clazz) {
return assignable == null ? clazz.getGenericSuperclass() : assignable; return assignable == null ? clazz.getGenericSuperclass() : assignable;
} }


private static Map<TypeVariable, Type> createTypeParameterMap(TypeVariable[] typeParameters, Type[] actualTypeArguments) { private static Map<TypeVariable, Type> createActualTypeMap(TypeVariable[] typeParameters, Type[] actualTypeArguments) {
int length = typeParameters.length; int length = typeParameters.length;
Map<TypeVariable, Type> typeParameterMap = new HashMap<TypeVariable, Type>(length); Map<TypeVariable, Type> actualTypeMap = new HashMap<TypeVariable, Type>(length);
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
typeParameterMap.put(typeParameters[i], actualTypeArguments[i]); actualTypeMap.put(typeParameters[i], actualTypeArguments[i]);
} }
return typeParameterMap; return actualTypeMap;
} }


private static ParameterizedType makeParameterizedType(Class<?> rawClass, Type[] typeParameters, Map<TypeVariable, Type> typeParameterMap) { private static ParameterizedType makeParameterizedType(Class<?> rawClass, Type[] typeParameters, Map<TypeVariable, Type> actualTypeMap) {
int length = typeParameters.length; int length = typeParameters.length;
Type[] actualTypeArguments = new Type[length]; Type[] actualTypeArguments = new Type[length];
System.arraycopy(typeParameters, 0, actualTypeArguments, 0, length); for (int i = 0; i < length; i++) {
for (int i = 0; i < actualTypeArguments.length; i++) { actualTypeArguments[i] = getActualType(typeParameters[i], actualTypeMap);
actualTypeArguments[i] = expandType(actualTypeArguments[i], typeParameterMap);
} }
return new ParameterizedTypeImpl(actualTypeArguments, null, rawClass); return new ParameterizedTypeImpl(actualTypeArguments, null, rawClass);
} }


private static Type expandType(Type type, Map<TypeVariable, Type> typeParameterMap) { private static Type getActualType(Type typeParameter, Map<TypeVariable, Type> actualTypeMap) {
if (type instanceof TypeVariable) { if (typeParameter instanceof TypeVariable) {
return typeParameterMap.get(type); return actualTypeMap.get(typeParameter);
} else if (type instanceof ParameterizedType) { } else if (typeParameter instanceof ParameterizedType) {
return makeParameterizedType(getRawClass(type), ((ParameterizedType) type).getActualTypeArguments(), typeParameterMap); return makeParameterizedType(getRawClass(typeParameter), ((ParameterizedType) typeParameter).getActualTypeArguments(), actualTypeMap);
} else if (type instanceof GenericArrayType) { } else if (typeParameter instanceof GenericArrayType) {
return new GenericArrayTypeImpl(expandType(((GenericArrayType) type).getGenericComponentType(), typeParameterMap)); return new GenericArrayTypeImpl(getActualType(((GenericArrayType) typeParameter).getGenericComponentType(), actualTypeMap));
} }
return type; return typeParameter;
} }


private static Type getWildcardTypeUpperBounds(Type type) { private static Type getWildcardTypeUpperBounds(Type type) {
Expand Down

0 comments on commit 978523f

Please sign in to comment.