View Javadoc
1   /**
2    *    Copyright 2009-2015 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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   * @author Clinton Begin
32   */
33  public class MetaObject {
34  
35    private Object originalObject;
36    private ObjectWrapper objectWrapper;
37    private ObjectFactory objectFactory;
38    private ObjectWrapperFactory objectWrapperFactory;
39    private 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 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 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 && prop.getChildren() != null) {
132           // don't instantiate child path if value is null
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 }