1 22 package org.jboss.xb.binding.introspection; 23 24 import java.beans.BeanInfo ; 25 import java.beans.IntrospectionException ; 26 import java.beans.PropertyDescriptor ; 27 import java.lang.reflect.Method ; 28 import java.util.Map ; 29 import org.jboss.xb.binding.JBossXBRuntimeException; 30 import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap; 31 32 36 public class ClassInfo 37 { 38 private static final Object FIELD_INFO_NA = new Object (); 39 private final Class type; 40 private Map fields = new ConcurrentHashMap(); 41 private boolean introspected; 42 43 public ClassInfo(Class cls) 44 { 45 this.type = cls; 46 } 47 48 public Class getType() 49 { 50 return type; 51 } 52 53 59 public FieldInfo getFieldInfo(String name, boolean required) 60 { 61 Object o = fields.get(name); 62 if(o == null) 63 { 64 FieldInfo fieldInfo = FieldInfo.getFieldInfo(this, name); 65 if(fieldInfo == null) 66 { 67 fields.put(name, FIELD_INFO_NA); 68 } 69 else 70 { 71 return fieldInfo; 72 } 73 } 74 else if(o != FIELD_INFO_NA) 75 { 76 return (FieldInfo)o; 77 } 78 79 if(required) 80 { 81 throw new JBossXBRuntimeException( 82 "Failed to find read method or field for property '" + name + "' in " + type 83 ); 84 } 85 86 return null; 87 } 88 89 void addFieldInfo(FieldInfo fieldInfo) 90 { 91 fields.put(fieldInfo.getName(), fieldInfo); 92 } 93 94 FieldInfo introspect(String name) 95 { 96 if(introspected) 97 { 98 return null; 99 } 100 101 try 102 { 103 BeanInfo info = java.beans.Introspector.getBeanInfo(type); 104 PropertyDescriptor [] props = info.getPropertyDescriptors(); 105 if(props != null) 106 { 107 for(int i = 0; i < props.length; ++i) 108 { 109 PropertyDescriptor prop = props[i]; 110 Method readMethod = prop.getReadMethod(); 111 if(readMethod != null) 113 { 114 Method writeMethod = prop.getWriteMethod(); 115 FieldInfo fieldInfo = new FieldInfo(type, prop.getName(), readMethod, writeMethod); 116 addFieldInfo(fieldInfo); 117 } 118 } 119 } 120 } 121 catch(IntrospectionException e) 122 { 123 } 124 125 introspected = true; 126 return getFieldInfo(name, false); 127 } 128 } 129 | Popular Tags |