/* * Copyright 2009-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.ibatis.type; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; /** * References a generic type. * * @param <T> the referenced type * @author Simone Tripodi * @since 3.1.0 * 3.1新加的类型引用,为了引用一个泛型类型 */ public abstract class TypeReference<T> { //引用的原生类型 private final Type rawType; protected TypeReference() { rawType = getSuperclassTypeParameter(getClass()); } Type getSuperclassTypeParameter(Class<?> clazz) { //得到泛型T的实际类型 Type genericSuperclass = clazz.getGenericSuperclass(); //???? 怎么会是Class型 if (genericSuperclass instanceof Class) { // try to climb up the hierarchy until meet something useful if (TypeReference.class != genericSuperclass) { return getSuperclassTypeParameter(clazz.getSuperclass()); } throw new TypeException("'" + getClass() + "' extends TypeReference but misses the type parameter. " + "Remove the extension or add a type parameter to it."); } Type rawType = ((ParameterizedType) genericSuperclass).getActualTypeArguments()[0]; // TODO remove this when Reflector is fixed to return Types if (rawType instanceof ParameterizedType) { rawType = ((ParameterizedType) rawType).getRawType(); } return rawType; } public final Type getRawType() { return rawType; } @Override public String toString() { return rawType.toString(); } }