View Javadoc
1   /*
2    *    Copyright 2009-2024 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.wrapper;
17  
18  import java.util.HashMap;
19  import java.util.List;
20  import java.util.Map;
21  
22  import org.apache.ibatis.reflection.MetaObject;
23  import org.apache.ibatis.reflection.SystemMetaObject;
24  import org.apache.ibatis.reflection.factory.ObjectFactory;
25  import org.apache.ibatis.reflection.property.PropertyTokenizer;
26  
27  /**
28   * @author Clinton Begin
29   */
30  public class MapWrapper extends BaseWrapper {
31  
32    protected final Map<String, Object> map;
33  
34    public MapWrapper(MetaObject metaObject, Map<String, Object> map) {
35      super(metaObject);
36      this.map = map;
37    }
38  
39    @Override
40    public Object get(PropertyTokenizer prop) {
41      if (prop.hasNext()) {
42        return getChildValue(prop);
43      } else if (prop.getIndex() != null) {
44        return getCollectionValue(prop, resolveCollection(prop, map));
45      } else {
46        return map.get(prop.getName());
47      }
48    }
49  
50    @Override
51    public void set(PropertyTokenizer prop, Object value) {
52      if (prop.hasNext()) {
53        setChildValue(prop, value);
54      } else if (prop.getIndex() != null) {
55        setCollectionValue(prop, resolveCollection(prop, map), value);
56      } else {
57        map.put(prop.getName(), value);
58      }
59    }
60  
61    @Override
62    public String findProperty(String name, boolean useCamelCaseMapping) {
63      return name;
64    }
65  
66    @Override
67    public String[] getGetterNames() {
68      return map.keySet().toArray(new String[0]);
69    }
70  
71    @Override
72    public String[] getSetterNames() {
73      return map.keySet().toArray(new String[0]);
74    }
75  
76    @Override
77    public Class<?> getSetterType(String name) {
78      PropertyTokenizer prop = new PropertyTokenizer(name);
79      if (prop.hasNext()) {
80        MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
81        if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
82          return Object.class;
83        } else {
84          return metaValue.getSetterType(prop.getChildren());
85        }
86      }
87      if (map.get(name) != null) {
88        return map.get(name).getClass();
89      } else {
90        return Object.class;
91      }
92    }
93  
94    @Override
95    public Class<?> getGetterType(String name) {
96      PropertyTokenizer prop = new PropertyTokenizer(name);
97      if (prop.hasNext()) {
98        MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
99        if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
100         return Object.class;
101       } else {
102         return metaValue.getGetterType(prop.getChildren());
103       }
104     }
105     if (map.get(name) != null) {
106       return map.get(name).getClass();
107     } else {
108       return Object.class;
109     }
110   }
111 
112   @Override
113   public boolean hasSetter(String name) {
114     return true;
115   }
116 
117   @Override
118   public boolean hasGetter(String name) {
119     PropertyTokenizer prop = new PropertyTokenizer(name);
120     if (!prop.hasNext()) {
121       return map.containsKey(prop.getName());
122     }
123     if (map.containsKey(prop.getIndexedName())) {
124       MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
125       if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
126         return true;
127       } else {
128         return metaValue.hasGetter(prop.getChildren());
129       }
130     } else {
131       return false;
132     }
133   }
134 
135   @Override
136   public MetaObject instantiatePropertyValue(String name, PropertyTokenizer prop, ObjectFactory objectFactory) {
137     HashMap<String, Object> map = new HashMap<>();
138     set(prop, map);
139     return MetaObject.forObject(map, metaObject.getObjectFactory(), metaObject.getObjectWrapperFactory(),
140         metaObject.getReflectorFactory());
141   }
142 
143   @Override
144   public boolean isCollection() {
145     return false;
146   }
147 
148   @Override
149   public void add(Object element) {
150     throw new UnsupportedOperationException();
151   }
152 
153   @Override
154   public <E> void addAll(List<E> element) {
155     throw new UnsupportedOperationException();
156   }
157 
158 }