1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.type;
17
18 import java.lang.reflect.ParameterizedType;
19 import java.lang.reflect.Type;
20
21
22
23
24
25
26
27
28 public abstract class TypeReference<T> {
29
30 private final Type rawType;
31
32 protected TypeReference() {
33 rawType = getSuperclassTypeParameter(getClass());
34 }
35
36 Type getSuperclassTypeParameter(Class<?> clazz) {
37 Type genericSuperclass = clazz.getGenericSuperclass();
38 if (genericSuperclass instanceof Class) {
39
40 if (TypeReference.class != genericSuperclass) {
41 return getSuperclassTypeParameter(clazz.getSuperclass());
42 }
43
44 throw new TypeException("'" + getClass() + "' extends TypeReference but misses the type parameter. "
45 + "Remove the extension or add a type parameter to it.");
46 }
47
48 Type rawType = ((ParameterizedType) genericSuperclass).getActualTypeArguments()[0];
49
50 if (rawType instanceof ParameterizedType) {
51 rawType = ((ParameterizedType) rawType).getRawType();
52 }
53
54 return rawType;
55 }
56
57 public final Type getRawType() {
58 return rawType;
59 }
60
61 @Override
62 public String toString() {
63 return rawType.toString();
64 }
65
66 }