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
29
30
31 public abstract class TypeReference<T> {
32
33 private final Type rawType;
34
35 protected TypeReference() {
36 rawType = getSuperclassTypeParameter(getClass());
37 }
38
39 Type getSuperclassTypeParameter(Class<?> clazz) {
40 Type genericSuperclass = clazz.getGenericSuperclass();
41 if (genericSuperclass instanceof Class) {
42
43 if (TypeReference.class != genericSuperclass) {
44 return getSuperclassTypeParameter(clazz.getSuperclass());
45 }
46
47 throw new TypeException("'" + getClass() + "' extends TypeReference but misses the type parameter. "
48 + "Remove the extension or add a type parameter to it.");
49 }
50
51 Type rawType = ((ParameterizedType) genericSuperclass).getActualTypeArguments()[0];
52
53 if (rawType instanceof ParameterizedType) {
54 rawType = ((ParameterizedType) rawType).getRawType();
55 }
56
57 return rawType;
58 }
59
60 public final Type getRawType() {
61 return rawType;
62 }
63
64 @Override
65 public String toString() {
66 return rawType.toString();
67 }
68
69 }