抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

MYBATIS反射模块

Wrapper包

对各种对象的包装器,Wrapper包装器的大部分功能都是委托MetaClass和MetaObject对象实现的,现在来具体看下它们的实现。

ObjectWrapper 类

ObjectWrapper对象包装器接口,基于 MetaClass工具类,定义对指定对象的各种操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* 对象包装器接口
* @author Clinton Begin
*/
public interface ObjectWrapper {
/**
* 根据prop获取对象的属性值
* 1.如果封装的是普通的Bean对象,则调用相应属性的getter方法
* 2.如果封装的集合类,则获取指定key或下标对应的value
* @param prop PropertyTokenizer分词器对象
* @return
*/
Object get(PropertyTokenizer prop);

/**
* 根据prop设置对象的属性值
* 1.如果封装的是普通的Bean对象,则调用相应属性的setter方法
* 2.如果封装的集合类,则设置指定key或下标对应的value
* @param prop PropertyTokenizer分词器对象
* @param value 要设置的值
*/
void set(PropertyTokenizer prop, Object value);

/**
* 查找属性表达式指定的属性,第二个参数表示是否忽略属性表达式中的下划线
* @param name 名称
* @param useCamelCaseMapping 是否开启驼峰命名映射
* @return
*/
String findProperty(String name, boolean useCamelCaseMapping);

/**
* 获取对象的可读属性数组
* @return
*/
String[] getGetterNames();

/**
* 获取对象的可写属性数组
* @return
*/
String[] getSetterNames();

/**
* 根据属性表达式获取对应的setter方法的参数类型
* @param name
* @return
*/
Class<?> getSetterType(String name);

/**
* 根据属性表达式获取对应的getter方法的返回值类型
* @param name
* @return
*/
Class<?> getGetterType(String name);

/**
* 是否有该属性表达式对应的setter方法
* @param name
* @return
*/
boolean hasSetter(String name);

/**
* 是否有该属性表达式对应的getter方法
* @param name
* @return
*/
boolean hasGetter(String name);

/**
* 根据属性表达式实例化对象,并set到当前对象
* 主要作用于初始化对象属性也是对象的场景
* @param name
* @param prop
* @param objectFactory
* @return
*/
MetaObject instantiatePropertyValue(String name, PropertyTokenizer prop, ObjectFactory objectFactory);

/**
* 是否是集合
* @return
*/
boolean isCollection();

/**
* 添加元素到集合
* @param element
*/
void add(Object element);

/**
* 批量添加到集合
* @param element
*/
<E> void addAll(List<E> element);

}

BaseWrapper 类

