001: /*
002: * Copyright 2004 Clinton Begin
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package com.ibatis.common.beans;
017:
018: import java.util.List;
019:
020: /**
021: * Abstract class used to help development of Probe implementations
022: */
023: public abstract class BaseProbe implements Probe {
024:
025: protected abstract void setProperty(Object object, String property,
026: Object value);
027:
028: protected abstract Object getProperty(Object object, String property);
029:
030: /**
031: * Returns an array of the readable properties exposed by an object
032: *
033: * @param object - the object
034: * @return The array of property names
035: */
036: public abstract String[] getReadablePropertyNames(Object object);
037:
038: /**
039: * Returns an array of the writeable properties exposed by an object
040: *
041: * @param object - the object
042: * @return The array of property names
043: */
044: public abstract String[] getWriteablePropertyNames(Object object);
045:
046: protected Object getIndexedProperty(Object object,
047: String indexedName) {
048:
049: Object value = null;
050:
051: try {
052: String name = indexedName.substring(0, indexedName
053: .indexOf('['));
054: int i = Integer.parseInt(indexedName.substring(indexedName
055: .indexOf('[') + 1, indexedName.indexOf(']')));
056: Object list = null;
057: if ("".equals(name)) {
058: list = object;
059: } else {
060: list = getProperty(object, name);
061: }
062:
063: if (list instanceof List) {
064: value = ((List) list).get(i);
065: } else if (list instanceof Object[]) {
066: value = ((Object[]) list)[i];
067: } else if (list instanceof char[]) {
068: value = new Character(((char[]) list)[i]);
069: } else if (list instanceof boolean[]) {
070: value = new Boolean(((boolean[]) list)[i]);
071: } else if (list instanceof byte[]) {
072: value = new Byte(((byte[]) list)[i]);
073: } else if (list instanceof double[]) {
074: value = new Double(((double[]) list)[i]);
075: } else if (list instanceof float[]) {
076: value = new Float(((float[]) list)[i]);
077: } else if (list instanceof int[]) {
078: value = new Integer(((int[]) list)[i]);
079: } else if (list instanceof long[]) {
080: value = new Long(((long[]) list)[i]);
081: } else if (list instanceof short[]) {
082: value = new Short(((short[]) list)[i]);
083: } else {
084: throw new ProbeException("The '" + name
085: + "' property of the "
086: + object.getClass().getName()
087: + " class is not a List or Array.");
088: }
089:
090: } catch (ProbeException e) {
091: throw e;
092: } catch (Exception e) {
093: throw new ProbeException(
094: "Error getting ordinal list from JavaBean. Cause "
095: + e, e);
096: }
097:
098: return value;
099: }
100:
101: protected Class getIndexedType(Object object, String indexedName) {
102:
103: Class value = null;
104:
105: try {
106: String name = indexedName.substring(0, indexedName
107: .indexOf('['));
108: int i = Integer.parseInt(indexedName.substring(indexedName
109: .indexOf('[') + 1, indexedName.indexOf(']')));
110: Object list = null;
111: if (!"".equals(name)) {
112: list = getProperty(object, name);
113: } else {
114: list = object;
115: }
116:
117: if (list instanceof List) {
118: value = ((List) list).get(i).getClass();
119: } else if (list instanceof Object[]) {
120: value = ((Object[]) list)[i].getClass();
121: } else if (list instanceof char[]) {
122: value = Character.class;
123: } else if (list instanceof boolean[]) {
124: value = Boolean.class;
125: } else if (list instanceof byte[]) {
126: value = Byte.class;
127: } else if (list instanceof double[]) {
128: value = Double.class;
129: } else if (list instanceof float[]) {
130: value = Float.class;
131: } else if (list instanceof int[]) {
132: value = Integer.class;
133: } else if (list instanceof long[]) {
134: value = Long.class;
135: } else if (list instanceof short[]) {
136: value = Short.class;
137: } else {
138: throw new ProbeException("The '" + name
139: + "' property of the "
140: + object.getClass().getName()
141: + " class is not a List or Array.");
142: }
143:
144: } catch (ProbeException e) {
145: throw e;
146: } catch (Exception e) {
147: throw new ProbeException(
148: "Error getting ordinal list from JavaBean. Cause "
149: + e, e);
150: }
151:
152: return value;
153: }
154:
155: protected void setIndexedProperty(Object object,
156: String indexedName, Object value) {
157:
158: try {
159: String name = indexedName.substring(0, indexedName
160: .indexOf('['));
161: int i = Integer.parseInt(indexedName.substring(indexedName
162: .indexOf('[') + 1, indexedName.indexOf(']')));
163: Object list = getProperty(object, name);
164: if (list instanceof List) {
165: ((List) list).set(i, value);
166: } else if (list instanceof Object[]) {
167: ((Object[]) list)[i] = value;
168: } else if (list instanceof char[]) {
169: ((char[]) list)[i] = ((Character) value).charValue();
170: } else if (list instanceof boolean[]) {
171: ((boolean[]) list)[i] = ((Boolean) value)
172: .booleanValue();
173: } else if (list instanceof byte[]) {
174: ((byte[]) list)[i] = ((Byte) value).byteValue();
175: } else if (list instanceof double[]) {
176: ((double[]) list)[i] = ((Double) value).doubleValue();
177: } else if (list instanceof float[]) {
178: ((float[]) list)[i] = ((Float) value).floatValue();
179: } else if (list instanceof int[]) {
180: ((int[]) list)[i] = ((Integer) value).intValue();
181: } else if (list instanceof long[]) {
182: ((long[]) list)[i] = ((Long) value).longValue();
183: } else if (list instanceof short[]) {
184: ((short[]) list)[i] = ((Short) value).shortValue();
185: } else {
186: throw new ProbeException("The '" + name
187: + "' property of the "
188: + object.getClass().getName()
189: + " class is not a List or Array.");
190: }
191: } catch (ProbeException e) {
192: throw e;
193: } catch (Exception e) {
194: throw new ProbeException(
195: "Error getting ordinal value from JavaBean. Cause "
196: + e, e);
197: }
198: }
199: }
|