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,
42 ReflectorFactory reflectorFactory) {
43 this.originalObject = object;
44 this.objectFactory = objectFactory;
45 this.objectWrapperFactory = objectWrapperFactory;
46 this.reflectorFactory = reflectorFactory;
47
48 if (object instanceof ObjectWrapper) {
49 this.objectWrapper = (ObjectWrapper) object;
50 } else if (objectWrapperFactory.hasWrapperFor(object)) {
51 this.objectWrapper = objectWrapperFactory.getWrapperFor(this, object);
52 } else if (object instanceof Map) {
53 this.objectWrapper = new MapWrapper(this, (Map) object);
54 } else if (object instanceof Collection) {
55 this.objectWrapper = new CollectionWrapper(this, (Collection) object);
56 } else {
57 this.objectWrapper = new BeanWrapper(this, object);
58 }
59 }
60
61 public static MetaObject forObject(Object object, ObjectFactory objectFactory,
62 ObjectWrapperFactory objectWrapperFactory, ReflectorFactory reflectorFactory) {
63 if (object == null) {
64 return SystemMetaObject.NULL_META_OBJECT;
65 }
66 return new MetaObject(object, objectFactory, objectWrapperFactory, reflectorFactory);
67 }
68
69 public ObjectFactory getObjectFactory() {
70 return objectFactory;
71 }
72
73 public ObjectWrapperFactory getObjectWrapperFactory() {
74 return objectWrapperFactory;
75 }
76
77 public ReflectorFactory getReflectorFactory() {
78 return reflectorFactory;
79 }
80
81 public Object getOriginalObject() {
82 return originalObject;
83 }
84
85 public String findProperty(String propName, boolean useCamelCaseMapping) {
86 return objectWrapper.findProperty(propName, useCamelCaseMapping);
87 }
88
89 public String[] getGetterNames() {
90 return objectWrapper.getGetterNames();
91 }
92
93 public String[] getSetterNames() {
94 return objectWrapper.getSetterNames();
95 }
96
97 public Class<?> getSetterType(String name) {
98 return objectWrapper.getSetterType(name);
99 }
100
101 public Class<?> getGetterType(String name) {
102 return objectWrapper.getGetterType(name);
103 }
104
105 public boolean hasSetter(String name) {
106 return objectWrapper.hasSetter(name);
107 }
108
109 public boolean hasGetter(String name) {
110 return objectWrapper.hasGetter(name);
111 }
112
113 public Object getValue(String name) {
114 PropertyTokenizer prop = new PropertyTokenizer(name);
115 return objectWrapper.get(prop);
116 }
117
118 public void setValue(String name, Object value) {
119 objectWrapper.set(new PropertyTokenizer(name), value);
120 }
121
122 public MetaObject metaObjectForProperty(String name) {
123 Object value = getValue(name);
124 return MetaObject.forObject(value, objectFactory, objectWrapperFactory, reflectorFactory);
125 }
126
127 public ObjectWrapper getObjectWrapper() {
128 return objectWrapper;
129 }
130
131 public boolean isCollection() {
132 return objectWrapper.isCollection();
133 }
134
135 public void add(Object element) {
136 objectWrapper.add(element);
137 }
138
139 public <E> void addAll(List<E> list) {
140 objectWrapper.addAll(list);
141 }
142
143 }