View Javadoc
1   /*
2    *    Copyright 2009-2023 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    *       https://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 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     if (!prop.hasNext()) {
116       return objectWrapper.get(prop);
117     }
118     MetaObject metaValue = metaObjectForProperty(prop.getIndexedName());
119     if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
120       return null;
121     }
122     return metaValue.getValue(prop.getChildren());
123   }
124 
125   public void setValue(String name, Object value) {
126     PropertyTokenizer prop = new PropertyTokenizer(name);
127     if (prop.hasNext()) {
128       MetaObject metaValue = metaObjectForProperty(prop.getIndexedName());
129       if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
130         if (value == null) {
131           // don't instantiate child path if value is null
132           return;
133         }
134         metaValue = objectWrapper.instantiatePropertyValue(name, prop, objectFactory);
135       }
136       metaValue.setValue(prop.getChildren(), value);
137     } else {
138       objectWrapper.set(prop, value);
139     }
140   }
141 
142   public MetaObject metaObjectForProperty(String name) {
143     Object value = getValue(name);
144     return MetaObject.forObject(value, objectFactory, objectWrapperFactory, reflectorFactory);
145   }
146 
147   public ObjectWrapper getObjectWrapper() {
148     return objectWrapper;
149   }
150 
151   public boolean isCollection() {
152     return objectWrapper.isCollection();
153   }
154 
155   public void add(Object element) {
156     objectWrapper.add(element);
157   }
158 
159   public <E> void addAll(List<E> list) {
160     objectWrapper.addAll(list);
161   }
162 
163 }