1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.shale.test.mock; |
19 | |
|
20 | |
import java.beans.BeanInfo; |
21 | |
import java.beans.IntrospectionException; |
22 | |
import java.beans.Introspector; |
23 | |
import java.beans.PropertyDescriptor; |
24 | |
import java.lang.reflect.InvocationTargetException; |
25 | |
import java.util.Map; |
26 | |
import javax.faces.el.EvaluationException; |
27 | |
import javax.faces.el.PropertyNotFoundException; |
28 | |
import javax.faces.el.PropertyResolver; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
public class MockPropertyResolver extends PropertyResolver { |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 18 | public MockPropertyResolver() { |
46 | 18 | } |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
public Object getValue(Object base, Object property) |
60 | |
throws EvaluationException, PropertyNotFoundException { |
61 | |
|
62 | 10 | if (base == null) { |
63 | |
throw new NullPointerException(); |
64 | |
} |
65 | 10 | if (base instanceof Map) { |
66 | 9 | return ((Map) base).get(property); |
67 | |
} |
68 | 1 | String name = property.toString(); |
69 | 1 | PropertyDescriptor descriptor = descriptor(base.getClass(), name); |
70 | |
try { |
71 | 1 | return descriptor.getReadMethod().invoke(base, (Object[]) null); |
72 | |
} catch (IllegalAccessException e) { |
73 | |
throw new EvaluationException(e); |
74 | |
} catch (InvocationTargetException e) { |
75 | |
throw new EvaluationException(e.getTargetException()); |
76 | |
} |
77 | |
|
78 | |
} |
79 | |
|
80 | |
|
81 | |
|
82 | |
public Object getValue(Object base, int index) |
83 | |
throws PropertyNotFoundException { |
84 | |
|
85 | |
return getValue(base, "" + index); |
86 | |
|
87 | |
} |
88 | |
|
89 | |
|
90 | |
|
91 | |
public void setValue(Object base, Object property, Object value) |
92 | |
throws PropertyNotFoundException { |
93 | |
|
94 | 3 | if (base == null) { |
95 | |
throw new NullPointerException(); |
96 | |
} |
97 | 3 | if (base instanceof Map) { |
98 | 3 | ((Map) base).put(property, value); |
99 | 3 | return; |
100 | |
} |
101 | |
String name = property.toString(); |
102 | |
PropertyDescriptor descriptor = descriptor(base.getClass(), name); |
103 | |
try { |
104 | |
descriptor.getWriteMethod().invoke(base, new Object[] { value }); |
105 | |
} catch (IllegalAccessException e) { |
106 | |
throw new EvaluationException(e); |
107 | |
} catch (InvocationTargetException e) { |
108 | |
throw new EvaluationException(e.getTargetException()); |
109 | |
} |
110 | |
|
111 | |
} |
112 | |
|
113 | |
|
114 | |
|
115 | |
public void setValue(Object base, int index, Object value) |
116 | |
throws PropertyNotFoundException { |
117 | |
|
118 | |
setValue(base, "" + index, value); |
119 | |
|
120 | |
} |
121 | |
|
122 | |
|
123 | |
|
124 | |
public boolean isReadOnly(Object base, Object property) |
125 | |
throws PropertyNotFoundException { |
126 | |
|
127 | |
if (base == null) { |
128 | |
throw new NullPointerException(); |
129 | |
} |
130 | |
if (base instanceof Map) { |
131 | |
return false; |
132 | |
} |
133 | |
String name = property.toString(); |
134 | |
PropertyDescriptor descriptor = descriptor(base.getClass(), name); |
135 | |
return (descriptor.getWriteMethod() == null); |
136 | |
|
137 | |
} |
138 | |
|
139 | |
|
140 | |
|
141 | |
public boolean isReadOnly(Object base, int index) |
142 | |
throws PropertyNotFoundException { |
143 | |
|
144 | |
return isReadOnly(base, "" + index); |
145 | |
|
146 | |
} |
147 | |
|
148 | |
|
149 | |
|
150 | |
public Class getType(Object base, Object property) |
151 | |
throws PropertyNotFoundException { |
152 | |
|
153 | |
if (base == null) { |
154 | |
throw new NullPointerException(); |
155 | |
} |
156 | |
if (base instanceof Map) { |
157 | |
Object value = ((Map) base).get(property); |
158 | |
if (value != null) { |
159 | |
return value.getClass(); |
160 | |
} else { |
161 | |
return Object.class; |
162 | |
} |
163 | |
} |
164 | |
String name = property.toString(); |
165 | |
PropertyDescriptor descriptor = descriptor(base.getClass(), name); |
166 | |
return descriptor.getPropertyType(); |
167 | |
|
168 | |
} |
169 | |
|
170 | |
|
171 | |
|
172 | |
public Class getType(Object base, int index) |
173 | |
throws PropertyNotFoundException { |
174 | |
|
175 | |
return getType(base, "" + index); |
176 | |
|
177 | |
} |
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
private PropertyDescriptor descriptor(Class clazz, String name) { |
196 | |
|
197 | 1 | System.err.println("descriptor(class=" + clazz.getName() + ", name=" + name); |
198 | 1 | BeanInfo info = null; |
199 | |
try { |
200 | 1 | info = Introspector.getBeanInfo(clazz); |
201 | 1 | System.err.println(" Found BeanInfo " + info); |
202 | |
} catch (IntrospectionException e) { |
203 | |
throw new EvaluationException(e); |
204 | 1 | } |
205 | 1 | PropertyDescriptor[] descriptors = info.getPropertyDescriptors(); |
206 | 2 | for (int i = 0; i < descriptors.length; i++) { |
207 | 2 | if (name.equals(descriptors[i].getName())) { |
208 | 1 | System.err.print(" Found PropertyDescriptor " + descriptors[i]); |
209 | 1 | return descriptors[i]; |
210 | |
} |
211 | |
} |
212 | |
System.err.print(" No property descriptor for property " + name); |
213 | |
throw new PropertyNotFoundException(name); |
214 | |
|
215 | |
} |
216 | |
|
217 | |
|
218 | |
} |