1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.reflection.wrapper;
17
18 import java.util.List;
19 import java.util.Map;
20
21 import org.apache.ibatis.reflection.MetaObject;
22 import org.apache.ibatis.reflection.ReflectionException;
23 import org.apache.ibatis.reflection.SystemMetaObject;
24 import org.apache.ibatis.reflection.property.PropertyTokenizer;
25
26
27
28
29 public abstract class BaseWrapper implements ObjectWrapper {
30
31 protected static final Object[] NO_ARGUMENTS = {};
32 protected final MetaObject metaObject;
33
34 protected BaseWrapper(MetaObject metaObject) {
35 this.metaObject = metaObject;
36 }
37
38 protected Object resolveCollection(PropertyTokenizer prop, Object object) {
39 if ("".equals(prop.getName())) {
40 return object;
41 }
42 return metaObject.getValue(prop.getName());
43 }
44
45 protected Object getCollectionValue(PropertyTokenizer prop, Object collection) {
46 if (collection == null) {
47 throw new ReflectionException("Cannot get the value '" + prop.getIndexedName() + "' because the property '"
48 + prop.getName() + "' is null.");
49 }
50 if (collection instanceof Map) {
51 return ((Map) collection).get(prop.getIndex());
52 }
53 int i = Integer.parseInt(prop.getIndex());
54 if (collection instanceof List) {
55 return ((List) collection).get(i);
56 } else if (collection instanceof Object[]) {
57 return ((Object[]) collection)[i];
58 } else if (collection instanceof char[]) {
59 return ((char[]) collection)[i];
60 } else if (collection instanceof boolean[]) {
61 return ((boolean[]) collection)[i];
62 } else if (collection instanceof byte[]) {
63 return ((byte[]) collection)[i];
64 } else if (collection instanceof double[]) {
65 return ((double[]) collection)[i];
66 } else if (collection instanceof float[]) {
67 return ((float[]) collection)[i];
68 } else if (collection instanceof int[]) {
69 return ((int[]) collection)[i];
70 } else if (collection instanceof long[]) {
71 return ((long[]) collection)[i];
72 } else if (collection instanceof short[]) {
73 return ((short[]) collection)[i];
74 } else {
75 throw new ReflectionException("Cannot get the value '" + prop.getIndexedName() + "' because the property '"
76 + prop.getName() + "' is not Map, List or Array.");
77 }
78 }
79
80 protected void setCollectionValue(PropertyTokenizer prop, Object collection, Object value) {
81 if (collection == null) {
82 throw new ReflectionException("Cannot set the value '" + prop.getIndexedName() + "' because the property '"
83 + prop.getName() + "' is null.");
84 }
85 if (collection instanceof Map) {
86 ((Map) collection).put(prop.getIndex(), value);
87 } else {
88 int i = Integer.parseInt(prop.getIndex());
89 if (collection instanceof List) {
90 ((List) collection).set(i, value);
91 } else if (collection instanceof Object[]) {
92 ((Object[]) collection)[i] = value;
93 } else if (collection instanceof char[]) {
94 ((char[]) collection)[i] = (Character) value;
95 } else if (collection instanceof boolean[]) {
96 ((boolean[]) collection)[i] = (Boolean) value;
97 } else if (collection instanceof byte[]) {
98 ((byte[]) collection)[i] = (Byte) value;
99 } else if (collection instanceof double[]) {
100 ((double[]) collection)[i] = (Double) value;
101 } else if (collection instanceof float[]) {
102 ((float[]) collection)[i] = (Float) value;
103 } else if (collection instanceof int[]) {
104 ((int[]) collection)[i] = (Integer) value;
105 } else if (collection instanceof long[]) {
106 ((long[]) collection)[i] = (Long) value;
107 } else if (collection instanceof short[]) {
108 ((short[]) collection)[i] = (Short) value;
109 } else {
110 throw new ReflectionException("Cannot set the value '" + prop.getIndexedName() + "' because the property '"
111 + prop.getName() + "' is not Map, List or Array.");
112 }
113 }
114 }
115
116 protected Object getChildValue(PropertyTokenizer prop) {
117 MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
118 if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
119 return null;
120 }
121 return metaValue.getValue(prop.getChildren());
122 }
123
124 protected void setChildValue(PropertyTokenizer prop, Object value) {
125 MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
126 if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
127 if (value == null) {
128
129 return;
130 }
131 metaValue = instantiatePropertyValue(null, new PropertyTokenizer(prop.getName()), metaObject.getObjectFactory());
132 }
133 metaValue.setValue(prop.getChildren(), value);
134 }
135 }