1 18 package org.apache.beehive.netui.util.internal.cache; 19 20 import java.beans.Introspector ; 21 import java.beans.IntrospectionException ; 22 import java.beans.PropertyDescriptor ; 23 import java.lang.reflect.Method ; 24 import java.lang.reflect.Modifier ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 28 import org.apache.beehive.netui.util.internal.concurrent.InternalConcurrentHashMap; 29 import org.apache.beehive.netui.util.logging.Logger; 30 31 39 public final class PropertyCache { 40 41 private static final Logger LOGGER = Logger.getInstance(PropertyCache.class); 42 43 private final InternalConcurrentHashMap _classCache; 44 45 public PropertyCache() { 46 _classCache = new InternalConcurrentHashMap(); 47 } 48 49 57 public final PropertyDescriptor [] getPropertyDescriptors(Class type) { 58 CachedClass cc = getCachedClass(type); 59 return (cc != null ? cc.getPropertyDescriptors() : null); 60 } 61 62 public final Method getPropertyGetter(Class type, String property) { 63 CachedClass cc = getCachedClass(type); 64 if(cc == null) 65 return null; 66 CachedProperty cp = cc.getProperty(property); 67 return (cp != null ? cp.getReadMethod() : null); 68 } 69 70 public final Method getPropertySetter(Class type, String property) { 71 CachedClass cc = getCachedClass(type); 72 if(cc == null) 73 return null; 74 CachedProperty cp = cc.getProperty(property); 75 return (cp != null ? cp.getWriteMethod() : null); 76 } 77 78 public final Class getPropertyType(Class type, String property) { 79 CachedClass cc = getCachedClass(type); 80 if(cc == null) 81 return null; 82 CachedProperty cp = cc.getProperty(property); 83 return (cp != null ? cp.getType() : null); 84 } 85 86 private final CachedClass getCachedClass(Class type) { 87 Object obj = _classCache.get(type); 88 if(obj == null) { 89 try { 90 obj = new CachedClass(type); 91 _classCache.put(type, obj); 92 } catch(Exception e) { 93 LOGGER.error("Error introspecting a class of _type \"" + type + "\" when determining its JavaBean property info", e); 94 return null; 95 } 96 } 97 98 return (CachedClass)obj; 99 } 100 101 104 private class CachedClass { 105 106 private Class _type = null; 107 private HashMap _properties = null; 108 private PropertyDescriptor [] _propertyDescriptors = null; 109 110 CachedClass(Class type) 111 throws IntrospectionException { 112 this._type = type; 113 init(type); 114 } 115 116 private void init(Class type) 117 throws IntrospectionException { 118 _properties = new HashMap (); 119 120 if(Modifier.isPublic(type.getModifiers())) { 121 PropertyDescriptor [] pds = Introspector.getBeanInfo(type).getPropertyDescriptors(); 122 for(int i = 0; i < pds.length; i++) { 123 _properties.put(pds[i].getName(), new CachedProperty(pds[i])); 124 } 125 } 126 else { 128 for(Class c = type; c != null; c = c.getSuperclass()) { 130 Class [] interfaces = c.getInterfaces(); 131 for(int i = 0; i < interfaces.length; i++) { 132 Class iface = interfaces[i]; 133 if(Modifier.isPublic(iface.getModifiers())) { 134 PropertyDescriptor [] pds = Introspector.getBeanInfo(iface).getPropertyDescriptors(); 135 for(int j = 0; j < pds.length; j++) { 136 if(!_properties.containsKey(pds[j].getName())) 137 _properties.put(pds[j].getName(), new CachedProperty(pds[j])); 138 } 139 } 140 } 141 } 142 143 Class baseClass = type.getSuperclass(); 145 while(!Modifier.isPublic(baseClass.getModifiers())) { 146 baseClass = baseClass.getSuperclass(); 147 } 148 149 PropertyDescriptor [] pds = Introspector.getBeanInfo(baseClass).getPropertyDescriptors(); 150 for(int j = 0; j < pds.length; j++) { 151 if(!_properties.containsKey(pds[j].getName())) 152 _properties.put(pds[j].getName(), new CachedProperty(pds[j])); 153 } 154 } 155 156 if(_properties.size() > 0) { 157 _propertyDescriptors = new PropertyDescriptor [_properties.size()]; 158 Iterator iterator = _properties.values().iterator(); 159 for(int i = 0; iterator.hasNext(); i++) { 160 _propertyDescriptors[i] = ((CachedProperty)iterator.next()).getPropertyDescriptor(); 161 } 162 } 163 } 164 165 PropertyDescriptor [] getPropertyDescriptors() { 166 return _propertyDescriptors; 167 } 168 169 CachedProperty getProperty(String name) { 170 return (CachedProperty)_properties.get(name); 171 } 172 } 173 174 177 private class CachedProperty { 178 179 private Method _readMethod = null; 180 private Method _writeMethod = null; 181 private String _name = null; 182 private PropertyDescriptor _pd = null; 183 private Class _type = null; 184 185 CachedProperty(PropertyDescriptor pd) { 186 _pd = pd; 187 _name = pd.getName(); 188 _readMethod = pd.getReadMethod(); 189 _writeMethod = pd.getWriteMethod(); 190 _type = pd.getPropertyType(); 191 } 192 193 PropertyDescriptor getPropertyDescriptor() { 194 return _pd; 195 } 196 197 Method getReadMethod() { 198 return _readMethod; 199 } 200 201 Method getWriteMethod() { 202 return _writeMethod; 203 } 204 205 String getName() { 206 return _name; 207 } 208 209 Class getType() { 210 return _type; 211 } 212 } 213 } 214 | Popular Tags |