1 29 30 package nextapp.echo2.app.componentxml; 31 32 import java.beans.BeanInfo ; 33 import java.beans.IndexedPropertyDescriptor ; 34 import java.beans.IntrospectionException ; 35 import java.beans.Introspector ; 36 import java.beans.PropertyDescriptor ; 37 import java.lang.reflect.Field ; 38 import java.lang.reflect.Method ; 39 import java.lang.reflect.Modifier ; 40 import java.util.HashMap ; 41 import java.util.Iterator ; 42 import java.util.Map ; 43 44 52 public class ComponentIntrospector { 53 54 57 private static final int CONSTANT_MODIFERS = Modifier.STATIC | Modifier.PUBLIC | Modifier.FINAL; 58 59 63 private static final Map classLoaderCache = new HashMap (); 64 65 73 public static ComponentIntrospector forName(String typeName, ClassLoader classLoader) 74 throws ClassNotFoundException { 75 Map ciStore; 77 synchronized (classLoaderCache) { 78 ciStore = (Map ) classLoaderCache.get(classLoader); 79 if (ciStore == null) { 80 ciStore = new HashMap (); 81 classLoaderCache.put(classLoader, ciStore); 82 } 83 } 84 85 ComponentIntrospector ci; 87 synchronized (ciStore) { 88 ci = (ComponentIntrospector) ciStore.get(typeName); 89 if (ci == null) { 90 ci = new ComponentIntrospector(typeName, classLoader); 91 ciStore.put(typeName, ci); 92 } 93 } 94 return ci; 95 } 96 97 98 private Class componentClass; 99 private BeanInfo beanInfo; 100 private Map constants; 101 102 106 private Map propertyDescriptorMap = new HashMap (); 107 108 114 private ComponentIntrospector(String typeName, ClassLoader classLoader) 115 throws ClassNotFoundException { 116 super(); 117 componentClass = Class.forName(typeName, true, classLoader); 118 try { 119 beanInfo = Introspector.getBeanInfo(componentClass, Introspector.IGNORE_ALL_BEANINFO); 120 } catch (IntrospectionException ex) { 121 throw new RuntimeException ("Introspection Error", ex); 123 } 124 125 loadConstants(); 126 loadPropertyData(); 127 } 128 129 136 public Iterator getConstantNames() { 137 return constants.keySet().iterator(); 138 } 139 140 147 public Object getConstantValue(String constantName) { 148 return constants.get(constantName); 149 } 150 151 156 public Class getObjectClass() { 157 return componentClass; 158 } 159 160 166 public Class getPropertyClass(String propertyName) { 167 PropertyDescriptor propertyDescriptor = getPropertyDescriptor(propertyName); 168 if (propertyDescriptor == null) { 169 return null; 170 } else if (propertyDescriptor instanceof IndexedPropertyDescriptor ) { 171 return ((IndexedPropertyDescriptor ) propertyDescriptor).getIndexedPropertyType(); 172 } else { 173 return propertyDescriptor.getPropertyType(); 174 } 175 } 176 177 183 public PropertyDescriptor getPropertyDescriptor(String propertyName) { 184 return (PropertyDescriptor ) propertyDescriptorMap.get(propertyName); 185 } 186 187 193 public Method getWriteMethod(String propertyName) { 194 PropertyDescriptor propertyDescriptor = getPropertyDescriptor(propertyName); 195 if (propertyDescriptor == null) { 196 return null; 197 } else { 198 if (propertyDescriptor instanceof IndexedPropertyDescriptor ) { 199 return ((IndexedPropertyDescriptor ) propertyDescriptor).getIndexedWriteMethod(); 200 } else { 201 return propertyDescriptor.getWriteMethod(); 202 } 203 } 204 } 205 206 212 public boolean isIndexedProperty(String propertyName) { 213 return propertyDescriptorMap.get(propertyName) instanceof IndexedPropertyDescriptor ; 214 } 215 216 219 private void loadConstants() { 220 constants = new HashMap (); 221 Field [] fields = componentClass.getFields(); 222 for (int index = 0; index < fields.length; ++index) { 223 if ((fields[index].getModifiers() & CONSTANT_MODIFERS) != 0) { 224 String constantName = fields[index].getName(); 225 try { 226 Object constantValue = fields[index].get(null); 227 constants.put(constantName, constantValue); 228 } catch (IllegalAccessException ex) { 229 } 231 } 232 } 233 } 234 235 238 private void loadPropertyData() { 239 PropertyDescriptor [] propertyDescriptors = beanInfo.getPropertyDescriptors(); 240 for (int index = 0; index < propertyDescriptors.length; ++index) { 241 243 if (propertyDescriptors[index] instanceof IndexedPropertyDescriptor ) { 244 if (((IndexedPropertyDescriptor ) propertyDescriptors[index]).getIndexedWriteMethod() != null) { 245 String name = propertyDescriptors[index].getName(); 246 247 propertyDescriptorMap.put(name, propertyDescriptors[index]); 249 } 250 } else { 251 if (propertyDescriptors[index].getWriteMethod() != null) { 252 String name = propertyDescriptors[index].getName(); 253 254 propertyDescriptorMap.put(name, propertyDescriptors[index]); 256 } 257 } 258 } 259 } 260 } 261 | Popular Tags |