View Javadoc
1   /*
2    * Copyright 2004-2022 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 com.ibatis.common.beans;
17  
18  import java.util.List;
19  
20  /**
21   * Abstract class used to help development of Probe implementations.
22   */
23  public abstract class BaseProbe implements Probe {
24  
25    /**
26     * Sets the property.
27     *
28     * @param object
29     *          the object
30     * @param property
31     *          the property
32     * @param value
33     *          the value
34     */
35    protected abstract void setProperty(Object object, String property, Object value);
36  
37    /**
38     * Gets the property.
39     *
40     * @param object
41     *          the object
42     * @param property
43     *          the property
44     *
45     * @return the property
46     */
47    protected abstract Object getProperty(Object object, String property);
48  
49    /**
50     * Returns an array of the readable properties exposed by an object.
51     *
52     * @param object
53     *          - the object
54     *
55     * @return The array of property names
56     */
57    public abstract String[] getReadablePropertyNames(Object object);
58  
59    /**
60     * Returns an array of the writeable properties exposed by an object.
61     *
62     * @param object
63     *          - the object
64     *
65     * @return The array of property names
66     */
67    public abstract String[] getWriteablePropertyNames(Object object);
68  
69    /**
70     * Gets the indexed property.
71     *
72     * @param object
73     *          the object
74     * @param indexedName
75     *          the indexed name
76     *
77     * @return the indexed property
78     */
79    protected Object getIndexedProperty(Object object, String indexedName) {
80  
81      Object value = null;
82  
83      try {
84        String name = indexedName.substring(0, indexedName.indexOf('['));
85        int i = Integer.parseInt(indexedName.substring(indexedName.indexOf('[') + 1, indexedName.indexOf(']')));
86        Object list = null;
87        if ("".equals(name)) {
88          list = object;
89        } else {
90          list = getProperty(object, name);
91        }
92  
93        if (list instanceof List) {
94          value = ((List) list).get(i);
95        } else if (list instanceof Object[]) {
96          value = ((Object[]) list)[i];
97        } else if (list instanceof char[]) {
98          value = Character.valueOf(((char[]) list)[i]);
99        } else if (list instanceof boolean[]) {
100         value = new Boolean(((boolean[]) list)[i]);
101       } else if (list instanceof byte[]) {
102         value = Byte.valueOf(((byte[]) list)[i]);
103       } else if (list instanceof double[]) {
104         value = Double.valueOf(((double[]) list)[i]);
105       } else if (list instanceof float[]) {
106         value = Float.valueOf(((float[]) list)[i]);
107       } else if (list instanceof int[]) {
108         value = Integer.valueOf(((int[]) list)[i]);
109       } else if (list instanceof long[]) {
110         value = Long.valueOf(((long[]) list)[i]);
111       } else if (list instanceof short[]) {
112         value = Short.valueOf(((short[]) list)[i]);
113       } else {
114         throw new ProbeException(
115             "The '" + name + "' property of the " + object.getClass().getName() + " class is not a List or Array.");
116       }
117 
118     } catch (ProbeException e) {
119       throw e;
120     } catch (Exception e) {
121       throw new ProbeException("Error getting ordinal list from JavaBean. Cause " + e, e);
122     }
123 
124     return value;
125   }
126 
127   /**
128    * Gets the indexed type.
129    *
130    * @param object
131    *          the object
132    * @param indexedName
133    *          the indexed name
134    *
135    * @return the indexed type
136    */
137   protected Class getIndexedType(Object object, String indexedName) {
138 
139     Class value = null;
140 
141     try {
142       String name = indexedName.substring(0, indexedName.indexOf('['));
143       int i = Integer.parseInt(indexedName.substring(indexedName.indexOf('[') + 1, indexedName.indexOf(']')));
144       Object list = null;
145       if (!"".equals(name)) {
146         list = getProperty(object, name);
147       } else {
148         list = object;
149       }
150 
151       if (list instanceof List) {
152         value = ((List) list).get(i).getClass();
153       } else if (list instanceof Object[]) {
154         value = ((Object[]) list)[i].getClass();
155       } else if (list instanceof char[]) {
156         value = Character.class;
157       } else if (list instanceof boolean[]) {
158         value = Boolean.class;
159       } else if (list instanceof byte[]) {
160         value = Byte.class;
161       } else if (list instanceof double[]) {
162         value = Double.class;
163       } else if (list instanceof float[]) {
164         value = Float.class;
165       } else if (list instanceof int[]) {
166         value = Integer.class;
167       } else if (list instanceof long[]) {
168         value = Long.class;
169       } else if (list instanceof short[]) {
170         value = Short.class;
171       } else {
172         throw new ProbeException(
173             "The '" + name + "' property of the " + object.getClass().getName() + " class is not a List or Array.");
174       }
175 
176     } catch (ProbeException e) {
177       throw e;
178     } catch (Exception e) {
179       throw new ProbeException("Error getting ordinal list from JavaBean. Cause " + e, e);
180     }
181 
182     return value;
183   }
184 
185   /**
186    * Sets the indexed property.
187    *
188    * @param object
189    *          the object
190    * @param indexedName
191    *          the indexed name
192    * @param value
193    *          the value
194    */
195   protected void setIndexedProperty(Object object, String indexedName, Object value) {
196 
197     try {
198       String name = indexedName.substring(0, indexedName.indexOf('['));
199       int i = Integer.parseInt(indexedName.substring(indexedName.indexOf('[') + 1, indexedName.indexOf(']')));
200       Object list = getProperty(object, name);
201       if (list instanceof List) {
202         ((List) list).set(i, value);
203       } else if (list instanceof Object[]) {
204         ((Object[]) list)[i] = value;
205       } else if (list instanceof char[]) {
206         ((char[]) list)[i] = ((Character) value).charValue();
207       } else if (list instanceof boolean[]) {
208         ((boolean[]) list)[i] = ((Boolean) value).booleanValue();
209       } else if (list instanceof byte[]) {
210         ((byte[]) list)[i] = ((Byte) value).byteValue();
211       } else if (list instanceof double[]) {
212         ((double[]) list)[i] = ((Double) value).doubleValue();
213       } else if (list instanceof float[]) {
214         ((float[]) list)[i] = ((Float) value).floatValue();
215       } else if (list instanceof int[]) {
216         ((int[]) list)[i] = ((Integer) value).intValue();
217       } else if (list instanceof long[]) {
218         ((long[]) list)[i] = ((Long) value).longValue();
219       } else if (list instanceof short[]) {
220         ((short[]) list)[i] = ((Short) value).shortValue();
221       } else {
222         throw new ProbeException(
223             "The '" + name + "' property of the " + object.getClass().getName() + " class is not a List or Array.");
224       }
225     } catch (ProbeException e) {
226       throw e;
227     } catch (Exception e) {
228       throw new ProbeException("Error getting ordinal value from JavaBean. Cause " + e, e);
229     }
230   }
231 }