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.List;
19  import java.util.Map;
20  
21  import org.apache.ibatis.reflection.MetaObject;
22  import org.apache.ibatis.reflection.ReflectionException;
23  import org.apache.ibatis.reflection.SystemMetaObject;
24  import org.apache.ibatis.reflection.property.PropertyTokenizer;
25  
26  /**
27   * @author Clinton Begin
28   */
29  public abstract class BaseWrapper implements ObjectWrapper {
30  
31    protected static final Object[] NO_ARGUMENTS = {};
32    protected final MetaObject metaObject;
33  
34    protected BaseWrapper(MetaObject metaObject) {
35      this.metaObject = metaObject;
36    }
37  
38    protected Object resolveCollection(PropertyTokenizer prop, Object object) {
39      if ("".equals(prop.getName())) {
40        return object;
41      }
42      return metaObject.getValue(prop.getName());
43    }
44  
45    protected Object getCollectionValue(PropertyTokenizer prop, Object collection) {
46      if (collection == null) {
47        throw new ReflectionException("Cannot get the value '" + prop.getIndexedName() + "' because the property '"
48            + prop.getName() + "' is null.");
49      }
50      if (collection instanceof Map) {
51        return ((Map) collection).get(prop.getIndex());
52      }
53      int i = Integer.parseInt(prop.getIndex());
54      if (collection instanceof List) {
55        return ((List) collection).get(i);
56      } else if (collection instanceof Object[]) {
57        return ((Object[]) collection)[i];
58      } else if (collection instanceof char[]) {
59        return ((char[]) collection)[i];
60      } else if (collection instanceof boolean[]) {
61        return ((boolean[]) collection)[i];
62      } else if (collection instanceof byte[]) {
63        return ((byte[]) collection)[i];
64      } else if (collection instanceof double[]) {
65        return ((double[]) collection)[i];
66      } else if (collection instanceof float[]) {
67        return ((float[]) collection)[i];
68      } else if (collection instanceof int[]) {
69        return ((int[]) collection)[i];
70      } else if (collection instanceof long[]) {
71        return ((long[]) collection)[i];
72      } else if (collection instanceof short[]) {
73        return ((short[]) collection)[i];
74      } else {
75        throw new ReflectionException("Cannot get the value '" + prop.getIndexedName() + "' because the property '"
76            + prop.getName() + "' is not Map, List or Array.");
77      }
78    }
79  
80    protected void setCollectionValue(PropertyTokenizer prop, Object collection, Object value) {
81      if (collection == null) {
82        throw new ReflectionException("Cannot set the value '" + prop.getIndexedName() + "' because the property '"
83            + prop.getName() + "' is null.");
84      }
85      if (collection instanceof Map) {
86        ((Map) collection).put(prop.getIndex(), value);
87      } else {
88        int i = Integer.parseInt(prop.getIndex());
89        if (collection instanceof List) {
90          ((List) collection).set(i, value);
91        } else if (collection instanceof Object[]) {
92          ((Object[]) collection)[i] = value;
93        } else if (collection instanceof char[]) {
94          ((char[]) collection)[i] = (Character) value;
95        } else if (collection instanceof boolean[]) {
96          ((boolean[]) collection)[i] = (Boolean) value;
97        } else if (collection instanceof byte[]) {
98          ((byte[]) collection)[i] = (Byte) value;
99        } else if (collection instanceof double[]) {
100         ((double[]) collection)[i] = (Double) value;
101       } else if (collection instanceof float[]) {
102         ((float[]) collection)[i] = (Float) value;
103       } else if (collection instanceof int[]) {
104         ((int[]) collection)[i] = (Integer) value;
105       } else if (collection instanceof long[]) {
106         ((long[]) collection)[i] = (Long) value;
107       } else if (collection instanceof short[]) {
108         ((short[]) collection)[i] = (Short) value;
109       } else {
110         throw new ReflectionException("Cannot set the value '" + prop.getIndexedName() + "' because the property '"
111             + prop.getName() + "' is not Map, List or Array.");
112       }
113     }
114   }
115 
116   protected Object getChildValue(PropertyTokenizer prop) {
117     MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
118     if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
119       return null;
120     }
121     return metaValue.getValue(prop.getChildren());
122   }
123 
124   protected void setChildValue(PropertyTokenizer prop, Object value) {
125     MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
126     if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
127       if (value == null) {
128         // don't instantiate child path if value is null
129         return;
130       }
131       metaValue = instantiatePropertyValue(null, new PropertyTokenizer(prop.getName()), metaObject.getObjectFactory());
132     }
133     metaValue.setValue(prop.getChildren(), value);
134   }
135 }