1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.morph.reflect.reflectors;
17
18 import net.sf.morph.context.Context;
19 import net.sf.morph.context.contexts.MapContext;
20 import net.sf.morph.reflect.InstantiatingReflector;
21 import net.sf.morph.util.ClassUtils;
22
23 /**
24 * A reflector that can expose the properties of any Context.
25 *
26 * @author Matt Sgarlata
27 * @since Nov 28, 2004
28 */
29 public class ContextReflector extends BaseBeanReflector implements InstantiatingReflector {
30
31 private static final Class[] REFLECTABLE_TYPES = new Class[] {
32 Context.class
33 };
34
35 private Context getContext(Object bean) {
36 return (Context) bean;
37 }
38
39
40
41
42
43 public Class[] getReflectableClassesImpl() {
44 return REFLECTABLE_TYPES;
45 }
46
47 protected String[] getPropertyNamesImpl(Object bean) throws Exception {
48
49
50
51
52 return getContext(bean).getPropertyNames();
53
54 }
55
56 protected Class getTypeImpl(Object bean, String propertyName)
57 throws Exception {
58
59
60
61
62 return ClassUtils.getClass(getContext(bean).get(propertyName));
63
64 }
65
66 /**
67 * Returns <code>true</code> because all properties of a context are
68 * considered readable. If the property isn't a defined property returned by
69 * getPropertyNames, <code>null</code> is returned when the
70 * {@link net.sf.morph.reflect.BeanReflector#get(Object, String)} method is
71 * called.
72 */
73 protected boolean isReadableImpl(Object bean, String propertyName)
74 throws Exception {
75 return true;
76 }
77
78 /**
79 * Returns <code>true</code> because all properties in a context are
80 * assumed to be writeable. If they're not (e.g. - on an Object has been
81 * exposed as a Context), an exception is thrown when the set method is
82 * called on the context.
83 */
84 protected boolean isWriteableImpl(Object bean, String propertyName)
85 throws Exception {
86 return true;
87 }
88
89 protected Object getImpl(Object bean, String propertyName) throws Exception {
90 return getContext(bean).get(propertyName);
91 }
92
93 protected void setImpl(Object bean, String propertyName, Object value)
94 throws Exception {
95 getContext(bean).set(propertyName, value);
96 }
97
98 protected Object newInstanceImpl(Class clazz, Object parameters) throws Exception {
99 return clazz == Context.class ? new MapContext() : super.newInstanceImpl(clazz, parameters);
100 }
101 }