View Javadoc

1   /*
2    * Copyright 2004-2005 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
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  //	private ReflectorContext getReflectorContext(Object bean) {
40  //		return (ReflectorContext) bean;
41  //	}
42  	
43  	public Class[] getReflectableClassesImpl() {
44  		return REFLECTABLE_TYPES;
45  	}
46  	
47  	protected String[] getPropertyNamesImpl(Object bean) throws Exception {
48  //		if (bean instanceof ReflectorContext) {
49  //			return getReflectorContext(bean).getBeanReflector().getPropertyNames(bean);
50  //		}
51  //		else {
52  			return getContext(bean).getPropertyNames();
53  //		}
54  	}
55  
56  	protected Class getTypeImpl(Object bean, String propertyName)
57  		throws Exception {
58  //		if (bean instanceof ReflectorContext) {
59  //			return getReflectorContext(bean).getBeanReflector().getType(bean, propertyName);
60  //		}
61  //		else {
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 }