KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > beans > ProtectedBeanWrapper


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.beans;
25
26 import java.beans.IntrospectionException JavaDoc;
27 import java.beans.PropertyDescriptor JavaDoc;
28 import java.lang.reflect.Modifier JavaDoc;
29
30 import org.springframework.beans.BeanUtils;
31 import org.springframework.beans.BeanWrapperImpl;
32 import org.springframework.beans.BeansException;
33
34 /**
35  * BeanWrapper that that provides access to non-public setters and getters.
36  *
37  * @author Felix Gnass [fgnass at neteye dot de]
38  * @since 6.4
39  */

40 public class ProtectedBeanWrapper extends BeanWrapperImpl
41         implements ObjectWrapper {
42
43     private Class JavaDoc objectClass;
44
45     public ProtectedBeanWrapper() {
46         super();
47     }
48
49     public ProtectedBeanWrapper(Class JavaDoc clazz) {
50         this.objectClass = clazz;
51         if (!clazz.isInterface() && !Modifier.isAbstract(clazz.getModifiers())) {
52             setObject(BeanUtils.instantiateClass(clazz));
53         }
54     }
55
56     public ProtectedBeanWrapper(Object JavaDoc object) {
57         super(object);
58     }
59
60     public void setObject(Object JavaDoc object) {
61         super.setWrappedInstance(object);
62         this.objectClass = object.getClass();
63     }
64
65     public Object JavaDoc getObject() {
66         return getWrappedInstance();
67     }
68
69     public Class JavaDoc getObjectClass() {
70         return this.objectClass;
71     }
72
73     protected PropertyDescriptor JavaDoc getPropertyDescriptorInternal(String JavaDoc name)
74             throws BeansException {
75
76         try {
77             PropertyDescriptor JavaDoc pd = super.getPropertyDescriptorInternal(name);
78             if (pd == null) {
79                 pd = new PropertyDescriptor JavaDoc(name,
80                         PropertyUtils.findReadMethod(getWrappedClass(), name),
81                         PropertyUtils.findWriteMethod(getWrappedClass(), name));
82             }
83             else {
84                 if (pd.getReadMethod() == null) {
85                     pd.setReadMethod(PropertyUtils.findReadMethod(
86                             getWrappedClass(), name));
87                 }
88                 if (pd.getWriteMethod() == null) {
89                     pd.setWriteMethod(PropertyUtils.findWriteMethod(
90                             getWrappedClass(), name));
91                 }
92             }
93             return pd;
94         }
95         catch (IntrospectionException JavaDoc e) {
96             return null;
97         }
98     }
99
100 }
101
Popular Tags