01: /*
02: * @(#)BeanHelper.java 1.2 04/12/06
03: *
04: * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package pnuts.xml.action;
10:
11: import pnuts.beans.BeanUtil;
12:
13: class BeanHelper {
14: public static void setBeanProperty(Object bean,
15: String propertyName, String text) throws Exception {
16: Class cls = bean.getClass();
17: Object value;
18: Class type = BeanUtil.getPropertyType(cls, propertyName);
19: if (type == String.class) {
20: value = text;
21: } else if (type == int.class) {
22: value = Integer.valueOf(text);
23: } else if (type == long.class) {
24: value = Long.valueOf(text);
25: } else if (type == float.class) {
26: value = Float.valueOf(text);
27: } else if (type == double.class) {
28: value = Double.valueOf(text);
29: } else if (type == boolean.class) {
30: value = Boolean.valueOf(text);
31: } else if (type == short.class) {
32: value = Short.valueOf(text);
33: } else if (type == byte.class) {
34: value = Byte.valueOf(text);
35: } else {
36: throw new IllegalArgumentException("class=" + cls.getName()
37: + ", property=" + propertyName + ", text=" + text);
38: }
39: BeanUtil.setProperty(bean, propertyName, value);
40: }
41: }
|