BaseWrapper抽象类,实现ObjectWrapper接口,为子类BeanWrapper和MapWrapper提供公共的方法和属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* 抽象类,实现ObjectWrapper接口
* 为子类BeanWrapper和MapWrapper提供公共的方法和属性
*
* @author Clinton Begin
*/
public abstract class BaseWrapper implements ObjectWrapper {
// 无参数对象,主要用于执行getter方法所需
protected static final Object[] NO_ARGUMENTS = new Object[0];
// metaObject对象
protected final MetaObject metaObject;

protected BaseWrapper(MetaObject metaObject) {
this.metaObject = metaObject;
}

/**
* 处理集合:
* 根据PropertyTokenizer获取对应属性的集合(Array、List、Map)对象
*
* @param prop PropertyTokenizer 对象
* @param object 指定 Object 对象
* @return
*/
protected Object resolveCollection(PropertyTokenizer prop, Object object) {
if ("".equals(prop.getName())) {
return object;
} else {
return metaObject.getValue(prop.getName());
}
}

/**
* 根据PropertyTokenizer获取集合(Array、List、Map)的值
*
* @param prop PropertyTokenizer 对象
* @param collection 集合(Array、List、Map)
* @return 对应下标或key的值
*/
protected Object getCollectionValue(PropertyTokenizer prop, Object collection) {
if (collection instanceof Map) {
// 如果是Map类型,则index为key
return ((Map) collection).get(prop.getIndex());
} else {
// 如果是其他集合类型,则index为下标
int i = Integer.parseInt(prop.getIndex());
if (collection instanceof List) {
return ((List) collection).get(i);
} else if (collection instanceof Object[]) {
return ((Object[]) collection)[i];
} else if (collection instanceof char[]) {
return ((char[]) collection)[i];
} else if (collection instanceof boolean[]) {
return ((boolean[]) collection)[i];
} else if (collection instanceof byte[]) {
return ((byte[]) collection)[i];
} else if (collection instanceof double[]) {
return ((double[]) collection)[i];
} else if (collection instanceof float[]) {
return ((float[]) collection)[i];
} else if (collection instanceof int[]) {
return ((int[]) collection)[i];
} else if (collection instanceof long[]) {
return ((long[]) collection)[i];
} else if (collection instanceof short[]) {
return ((short[]) collection)[i];
} else {
// 不是集合对象抛ReflectionException异常
throw new ReflectionException("The '" + prop.getName() + "' property of " + collection + " is not a List or Array.");
}
}
}

/**
* 根据PropertyTokenizer设置集合(Array、List、Map)的值
*
* @param prop PropertyTokenizer 对象
* @param collection 集合(Array、List、Map)
* @param value 值
*/
protected void setCollectionValue(PropertyTokenizer prop, Object collection, Object value) {
if (collection instanceof Map) {
// 如果是Map类型,则index为key
((Map) collection).put(prop.getIndex(), value);
} else {
// 如果是其他集合类型,则index为下标
int i = Integer.parseInt(prop.getIndex());
if (collection instanceof List) {
((List) collection).set(i, value);
} else if (collection instanceof Object[]) {
((Object[]) collection)[i] = value;
} else if (collection instanceof char[]) {
((char[]) collection)[i] = (Character) value;
} else if (collection instanceof boolean[]) {
((boolean[]) collection)[i] = (Boolean) value;
} else if (collection instanceof byte[]) {
((byte[]) collection)[i] = (Byte) value;
} else if (collection instanceof double[]) {
((double[]) collection)[i] = (Double) value;
} else if (collection instanceof float[]) {
((float[]) collection)[i] = (Float) value;
} else if (collection instanceof int[]) {
((int[]) collection)[i] = (Integer) value;
} else if (collection instanceof long[]) {
((long[]) collection)[i] = (Long) value;
} else if (collection instanceof short[]) {
((short[]) collection)[i] = (Short) value;
} else {
// 不是集合对象抛ReflectionException异常
throw new ReflectionException("The '" + prop.getName() + "' property of " + collection + " is not a List or Array.");
}
}
}
}

BeanWrapper类

BeanWrapper普通对象包装器,继承BaseWrapper类,基于MetaClass实现对Object的属性操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**
* 继承BaseWrapper,普通Bean包装器。
*
* @author Clinton Begin
*/
public class BeanWrapper extends BaseWrapper {
// 封装的对象
private final Object object;
// 封装的对象对应的MetaClass对象
private final MetaClass metaClass;

public BeanWrapper(MetaObject metaObject, Object object) {
super(metaObject);
this.object = object;
this.metaClass = MetaClass.forClass(object.getClass(), metaObject.getReflectorFactory());
}

/**
* 根据属性获取对象
*
* @param prop PropertyTokenizer分词器对象
* @return
*/
@Override
public Object get(PropertyTokenizer prop) {
// 存在索引信息,则表示该属性表达式中的name部分为集合类型
if (prop.getIndex() != null) {
// 通过BaseWrapper中的公共方法获取集合对象和集合属性
Object collection = resolveCollection(prop, object);
return getCollectionValue(prop, collection);
} else {
// 不存在索引信息,则name部分为普通对象,查找并调用Invoker相关方法获取属性
return getBeanProperty(prop, object);
}
}

/**
* 数值属性的值
*
* @param prop PropertyTokenizer分词器对象
* @param value 要设置的值
*/
@Override
public void set(PropertyTokenizer prop, Object value) {
// 存在索引信息,则表示该属性表达式中的name部分为集合类型
if (prop.getIndex() != null) {
Object collection = resolveCollection(prop, object);
// 通过BaseWrapper中的公共方法获取集合对象,然后设置集合属性
setCollectionValue(prop, collection, value);
} else {
// 不存在索引信息,则name部分为普通对象,查找并调用Invoker相关方法设置属性
setBeanProperty(prop, object, value);
}
}

/**
* 根据名称查找属性
*
* @param name 名称
* @param useCamelCaseMapping 是否开启驼峰命名映射
* @return
*/
@Override
public String findProperty(String name, boolean useCamelCaseMapping) {
return metaClass.findProperty(name, useCamelCaseMapping);
}

/**
* 获取Get属性的数组
*
* @return
*/
@Override
public String[] getGetterNames() {
return metaClass.getGetterNames();
}

/**
* 获取set属性的数组
*
* @return
*/
@Override
public String[] getSetterNames() {
return metaClass.getSetterNames();
}

/**
* 根据属性名称获取set方法的type
*
* @param name 属性名称
* @return 属性类型
*/
@Override
public Class<?> getSetterType(String name) {
// 根据属性表达式创建PropertyTokenizer对象
PropertyTokenizer prop = new PropertyTokenizer(name);
if (prop.hasNext()) {
// 有子表达式,根据indexedName创建MetaObject对象
MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
// 对应的属性值为null
if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
return metaClass.getSetterType(name);
} else {
return metaValue.getSetterType(prop.getChildren());
}
} else {
// 没有子表达式
return metaClass.getSetterType(name);
}
}

