KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > utils > BeanTools


1 package org.jahia.utils;
2
3 import java.lang.reflect.Method JavaDoc;
4 import java.lang.reflect.InvocationTargetException JavaDoc;
5
6 /**
7  * <p>Title: </p>
8  * <p>Description: </p>
9  * <p>Copyright: Copyright (c) 2002</p>
10  * <p>Company: Jahia Ltd</p>
11  * @author Serge Huber
12  * @version 1.0
13  */

14
15 public class BeanTools {
16
17     private static org.apache.log4j.Logger logger =
18         org.apache.log4j.Logger.getLogger(BeanTools.class);
19
20     private BeanTools () {
21     }
22
23     public static boolean setBeanProperty (Object JavaDoc beanObject,
24                                            String JavaDoc propertyName,
25                                            Class JavaDoc propertyType,
26                                            Object JavaDoc propertyValue) {
27         Class JavaDoc beanClass = beanObject.getClass();
28         String JavaDoc beanMethodName = "set" + propertyName.substring(0, 1).toUpperCase() +
29                        propertyName.substring(1);
30         Class JavaDoc[] beanPropertySetterParamTypes = new Class JavaDoc[1];
31         beanPropertySetterParamTypes[0] = propertyType;
32         try {
33             Method JavaDoc beanPropertySetter = beanClass.getMethod(beanMethodName, beanPropertySetterParamTypes);
34             Object JavaDoc[] args = new Object JavaDoc[1];
35             args[0] = propertyValue;
36             beanPropertySetter.invoke(beanObject, args);
37             return true;
38         } catch (NoSuchMethodException JavaDoc nsme) {
39             logger.error("Couldn't find setter method " + beanMethodName + " on object " + beanObject, nsme);
40             return false;
41         } catch (IllegalAccessException JavaDoc iae) {
42             logger.error("Error accessing setter method " + beanMethodName + " on object " + beanObject, iae);
43             return false;
44         } catch (InvocationTargetException JavaDoc ite) {
45             logger.error("Exception in setter method " + beanMethodName + " on object " + beanObject,
46                          ite.getTargetException());
47             return false;
48         }
49     }
50
51     public static boolean setBooleanProperty (Object JavaDoc beanObject,
52                                               String JavaDoc propertyName,
53                                               String JavaDoc propertyValue) {
54         return setBeanProperty(beanObject, propertyName, Boolean.TYPE,
55                                new Boolean JavaDoc(propertyValue));
56     }
57
58     public static boolean setIntProperty (Object JavaDoc beanObject,
59                                           String JavaDoc propertyName,
60                                           String JavaDoc propertyValue) {
61         return setBeanProperty(beanObject, propertyName, Integer.TYPE,
62                                new Integer JavaDoc(propertyValue));
63     }
64
65     public static boolean setLongProperty (Object JavaDoc beanObject,
66                                            String JavaDoc propertyName,
67                                            String JavaDoc propertyValue) {
68         return setBeanProperty(beanObject, propertyName, Long.TYPE,
69                                new Long JavaDoc(propertyValue));
70     }
71
72     public static boolean setStringProperty (Object JavaDoc beanObject,
73                                              String JavaDoc propertyName,
74                                              String JavaDoc propertyValue) {
75         return setBeanProperty(beanObject, propertyName, String JavaDoc.class,
76                                propertyValue);
77     }
78
79 }
Popular Tags