|
| 1 | +/** |
| 2 | + * Copyright 2009-2016 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.apache.ibatis.reflection; |
| 17 | + |
| 18 | +import java.lang.reflect.Method; |
| 19 | +import java.lang.reflect.ParameterizedType; |
| 20 | +import java.lang.reflect.Type; |
| 21 | +import java.lang.reflect.TypeVariable; |
| 22 | +import java.lang.reflect.WildcardType; |
| 23 | + |
| 24 | +/** |
| 25 | + * @author Iwao AVE! |
| 26 | + */ |
| 27 | +public class TypeParameterResolver { |
| 28 | + |
| 29 | + /** |
| 30 | + * @return The return type of the method as {@link Type}. If it has type parameters in the declaration,<br> |
| 31 | + * they will be resolved to the actual runtime {@link Type}. |
| 32 | + */ |
| 33 | + public static Type resolveReturnType(Method method, Type mapper) { |
| 34 | + Type returnType = method.getGenericReturnType(); |
| 35 | + Class<?> declaringClass = method.getDeclaringClass(); |
| 36 | + Type result = null; |
| 37 | + if (returnType instanceof TypeVariable) { |
| 38 | + result = resolveTypeVar((TypeVariable<?>) returnType, mapper, declaringClass); |
| 39 | + } else if (returnType instanceof ParameterizedType) { |
| 40 | + result = resolveParameterizedType((ParameterizedType) returnType, mapper, declaringClass); |
| 41 | + } else { |
| 42 | + result = returnType; |
| 43 | + } |
| 44 | + return result; |
| 45 | + } |
| 46 | + |
| 47 | + private static ParameterizedType resolveParameterizedType(ParameterizedType parameterizedType, Type mapper, Class<?> declaringClass) { |
| 48 | + Class<?> rawType = (Class<?>) parameterizedType.getRawType(); |
| 49 | + Type[] typeArgs = parameterizedType.getActualTypeArguments(); |
| 50 | + Type[] args = new Type[typeArgs.length]; |
| 51 | + for (int i = 0; i < typeArgs.length; i++) { |
| 52 | + if (typeArgs[i] instanceof TypeVariable) { |
| 53 | + args[i] = resolveTypeVar((TypeVariable<?>) typeArgs[i], mapper, declaringClass); |
| 54 | + } else if (typeArgs[i] instanceof ParameterizedType) { |
| 55 | + args[i] = resolveParameterizedType((ParameterizedType) typeArgs[i], mapper, declaringClass); |
| 56 | + } else if (typeArgs[i] instanceof WildcardType) { |
| 57 | + args[i] = resolveWildcardType((WildcardType) typeArgs[i], mapper, declaringClass); |
| 58 | + } else { |
| 59 | + args[i] = typeArgs[i]; |
| 60 | + } |
| 61 | + } |
| 62 | + return new ParameterizedTypeImpl(rawType, null, args); |
| 63 | + } |
| 64 | + |
| 65 | + private static Type resolveWildcardType(WildcardType wildcardType, Type mapper, Class<?> declaringClass) { |
| 66 | + Type[] lowerBounds = resolveWildcardTypeBounds(wildcardType.getLowerBounds(), mapper, declaringClass); |
| 67 | + Type[] upperBounds = resolveWildcardTypeBounds(wildcardType.getUpperBounds(), mapper, declaringClass); |
| 68 | + return new WildcardTypeImpl(lowerBounds, upperBounds); |
| 69 | + } |
| 70 | + |
| 71 | + private static Type[] resolveWildcardTypeBounds(Type[] bounds, Type mapper, Class<?> declaringClass) { |
| 72 | + Type[] result = new Type[bounds.length]; |
| 73 | + for (int i = 0; i < bounds.length; i++) { |
| 74 | + if (bounds[i] instanceof TypeVariable) { |
| 75 | + result[i] = resolveTypeVar((TypeVariable<?>) bounds[i], mapper, declaringClass); |
| 76 | + } else if (bounds[i] instanceof ParameterizedType) { |
| 77 | + result[i] = resolveParameterizedType((ParameterizedType) bounds[i], mapper, declaringClass); |
| 78 | + } else if (bounds[i] instanceof WildcardType) { |
| 79 | + result[i] = resolveWildcardType((WildcardType) bounds[i], mapper, declaringClass); |
| 80 | + } else { |
| 81 | + result[i] = bounds[i]; |
| 82 | + } |
| 83 | + } |
| 84 | + return result; |
| 85 | + } |
| 86 | + |
| 87 | + private static Type resolveTypeVar(TypeVariable<?> typeVar, Type type, Class<?> declaringClass) { |
| 88 | + Type result = null; |
| 89 | + Class<?> clazz = null; |
| 90 | + if (type instanceof Class) { |
| 91 | + clazz = (Class<?>) type; |
| 92 | + } else if (type instanceof ParameterizedType) { |
| 93 | + ParameterizedType parameterizedType = (ParameterizedType) type; |
| 94 | + clazz = (Class<?>) parameterizedType.getRawType(); |
| 95 | + } else { |
| 96 | + throw new IllegalArgumentException("The 2nd arg must be Class or ParameterizedType, but was: " + type.getClass()); |
| 97 | + } |
| 98 | + |
| 99 | + if (clazz == declaringClass) { |
| 100 | + return typeVar; |
| 101 | + } |
| 102 | + |
| 103 | + Type[] superInterfaces = clazz.getGenericInterfaces(); |
| 104 | + for (Type superInterface : superInterfaces) { |
| 105 | + if (superInterface instanceof ParameterizedType) { |
| 106 | + ParameterizedType parentAsType = (ParameterizedType) superInterface; |
| 107 | + Class<?> parentAsClass = (Class<?>) parentAsType.getRawType(); |
| 108 | + if (declaringClass == parentAsClass) { |
| 109 | + Type[] typeArgs = parentAsType.getActualTypeArguments(); |
| 110 | + TypeVariable<?>[] declaredTypeVars = declaringClass.getTypeParameters(); |
| 111 | + for (int i = 0; i < declaredTypeVars.length; i++) { |
| 112 | + if (declaredTypeVars[i] == typeVar) { |
| 113 | + if (typeArgs[i] instanceof TypeVariable) { |
| 114 | + TypeVariable<?>[] typeParams = clazz.getTypeParameters(); |
| 115 | + for (int j = 0; j < typeParams.length; j++) { |
| 116 | + if (typeParams[j] == typeArgs[i]) { |
| 117 | + result = ((ParameterizedType) type).getActualTypeArguments()[j]; |
| 118 | + break; |
| 119 | + } |
| 120 | + } |
| 121 | + } else { |
| 122 | + result = typeArgs[i]; |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + } else if (declaringClass.isAssignableFrom(parentAsClass)) { |
| 127 | + result = resolveTypeVar(typeVar, parentAsType, declaringClass); |
| 128 | + } |
| 129 | + } else if (superInterface instanceof Class) { |
| 130 | + if (declaringClass.isAssignableFrom((Class<?>) superInterface)) { |
| 131 | + result = resolveTypeVar(typeVar, superInterface, declaringClass); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + if (result == null) { |
| 136 | + return typeVar; |
| 137 | + } |
| 138 | + return result; |
| 139 | + } |
| 140 | + |
| 141 | + private TypeParameterResolver() { |
| 142 | + super(); |
| 143 | + } |
| 144 | + |
| 145 | + static class ParameterizedTypeImpl implements ParameterizedType { |
| 146 | + private Class<?> rawType; |
| 147 | + |
| 148 | + private Type ownerType; |
| 149 | + |
| 150 | + private Type[] actualTypeArguments; |
| 151 | + |
| 152 | + public ParameterizedTypeImpl(Class<?> rawType, Type ownerType, Type[] actualTypeArguments) { |
| 153 | + super(); |
| 154 | + this.rawType = rawType; |
| 155 | + this.ownerType = ownerType; |
| 156 | + this.actualTypeArguments = actualTypeArguments; |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public Type[] getActualTypeArguments() { |
| 161 | + return actualTypeArguments; |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public Type getOwnerType() { |
| 166 | + return ownerType; |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public Type getRawType() { |
| 171 | + return rawType; |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + static class WildcardTypeImpl implements WildcardType { |
| 176 | + private Type[] lowerBounds; |
| 177 | + |
| 178 | + private Type[] upperBounds; |
| 179 | + |
| 180 | + private WildcardTypeImpl(Type[] lowerBounds, Type[] upperBounds) { |
| 181 | + super(); |
| 182 | + this.lowerBounds = lowerBounds; |
| 183 | + this.upperBounds = upperBounds; |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public Type[] getLowerBounds() { |
| 188 | + return lowerBounds; |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + public Type[] getUpperBounds() { |
| 193 | + return upperBounds; |
| 194 | + } |
| 195 | + } |
| 196 | +} |
0 commit comments