01: /*
02: * Copyright 2004 Clinton Begin
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.sqlmap.engine.accessplan;
17:
18: import com.ibatis.common.beans.ClassInfo;
19:
20: import java.lang.reflect.Method;
21:
22: /**
23: * Property access plan (for working with beans)
24: */
25: public class PropertyAccessPlan extends BaseAccessPlan {
26:
27: protected static final Object[] NO_ARGUMENTS = new Object[0];
28:
29: protected Method[] setters;
30: protected Method[] getters;
31:
32: PropertyAccessPlan(Class clazz, String[] propertyNames) {
33: super (clazz, propertyNames);
34: setters = getSetters(propertyNames);
35: getters = getGetters(propertyNames);
36: }
37:
38: public void setProperties(Object object, Object[] values) {
39: int i = 0;
40: try {
41: Object[] arg = new Object[1];
42: for (i = 0; i < propertyNames.length; i++) {
43: arg[0] = values[i];
44: try {
45: setters[i].invoke(object, arg);
46: } catch (Throwable t) {
47: throw ClassInfo.unwrapThrowable(t);
48: }
49: }
50: } catch (Throwable t) {
51: throw new RuntimeException("Error setting property '"
52: + setters[i].getName() + "' of '" + object
53: + "'. Cause: " + t, t);
54: }
55: }
56:
57: public Object[] getProperties(Object object) {
58: int i = 0;
59: Object[] values = new Object[propertyNames.length];
60: try {
61: for (i = 0; i < propertyNames.length; i++) {
62: try {
63: values[i] = getters[i].invoke(object, NO_ARGUMENTS);
64: } catch (Throwable t) {
65: throw ClassInfo.unwrapThrowable(t);
66: }
67: }
68: } catch (Throwable t) {
69: throw new RuntimeException("Error getting property '"
70: + getters[i].getName() + "' of '" + object
71: + "'. Cause: " + t, t);
72: }
73: return values;
74: }
75:
76: }
|