1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.reflection;
17
18 import java.util.Collection;
19 import java.util.List;
20 import java.util.Map;
21
22 import org.apache.ibatis.reflection.factory.ObjectFactory;
23 import org.apache.ibatis.reflection.property.PropertyTokenizer;
24 import org.apache.ibatis.reflection.wrapper.BeanWrapper;
25 import org.apache.ibatis.reflection.wrapper.CollectionWrapper;
26 import org.apache.ibatis.reflection.wrapper.MapWrapper;
27 import org.apache.ibatis.reflection.wrapper.ObjectWrapper;
28 import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;
29
30
31
32
33 public class MetaObject {
34
35 private final Object originalObject;
36 private final ObjectWrapper objectWrapper;
37 private final ObjectFactory objectFactory;
38 private final ObjectWrapperFactory objectWrapperFactory;
39 private final ReflectorFactory reflectorFactory;
40
41 private MetaObject(Object object, ObjectFactory objectFactory, ObjectWrapperFactory objectWrapperFactory, ReflectorFactory reflectorFactory) {
42 this.originalObject = object;
43 this.objectFactory = objectFactory;
44 this.objectWrapperFactory = objectWrapperFactory;
45 this.reflectorFactory = reflectorFactory;
46
47 if (object instanceof ObjectWrapper) {
48 this.objectWrapper = (ObjectWrapper) object;
49 } else if (objectWrapperFactory.hasWrapperFor(object)) {
50 this.objectWrapper = objectWrapperFactory.getWrapperFor(this, object);
51 } else if (object instanceof Map) {
52 this.objectWrapper = new MapWrapper(this, (Map) object);
53 } else if (object instanceof Collection) {
54 this.objectWrapper = new CollectionWrapper(this, (Collection) object);
55 } else {
56 this.objectWrapper = new BeanWrapper(this, object);
57 }
58 }
59
60 public static MetaObject forObject(Object object, ObjectFactory objectFactory, ObjectWrapperFactory objectWrapperFactory, ReflectorFactory reflectorFactory) {
61 if (object == null) {
62 return SystemMetaObject.NULL_META_OBJECT;
63 } else {
64 return new MetaObject(object, objectFactory, objectWrapperFactory, reflectorFactory);
65 }
66 }
67
68 public ObjectFactory getObjectFactory() {
69 return objectFactory;
70 }
71
72 public ObjectWrapperFactory getObjectWrapperFactory() {
73 return objectWrapperFactory;
74 }
75
76 public ReflectorFactory getReflectorFactory() {
77 return reflectorFactory;
78 }
79
80 public Object getOriginalObject() {
81 return originalObject;
82 }
83
84 public String findProperty(String propName, boolean useCamelCaseMapping) {
85 return objectWrapper.findProperty(propName, useCamelCaseMapping);
86 }
87
88 public String[] getGetterNames() {
89 return objectWrapper.getGetterNames();
90 }
91
92 public String[] getSetterNames() {
93 return objectWrapper.getSetterNames();
94 }
95
96 public Class<?> getSetterType(String name) {
97 return objectWrapper.getSetterType(name);
98 }
99
100 public Class<?> getGetterType(String name) {
101 return objectWrapper.getGetterType(name);
102 }
103
104 public boolean hasSetter(String name) {
105 return objectWrapper.hasSetter(name);
106 }
107
108 public boolean hasGetter(String name) {
109 return objectWrapper.hasGetter(name);
110 }
111
112 public Object getValue(String name) {
113 PropertyTokenizer/PropertyTokenizer.html#PropertyTokenizer">PropertyTokenizer prop = new PropertyTokenizer(name);
114 if (prop.hasNext()) {
115 MetaObject metaValue = metaObjectForProperty(prop.getIndexedName());
116 if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
117 return null;
118 } else {
119 return metaValue.getValue(prop.getChildren());
120 }
121 } else {
122 return objectWrapper.get(prop);
123 }
124 }
125
126 public void setValue(String name, Object value) {
127 PropertyTokenizer/PropertyTokenizer.html#PropertyTokenizer">PropertyTokenizer prop = new PropertyTokenizer(name);
128 if (prop.hasNext()) {
129 MetaObject metaValue = metaObjectForProperty(prop.getIndexedName());
130 if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
131 if (value == null) {
132
133 return;
134 } else {
135 metaValue = objectWrapper.instantiatePropertyValue(name, prop, objectFactory);
136 }
137 }
138 metaValue.setValue(prop.getChildren(), value);
139 } else {
140 objectWrapper.set(prop, value);
141 }
142 }
143
144 public MetaObject metaObjectForProperty(String name) {
145 Object value = getValue(name);
146 return MetaObject.forObject(value, objectFactory, objectWrapperFactory, reflectorFactory);
147 }
148
149 public ObjectWrapper getObjectWrapper() {
150 return objectWrapper;
151 }
152
153 public boolean isCollection() {
154 return objectWrapper.isCollection();
155 }
156
157 public void add(Object element) {
158 objectWrapper.add(element);
159 }
160
161 public <E> void addAll(List<E> list) {
162 objectWrapper.addAll(list);
163 }
164
165 }