Source code: com/ibatis/sqlmap/engine/accessplan/PropertyAccessPlan.java
1 /*
2 * Copyright 2004 Clinton Begin
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 com.ibatis.sqlmap.engine.accessplan;
17
18 import com.ibatis.common.beans.ClassInfo;
19 import com.ibatis.common.exception.NestedRuntimeException;
20
21 import java.lang.reflect.Method;
22
23 /**
24 * Property access plan (for working with beans)
25 */
26 public class PropertyAccessPlan extends BaseAccessPlan {
27
28 protected static final Object[] NO_ARGUMENTS = new Object[0];
29
30 protected Method[] setters;
31 protected Method[] getters;
32
33 PropertyAccessPlan(Class clazz, String[] propertyNames) {
34 super(clazz, propertyNames);
35 setters = getSetters(propertyNames);
36 getters = getGetters(propertyNames);
37 }
38
39 public void setProperties(Object object, Object[] values) {
40 try {
41 Object[] arg = new Object[1];
42 for (int 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 NestedRuntimeException("Error setting properties of '" + object + "'. Cause: " + t, t);
52 }
53 }
54
55 public Object[] getProperties(Object object) {
56 Object[] values = new Object[propertyNames.length];
57 try {
58 //Object[] arg = new Object[1];
59 for (int i = 0; i < propertyNames.length; i++) {
60 try {
61 values[i] = getters[i].invoke(object, NO_ARGUMENTS);
62 } catch (Throwable t) {
63 throw ClassInfo.unwrapThrowable(t);
64 }
65 }
66 } catch (Throwable t) {
67 throw new NestedRuntimeException("Error getting properties of '" + object + "'. Cause: " + t, t);
68 }
69 return values;
70 }
71
72 }