KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > util > Bean


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 /*
22  * Created on Mar 20, 2005
23  */

24 package org.lobobrowser.util;
25
26 //import java.util.logging.*;
27

28 import java.beans.*;
29 import java.lang.reflect.Method JavaDoc;
30 import java.util.*;
31
32 /**
33  * @author J. H. S.
34  */

35 public class Bean {
36     //private static final java.util.logging.Logger logger = Logger.getLogger(Bean.class);
37
private final Class JavaDoc clazz;
38
39     public Bean(Class JavaDoc clazz) {
40         this.clazz = clazz;
41     }
42     
43     private Map propertyDescriptors = null;
44     
45     private void populateDescriptors(Map map, Class JavaDoc clazz) throws IntrospectionException {
46         BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
47         PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
48         for(int i = 0; i < pds.length; i++) {
49             map.put(pds[i].getName(), pds[i]);
50         }
51         if(clazz.isInterface()) {
52             java.lang.reflect.Type JavaDoc[] interfaces = clazz.getGenericInterfaces();
53             for(int i = 0; i < interfaces.length; i++) {
54                 this.populateDescriptors(map, (Class JavaDoc) interfaces[i]);
55             }
56         }
57     }
58     
59     public PropertyDescriptor getPropertyDescriptor(String JavaDoc propertyName) throws IntrospectionException {
60         synchronized(this) {
61             if(this.propertyDescriptors == null) {
62                 this.propertyDescriptors = new HashMap();
63                 this.populateDescriptors(this.propertyDescriptors, this.clazz);
64             }
65             return (PropertyDescriptor) this.propertyDescriptors.get(propertyName);
66         }
67     }
68     
69     public Map getPropertyDescriptorsMap() throws IntrospectionException {
70         synchronized(this) {
71             if(this.propertyDescriptors == null) {
72                 this.propertyDescriptors = new HashMap();
73                 this.populateDescriptors(this.propertyDescriptors, this.clazz);
74             }
75             return this.propertyDescriptors;
76         }
77     }
78     
79     public PropertyDescriptor[] getPropertyDescriptors() throws IntrospectionException {
80         synchronized(this) {
81             return (PropertyDescriptor[]) this.getPropertyDescriptorsMap().values().toArray(new PropertyDescriptor[0]);
82         }
83     }
84     
85     public void setPropertyForFQN(Object JavaDoc receiver, String JavaDoc fullyQualifiedPropertyName, Object JavaDoc value) throws Exception JavaDoc {
86         int idx = fullyQualifiedPropertyName.indexOf('.');
87         if(idx == -1) {
88             PropertyDescriptor pd = this.getPropertyDescriptor(fullyQualifiedPropertyName);
89             if(pd == null) {
90                 throw new IllegalStateException JavaDoc("Property '" + fullyQualifiedPropertyName + "' unknown");
91             }
92             Method JavaDoc method = pd.getWriteMethod();
93             if(method == null) {
94                 throw new IllegalStateException JavaDoc("Property '" + fullyQualifiedPropertyName + "' not settable");
95             }
96             Object JavaDoc actualValue = convertValue(value, pd.getPropertyType());
97             method.invoke(receiver, new Object JavaDoc[] { actualValue });
98         }
99         else {
100             String JavaDoc prefix = fullyQualifiedPropertyName.substring(0, idx);
101             PropertyDescriptor pinfo = this.getPropertyDescriptor(prefix);
102             if(pinfo == null) {
103                 throw new IllegalStateException JavaDoc("Property '" + prefix + "' unknown");
104             }
105             Method JavaDoc readMethod = pinfo.getReadMethod();
106             if(readMethod == null) {
107                 throw new IllegalStateException JavaDoc("Property '" + prefix + "' not readable");
108             }
109             Object JavaDoc newReceiver = readMethod.invoke(receiver, new Object JavaDoc[0]);
110             //Class newClass = pinfo.getPropertyType();
111
String JavaDoc nameRest = fullyQualifiedPropertyName.substring(idx + 1);
112             this.setPropertyForFQN(newReceiver, nameRest, value);
113         }
114     }
115     
116     private static Object JavaDoc convertValue(Object JavaDoc value, Class JavaDoc targetType) {
117         boolean targetString = targetType.isAssignableFrom(String JavaDoc.class);
118         if(value instanceof String JavaDoc && targetString) {
119             // ignore
120
}
121         else if(targetString) {
122             value = String.valueOf(value);
123         }
124         else if(!(value instanceof Byte JavaDoc) && (targetType == Byte JavaDoc.class || targetType == byte.class)) {
125             value = Byte.valueOf(String.valueOf(value));
126         }
127         else if(!(value instanceof Boolean JavaDoc) && (targetType == Boolean JavaDoc.class || targetType == boolean.class)) {
128             value = Boolean.valueOf(String.valueOf(value));
129         }
130         else if(!(value instanceof Short JavaDoc) && (targetType == Short JavaDoc.class || targetType == short.class)) {
131             value = Short.valueOf(String.valueOf(value));
132         }
133         else if(!(value instanceof Integer JavaDoc) && (targetType == Integer JavaDoc.class || targetType == int.class)) {
134             value = Integer.valueOf(String.valueOf(value));
135         }
136         else if(!(value instanceof Long JavaDoc) && (targetType == Long JavaDoc.class || targetType == long.class)) {
137             value = Long.valueOf(String.valueOf(value));
138         }
139         return value;
140     }
141
142 }
143
Popular Tags