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.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 = {};
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      }
41      return metaObject.getValue(prop.getName());
42    }
43  
44    protected Object getCollectionValue(PropertyTokenizer prop, Object collection) {
45      if (collection instanceof Map) {
46        return ((Map) collection).get(prop.getIndex());
47      }
48      int i = Integer.parseInt(prop.getIndex());
49      if (collection instanceof List) {
50        return ((List) collection).get(i);
51      } else if (collection instanceof Object[]) {
52        return ((Object[]) collection)[i];
53      } else if (collection instanceof char[]) {
54        return ((char[]) collection)[i];
55      } else if (collection instanceof boolean[]) {
56        return ((boolean[]) collection)[i];
57      } else if (collection instanceof byte[]) {
58        return ((byte[]) collection)[i];
59      } else if (collection instanceof double[]) {
60        return ((double[]) collection)[i];
61      } else if (collection instanceof float[]) {
62        return ((float[]) collection)[i];
63      } else if (collection instanceof int[]) {
64        return ((int[]) collection)[i];
65      } else if (collection instanceof long[]) {
66        return ((long[]) collection)[i];
67      } else if (collection instanceof short[]) {
68        return ((short[]) collection)[i];
69      } else {
70        throw new ReflectionException(
71            "The '" + prop.getName() + "' property of " + collection + " is not a List or Array.");
72      }
73    }
74  
75    protected void setCollectionValue(PropertyTokenizer prop, Object collection, Object value) {
76      if (collection instanceof Map) {
77        ((Map) collection).put(prop.getIndex(), value);
78      } else {
79        int i = Integer.parseInt(prop.getIndex());
80        if (collection instanceof List) {
81          ((List) collection).set(i, value);
82        } else if (collection instanceof Object[]) {
83          ((Object[]) collection)[i] = value;
84        } else if (collection instanceof char[]) {
85          ((char[]) collection)[i] = (Character) value;
86        } else if (collection instanceof boolean[]) {
87          ((boolean[]) collection)[i] = (Boolean) value;
88        } else if (collection instanceof byte[]) {
89          ((byte[]) collection)[i] = (Byte) value;
90        } else if (collection instanceof double[]) {
91          ((double[]) collection)[i] = (Double) value;
92        } else if (collection instanceof float[]) {
93          ((float[]) collection)[i] = (Float) value;
94        } else if (collection instanceof int[]) {
95          ((int[]) collection)[i] = (Integer) value;
96        } else if (collection instanceof long[]) {
97          ((long[]) collection)[i] = (Long) value;
98        } else if (collection instanceof short[]) {
99          ((short[]) collection)[i] = (Short) value;
100       } else {
101         throw new ReflectionException(
102             "The '" + prop.getName() + "' property of " + collection + " is not a List or Array.");
103       }
104     }
105   }
106 
107 }