View Javadoc
1   /**
2    *    Copyright 2009-2020 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.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.property.PropertyTokenizer;
24  
25  /**
26   * @author Clinton Begin
27   */
28  public abstract class BaseWrapper implements ObjectWrapper {
29  
30    protected static final Object[] NO_ARGUMENTS = new Object[0];
31    protected final MetaObject metaObject;
32  
33    protected BaseWrapper(MetaObject metaObject) {
34      this.metaObject = metaObject;
35    }
36  
37    protected Object resolveCollection(PropertyTokenizer prop, Object object) {
38      if ("".equals(prop.getName())) {
39        return object;
40      } else {
41        return metaObject.getValue(prop.getName());
42      }
43    }
44  
45    protected Object getCollectionValue(PropertyTokenizer prop, Object collection) {
46      if (collection instanceof Map) {
47        return ((Map) collection).get(prop.getIndex());
48      } else {
49        int i = Integer.parseInt(prop.getIndex());
50        if (collection instanceof List) {
51          return ((List) collection).get(i);
52        } else if (collection instanceof Object[]) {
53          return ((Object[]) collection)[i];
54        } else if (collection instanceof char[]) {
55          return ((char[]) collection)[i];
56        } else if (collection instanceof boolean[]) {
57          return ((boolean[]) collection)[i];
58        } else if (collection instanceof byte[]) {
59          return ((byte[]) collection)[i];
60        } else if (collection instanceof double[]) {
61          return ((double[]) collection)[i];
62        } else if (collection instanceof float[]) {
63          return ((float[]) collection)[i];
64        } else if (collection instanceof int[]) {
65          return ((int[]) collection)[i];
66        } else if (collection instanceof long[]) {
67          return ((long[]) collection)[i];
68        } else if (collection instanceof short[]) {
69          return ((short[]) collection)[i];
70        } else {
71          throw new ReflectionException("The '" + prop.getName() + "' property of " + collection + " is not a List or Array.");
72        }
73      }
74    }
75  
76    protected void setCollectionValue(PropertyTokenizer prop, Object collection, Object value) {
77      if (collection instanceof Map) {
78        ((Map) collection).put(prop.getIndex(), value);
79      } else {
80        int i = Integer.parseInt(prop.getIndex());
81        if (collection instanceof List) {
82          ((List) collection).set(i, value);
83        } else if (collection instanceof Object[]) {
84          ((Object[]) collection)[i] = value;
85        } else if (collection instanceof char[]) {
86          ((char[]) collection)[i] = (Character) value;
87        } else if (collection instanceof boolean[]) {
88          ((boolean[]) collection)[i] = (Boolean) value;
89        } else if (collection instanceof byte[]) {
90          ((byte[]) collection)[i] = (Byte) value;
91        } else if (collection instanceof double[]) {
92          ((double[]) collection)[i] = (Double) value;
93        } else if (collection instanceof float[]) {
94          ((float[]) collection)[i] = (Float) value;
95        } else if (collection instanceof int[]) {
96          ((int[]) collection)[i] = (Integer) value;
97        } else if (collection instanceof long[]) {
98          ((long[]) collection)[i] = (Long) value;
99        } else if (collection instanceof short[]) {
100         ((short[]) collection)[i] = (Short) value;
101       } else {
102         throw new ReflectionException("The '" + prop.getName() + "' property of " + collection + " is not a List or Array.");
103       }
104     }
105   }
106 
107 }