/**
* 根据属性获取get方法的type
*
* @param name
* @return
*/
@Override
public Class<?> getGetterType(String name) {
// 根据属性表达式创建PropertyTokenizer对象
PropertyTokenizer prop = new PropertyTokenizer(name);
if (prop.hasNext()) {
// 有子表达式,根据indexedName创建MetaObject对象
MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
// 对应的属性值为null
if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
return metaClass.getGetterType(name);
} else {
return metaValue.getGetterType(prop.getChildren());
}
} else {
// 没有子表达式
return metaClass.getGetterType(name);
}
}

/**
* 判断属性是否包含set方法
*
* @param name 属性名称
* @return
*/
@Override
public boolean hasSetter(String name) {
// 根据属性表达式创建PropertyTokenizer对象
PropertyTokenizer prop = new PropertyTokenizer(name);
if (prop.hasNext()) {
// 有子表达式,根据indexedName创建MetaObject对象
if (metaClass.hasSetter(prop.getIndexedName())) {
MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
//对应的属性值为null
if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
return metaClass.hasSetter(name);
} else {
return metaValue.hasSetter(prop.getChildren());
}
} else {
return false;
}
} else {
// 没有子表达式
return metaClass.hasSetter(name);
}
}

/**
* 判断属性是否包含get方法
*
* @param name 属性名称
* @return
*/
@Override
public boolean hasGetter(String name) {
// 根据属性表达式创建PropertyTokenizer对象
PropertyTokenizer prop = new PropertyTokenizer(name);
if (prop.hasNext()) {
// 有子表达式,根据indexedName创建MetaObject对象
if (metaClass.hasGetter(prop.getIndexedName())) {
MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
//对应的属性值为null
if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
return metaClass.hasGetter(name);
} else {
return metaValue.hasGetter(prop.getChildren());
}
} else {
return false;
}
} else {
// 没有子表达式
return metaClass.hasGetter(name);
}
}

/**
* 主要针对嵌套属性的场景
* 即 address.street address.city时
* address 也是一个对象,且未初始化
* 首次设置address初始化相关对象并赋值到相关属性
*
* @param name 属性名
* @param prop
* @param objectFactory
* @return
*/
@Override
public MetaObject instantiatePropertyValue(String name, PropertyTokenizer prop, ObjectFactory objectFactory) {
MetaObject metaValue;
// 获得 setter 方法的方法参数类型
Class<?> type = getSetterType(prop.getName());
try {
// 创建对象
Object newObject = objectFactory.create(type);
// 创建 MetaObject 对象
metaValue = MetaObject.forObject(newObject, metaObject.getObjectFactory(), metaObject.getObjectWrapperFactory(), metaObject.getReflectorFactory());
// 设置当前对象的值
set(prop, newObject);
} catch (Exception e) {
throw new ReflectionException("Cannot set value of property '" + name + "' because '" + name + "' is null and cannot be instantiated on instance of " + type.getName() + ". Cause:" + e.toString(), e);
}
return metaValue;
}

/**
* 获取Object对应PropertyTokenizer的属性值
*
* @param prop PropertyTokenizer对象
* @param object
* @return
*/
private Object getBeanProperty(PropertyTokenizer prop, Object object) {
try {
// 根据属性名称,查找Reflector.getMethods集合中相应的GetFieldInvoker或MethodInvoker
Invoker method = metaClass.getGetInvoker(prop.getName());
try {
//反射 获取属性
return method.invoke(object, NO_ARGUMENTS);
} catch (Throwable t) {
throw ExceptionUtil.unwrapThrowable(t);
}
} catch (RuntimeException e) {
throw e;
} catch (Throwable t) {
throw new ReflectionException("Could not get property '" + prop.getName() + "' from " + object.getClass() + ". Cause: " + t.toString(), t);
}
}

