1 24 package org.riotfamily.common.beans; 25 26 import java.beans.IntrospectionException ; 27 import java.beans.PropertyDescriptor ; 28 import java.lang.reflect.Modifier ; 29 30 import org.springframework.beans.BeanUtils; 31 import org.springframework.beans.BeanWrapperImpl; 32 import org.springframework.beans.BeansException; 33 34 40 public class ProtectedBeanWrapper extends BeanWrapperImpl 41 implements ObjectWrapper { 42 43 private Class objectClass; 44 45 public ProtectedBeanWrapper() { 46 super(); 47 } 48 49 public ProtectedBeanWrapper(Class 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 object) { 57 super(object); 58 } 59 60 public void setObject(Object object) { 61 super.setWrappedInstance(object); 62 this.objectClass = object.getClass(); 63 } 64 65 public Object getObject() { 66 return getWrappedInstance(); 67 } 68 69 public Class getObjectClass() { 70 return this.objectClass; 71 } 72 73 protected PropertyDescriptor getPropertyDescriptorInternal(String name) 74 throws BeansException { 75 76 try { 77 PropertyDescriptor pd = super.getPropertyDescriptorInternal(name); 78 if (pd == null) { 79 pd = new PropertyDescriptor (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 e) { 96 return null; 97 } 98 } 99 100 } 101 | Popular Tags |