KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > shiftone > cache > config > BeanWrapper


1 package org.shiftone.cache.config;
2
3
4
5 import org.shiftone.cache.util.Log;
6
7 import java.lang.reflect.InvocationTargetException JavaDoc;
8 import java.lang.reflect.Method JavaDoc;
9 import java.util.HashMap JavaDoc;
10 import java.util.Map JavaDoc;
11
12
13 /**
14  * This class treats methods case-insensitive. This can cause a problem if
15  * their are two setters with the same name in different case. Don't do that.
16  * @version $Revision: 1.6 $
17  * @author <a HREF="mailto:jeff@shiftone.org">Jeff Drost</a>
18  */

19 public class BeanWrapper
20 {
21
22     private static final Log LOG = new Log(BeanWrapper.class);
23     private final Object JavaDoc object;
24     private final Class JavaDoc klass;
25     private Method JavaDoc[] methods;
26     private Map JavaDoc setters = new HashMap JavaDoc();
27
28     public BeanWrapper(Object JavaDoc object)
29     {
30
31         this.object = object;
32         this.klass = object.getClass();
33         this.methods = klass.getMethods();
34
35         for (int i = 0; i < methods.length; i++)
36         {
37             Method JavaDoc method = methods[i];
38             String JavaDoc name = method.getName().toLowerCase();
39
40             if ((name.startsWith("set")) //
41
&& (method.getParameterTypes().length == 1) //
42
&& (method.getReturnType().equals(Void.TYPE)))
43             {
44                 setters.put(name.substring(3), method);
45
46                 // LOG.info(name.substring(3) + " " + method.getReturnType());
47
}
48         }
49     }
50
51
52     public Class JavaDoc getWrappedObjectClass()
53     {
54         return klass;
55     }
56
57
58     public Object JavaDoc getWrappedObject()
59     {
60         return object;
61     }
62
63
64     public Method JavaDoc getSetter(String JavaDoc name) throws NoSuchMethodException JavaDoc
65     {
66
67         Method JavaDoc method = (Method JavaDoc) setters.get(name.toLowerCase());
68
69         if (method == null)
70         {
71             throw new NoSuchMethodException JavaDoc("no setter for : " + name);
72         }
73
74         return method;
75     }
76
77
78     public Class JavaDoc getType(String JavaDoc name) throws NoSuchMethodException JavaDoc
79     {
80         return getSetter(name).getParameterTypes()[0];
81     }
82
83
84     public void setProperty(String JavaDoc name, String JavaDoc value) throws NoSuchMethodException JavaDoc, IllegalAccessException JavaDoc, IllegalArgumentException JavaDoc, InvocationTargetException JavaDoc, ClassNotFoundException JavaDoc
85     {
86         setProperty(name, convert(value, getType(name)));
87     }
88
89
90     public void setProperty(String JavaDoc name, Object JavaDoc objectValue) throws NoSuchMethodException JavaDoc, IllegalAccessException JavaDoc, IllegalArgumentException JavaDoc, InvocationTargetException JavaDoc
91     {
92         LOG.debug(klass.getName() + " SET " + name + " -> " + objectValue);
93         getSetter(name).invoke(object, new Object JavaDoc[]{ objectValue });
94     }
95
96
97     private Object JavaDoc convert(String JavaDoc val, Class JavaDoc type) throws IllegalArgumentException JavaDoc, ClassNotFoundException JavaDoc
98     {
99
100         String JavaDoc v = val.trim();
101
102         if (val == null)
103         {
104             return null;
105         }
106         else if (Class JavaDoc.class.isAssignableFrom(type))
107         {
108             return Class.forName(val);
109         }
110         else if (String JavaDoc.class.isAssignableFrom(type))
111         {
112             return val;
113         }
114         else if (Integer.TYPE.isAssignableFrom(type) || Integer JavaDoc.class.isAssignableFrom(type))
115         {
116             return new Integer JavaDoc(v);
117         }
118         else if (Long.TYPE.isAssignableFrom(type) || Long JavaDoc.class.isAssignableFrom(type))
119         {
120             return new Long JavaDoc(v);
121         }
122         else if (Boolean.TYPE.isAssignableFrom(type) || Boolean JavaDoc.class.isAssignableFrom(type))
123         {
124             if ("true".equalsIgnoreCase(v))
125             {
126                 return Boolean.TRUE;
127             }
128             else if ("false".equalsIgnoreCase(v))
129             {
130                 return Boolean.FALSE;
131             }
132         }
133
134         throw new IllegalArgumentException JavaDoc("unable to convert '" + v + "' to '" + type.getName() + "' on class '" + klass.getName() + "'");
135     }
136 }
137
Popular Tags