KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > accessplan > PropertyAccessPlan


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 JavaDoc;
22
23 /**
24  * Property access plan (for working with beans)
25  */

26 public class PropertyAccessPlan extends BaseAccessPlan {
27
28   protected static final Object JavaDoc[] NO_ARGUMENTS = new Object JavaDoc[0];
29
30   protected Method JavaDoc[] setters;
31   protected Method JavaDoc[] getters;
32
33   PropertyAccessPlan(Class JavaDoc clazz, String JavaDoc[] propertyNames) {
34     super(clazz, propertyNames);
35     setters = getSetters(propertyNames);
36     getters = getGetters(propertyNames);
37   }
38
39   public void setProperties(Object JavaDoc object, Object JavaDoc[] values) {
40     try {
41       Object JavaDoc[] arg = new Object JavaDoc[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 JavaDoc t) {
47           throw ClassInfo.unwrapThrowable(t);
48         }
49       }
50     } catch (Throwable JavaDoc t) {
51       throw new NestedRuntimeException("Error setting properties of '" + object + "'. Cause: " + t, t);
52     }
53   }
54
55   public Object JavaDoc[] getProperties(Object JavaDoc object) {
56     Object JavaDoc[] values = new Object JavaDoc[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 JavaDoc t) {
63           throw ClassInfo.unwrapThrowable(t);
64         }
65       }
66     } catch (Throwable JavaDoc t) {
67       throw new NestedRuntimeException("Error getting properties of '" + object + "'. Cause: " + t, t);
68     }
69     return values;
70   }
71
72 }
73
Popular Tags