/**
* 设置Object对应PropertyTokenizer的属性值
*
* @param prop
* @param object
* @param value
*/
private void setBeanProperty(PropertyTokenizer prop, Object object, Object value) {
try {
// 根据属性名称,查找Reflector.getMethods集合中相应的GetFieldInvoker或MethodInvoker
Invoker method = metaClass.getSetInvoker(prop.getName());
Object[] params = {value};
try {
//反射 设置属性
method.invoke(object, params);
} catch (Throwable t) {
throw ExceptionUtil.unwrapThrowable(t);
}
} catch (Throwable t) {
throw new ReflectionException("Could not set property '" + prop.getName() + "' of '" + object.getClass() + "' with value '" + value + "' Cause: " + t.toString(), t);
}
}

@Override
public boolean isCollection() {
return false;
}

@Override
public void add(Object element) {
throw new UnsupportedOperationException();
}

@Override
public <E> void addAll(List<E> list) {
throw new UnsupportedOperationException();
}

}

MapWrapper类

MapWrapper是Map对象包装器,继承BaseWrapper类,基于java.util.Map接口方法实现对属性的操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* Map 对象包装器
*
* @author Clinton Begin
*/
public class MapWrapper extends BaseWrapper {

// 封装的Map对象
private final Map<String, Object> map;

public MapWrapper(MetaObject metaObject, Map<String, Object> map) {
super(metaObject);
this.map = map;
}

@Override
public Object get(PropertyTokenizer prop) {
// 存在索引信息,则表示该属性表达式中的name部分为集合类型
if (prop.getIndex() != null) {
// 通过BaseWrapper中的公共方法获取集合对象和集合属性
Object collection = resolveCollection(prop, map);
return getCollectionValue(prop, collection);
} else {
// 不存在索引信息,则name部分为普通对象,直接从Map中获取值
return map.get(prop.getName());
}
}

/**
* set方法
*
* @param prop PropertyTokenizer分词器对象
* @param value 要设置的值
*/
@Override
public void set(PropertyTokenizer prop, Object value) {
// 存在索引信息,则表示该属性表达式中的name部分为集合类型
if (prop.getIndex() != null) {
// 通过BaseWrapper中的公共方法获取集合对象并设置集合属性
Object collection = resolveCollection(prop, map);
setCollectionValue(prop, collection, value);
} else {
// 不存在索引信息,则name部分为普通对象,直接Map.put设置值
map.put(prop.getName(), value);
}
}

/**
* 查找属性
*
* @param name 名称
* @param useCamelCaseMapping 是否开启驼峰命名映射
* @return
*/
@Override
public String findProperty(String name, boolean useCamelCaseMapping) {
return name;
}

/**
* 获取key的数组
*
* @return
*/
@Override
public String[] getGetterNames() {
return map.keySet().toArray(new String[map.keySet().size()]);
}

/**
* 获取key的数组
*
* @return
*/
@Override
public String[] getSetterNames() {
return map.keySet().toArray(new String[map.keySet().size()]);
}

/**
* 获取set方法的类型
*
* @param name 属性名称
* @return
*/
@Override
public Class<?> getSetterType(String name) {
// 根据属性表达式创建PropertyTokenizer对象
PropertyTokenizer prop = new PropertyTokenizer(name);
if (prop.hasNext()) {
// 有子表达式,根据indexedName创建MetaObject对象
MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
// 对应的属性值为null
if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
return Object.class;
} else {
// 子表达式由MetaObject处理
return metaValue.getSetterType(prop.getChildren());
}
} else {
// 没有子表达式
if (map.get(name) != null) {
return map.get(name).getClass();
} else {
return Object.class;
}
}
}

/**
* 获取getet方法的类型
*
* @param name
* @return
*/
@Override
public Class<?> getGetterType(String name) {
// 根据属性表达式创建PropertyTokenizer对象
PropertyTokenizer prop = new PropertyTokenizer(name);
if (prop.hasNext()) {
// 有子表达式,根据indexedName创建MetaObject对象
MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
// 对应的属性值为null
if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
return Object.class;
} else {
// 子表达式由MetaObject处理
return metaValue.getGetterType(prop.getChildren());
}
} else {
// 没有子表达式
if (map.get(name) != null) {
return map.get(name).getClass();
} else {
return Object.class;
}
}
}

@Override
public boolean hasSetter(String name) {
return true;
}

/**
* 是否包含get方法
*
* @param name 属性
* @return
*/
@Override
public boolean hasGetter(String name) {
// 根据属性表达式创建PropertyTokenizer对象
PropertyTokenizer prop = new PropertyTokenizer(name);
if (prop.hasNext()) {
if (map.containsKey(prop.getIndexedName())) {
// 有子表达式,根据indexedName创建MetaObject对象
MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
// 对应的属性值为null
if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
return true;
} else {
// 子表达式由MetaObject处理
return metaValue.hasGetter(prop.getChildren());
}
} else {
return false;
}
} else {
// 没有子表达式
return map.containsKey(prop.getName());
}
}

