1 7 package com.inversoft.beans; 8 9 10 import java.lang.reflect.Method ; 11 import java.util.HashMap ; 12 import java.util.Map ; 13 14 import com.inversoft.util.ObjectTools; 15 import com.inversoft.util.typeconverter.TypeConversionException; 16 17 18 27 public class BeanProperty extends BaseBeanProperty { 28 29 32 static final boolean CACHING = true; 33 34 37 static Map cache = new HashMap (); 38 39 protected Method read; 40 protected Method write; 41 protected boolean collection; 42 43 44 48 59 public static BeanProperty getInstance(String propertyName, Class beanClass) 60 throws BeanException { 61 62 if (propertyName == null || propertyName.indexOf(".") != -1) { 63 throw new BeanException("Invalid propertyName: " + propertyName); 64 } 65 66 if (!CACHING) { 68 return new BeanProperty(propertyName, beanClass); 69 } 70 71 synchronized (cache) { 72 Map propMap = (Map ) cache.get(beanClass); 74 if (propMap == null) { 75 propMap = new HashMap (); 76 cache.put(beanClass, propMap); 77 } 78 79 BeanProperty bp = (BeanProperty) propMap.get(propertyName); 81 if (bp == null) { 82 bp = new SynchronizedBeanProperty(propertyName, beanClass); 83 propMap.put(propertyName, bp); 84 } else { 85 if (bp.getClass() != SynchronizedBeanProperty.class) { 86 throw new BeanException("Looking for non-indexed property " + 87 "but cached version is indexed"); 88 } 89 } 90 91 return bp; 92 } 93 } 94 95 96 97 104 protected BeanProperty() { 105 super(); 106 } 107 108 127 public BeanProperty(String propertyName, Class beanClass) 128 throws BeanException { 129 super(propertyName, beanClass); } 131 132 151 public BeanProperty(String propertyName, String beanClass) 152 throws BeanException { 153 super(propertyName, beanClass); } 155 156 162 protected void initialize() throws BeanException { 163 164 read = JavaBeanTools.findReadMethod(propertyName, beanClass); 165 propertyType = read.getReturnType(); 166 collection = ObjectTools.isCollection(propertyType); 167 168 try { 169 write = JavaBeanTools.findWriteMethod(propertyName, beanClass, propertyType); 170 } catch (BeanException be) { 171 } 173 } 174 175 178 public Class getBeanClass() { 179 return beanClass; 180 } 181 182 185 public Method getReadMethod() { 186 return read; 187 } 188 189 192 public Method getWriteMethod() { 193 return write; 194 } 195 196 205 public Object getPropertyValue(final Object bean) throws BeanException { 206 Object value = JavaBeanTools.callGetter(this, bean); 207 208 if (hasPropertyListeners()) { 209 firePropertyEvent(GET, value, value, bean, null); 210 } 211 212 return value; 213 } 214 215 222 public Object getPropertyValue(final Object bean, final Object key) 223 throws BeanException { 224 throw new BeanException("Operation not supported except in IndexedBeanProperty"); 225 } 226 227 239 public void setPropertyValue(final Object bean, Object value, 240 final boolean convert) 241 throws BeanException, TypeConversionException { 242 243 if (convert) { 245 value = convertParameter(value, bean, propertyType); 246 } 247 248 Object oldValue = null; 250 251 if (hasPropertyListeners()) { 252 oldValue = JavaBeanTools.callGetter(this, bean); 253 } 254 255 JavaBeanTools.callSetter(this, bean, value); 256 257 if (hasPropertyListeners()) { 258 firePropertyEvent(SET, oldValue, value, bean, null); 259 } 260 } 261 } | Popular Tags |