/**
* 主要针对嵌套属性的场景
* 即 address.street address.city时
* 首次设置address会创建一个 key为address value为new HashMap<>()
*
* @param name
* @param prop
* @param objectFactory
* @return
*/
@Override
public MetaObject instantiatePropertyValue(String name, PropertyTokenizer prop, ObjectFactory objectFactory) {
// 创建Map
HashMap<String, Object> map = new HashMap<>();
// 设置值
set(prop, map);
// 返回封装map的MetaObject对象
return MetaObject.forObject(map, metaObject.getObjectFactory(), metaObject.getObjectWrapperFactory(), metaObject.getReflectorFactory());
}

/**
* 是否是集合
*
* @return
*/
@Override
public boolean isCollection() {
return false;
}

@Override
public void add(Object element) {
throw new UnsupportedOperationException();
}

@Override
public <E> void addAll(List<E> element) {
throw new UnsupportedOperationException();
}
}

CollectionWrapper类

CollectionWrapper是Collection对象包装器,直接实现ObjectWrapper接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* 集合包装器
*
* @author Clinton Begin
*/
public class CollectionWrapper implements ObjectWrapper {
// 封装的集合对象
private final Collection<Object> object;

public CollectionWrapper(MetaObject metaObject, Collection<Object> object) {
this.object = object;
}

@Override
public Object get(PropertyTokenizer prop) {
throw new UnsupportedOperationException();
}

@Override
public void set(PropertyTokenizer prop, Object value) {
throw new UnsupportedOperationException();
}

@Override
public String findProperty(String name, boolean useCamelCaseMapping) {
throw new UnsupportedOperationException();
}

@Override
public String[] getGetterNames() {
throw new UnsupportedOperationException();
}

@Override
public String[] getSetterNames() {
throw new UnsupportedOperationException();
}

@Override
public Class<?> getSetterType(String name) {
throw new UnsupportedOperationException();
}

@Override
public Class<?> getGetterType(String name) {
throw new UnsupportedOperationException();
}

@Override
public boolean hasSetter(String name) {
throw new UnsupportedOperationException();
}

@Override
public boolean hasGetter(String name) {
throw new UnsupportedOperationException();
}

@Override
public MetaObject instantiatePropertyValue(String name, PropertyTokenizer prop, ObjectFactory objectFactory) {
throw new UnsupportedOperationException();
}

/**
* 是否是集合
*
* @return
*/
@Override
public boolean isCollection() {
return true;
}

/**
* 添加元素到集合
*
* @param element
*/
@Override
public void add(Object element) {
object.add(element);
}

/**
* 批量添加元素到集合
*
* @param element
* @param <E>
*/
@Override
public <E> void addAll(List<E> element) {
object.addAll(element);
}
}

ObjectWrapperFactory类

ObjectWrapperFactory对象包装器工厂接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* 对象包装器工厂
*
* @author Clinton Begin
*/
public interface ObjectWrapperFactory {

/**
* 是否包装了指定对象
*
* @param object 指定对象
* @return 是否
*/
boolean hasWrapperFor(Object object);

/**
* 获得指定对象的 ObjectWrapper 对象
*
* @param metaObject MetaObject 对象
* @param object 指定对象
* @return
*/
ObjectWrapper getWrapperFor(MetaObject metaObject, Object object);
}

DefaultObjectWrapperFactory类

DefaultObjectWrapperFactory实现ObjectWrapperFactory接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 * 默认Object包装类工厂
* @author Clinton Begin
*/
public class DefaultObjectWrapperFactory implements ObjectWrapperFactory {

/**
* 是否包装了对象
* @param object 指定对象
* @return
*/
@Override
public boolean hasWrapperFor(Object object) {
return false;
}

/**
* 未实现的包装工厂
* @param metaObject MetaObject 对象
* @param object 指定对象
* @return
*/
@Override
public ObjectWrapper getWrapperFor(MetaObject metaObject, Object object) {
throw new ReflectionException("The DefaultObjectWrapperFactory should never be called to provide an ObjectWrapper.");
}
}
ObjectWrapperFactory扩展点

可以看到DefaultObjectWrapperFactory并未做任何功能性的设计,但是Mybatis提供了基于mybatis-config配置的扩展。

1
2
<!-- mybatis-config.xml -->
<objectWrapperFactory type="org.apache.ibatis.builder.ExampleObjectWrapperFactory"/>

评论