1 52 53 package com.go.trove.util; 54 55 import java.beans.*; 56 import java.util.*; 57 import java.lang.ref.*; 58 59 67 public class CompleteIntrospector { 68 private static Map cPropertiesCache; 70 71 static { 72 cPropertiesCache = new IdentityMap(); 73 74 Introspector.setBeanInfoSearchPath(new String [0]); 81 } 82 83 86 public static void main(String [] args) throws Exception { 87 Map map = getAllProperties(Class.forName(args[0])); 88 Iterator keys = map.keySet().iterator(); 89 while (keys.hasNext()) { 90 String key = (String )keys.next(); 91 PropertyDescriptor desc = (PropertyDescriptor)map.get(key); 92 System.out.println(key + " = " + desc); 93 } 94 } 95 96 106 public static Map getAllProperties(Class clazz) 107 throws IntrospectionException { 108 109 synchronized (cPropertiesCache) { 110 Map properties; 111 112 Reference ref = (Reference)cPropertiesCache.get(clazz); 113 if (ref != null) { 114 properties = (Map)ref.get(); 115 if (properties != null) { 116 return properties; 117 } 118 else { 119 cPropertiesCache.remove(clazz); 121 } 122 } 123 124 properties = Collections.unmodifiableMap(createProperties(clazz)); 125 cPropertiesCache.put(clazz, new SoftReference(properties)); 126 return properties; 127 } 128 } 129 130 private static Map createProperties(Class clazz) 131 throws IntrospectionException { 132 133 Map properties = new HashMap(); 134 135 if (clazz == null || clazz.isPrimitive()) { 136 return properties; 137 } 138 139 BeanInfo info; 140 try { 141 info = Introspector.getBeanInfo(clazz); 142 } 143 catch (LinkageError e) { 144 throw new IntrospectionException(e.toString()); 145 } 146 147 if (info != null) { 148 PropertyDescriptor[] pdArray = info.getPropertyDescriptors(); 149 150 int length = pdArray.length; 152 for (int i=0; i<length; i++) { 153 properties.put(pdArray[i].getName(), pdArray[i]); 154 } 155 } 156 157 if (clazz.isInterface()) { 159 properties.putAll(getAllProperties(Object .class)); 160 } 161 162 Class [] interfaces = clazz.getInterfaces(); 164 for (int i=0; i<interfaces.length; i++) { 165 properties.putAll(getAllProperties(interfaces[i])); 166 } 167 168 Iterator it = properties.keySet().iterator(); 170 while (it.hasNext()) { 171 String propertyName = (String )it.next(); 172 if (propertyName.indexOf('$') >= 0) { 173 it.remove(); 174 } 175 } 176 177 return properties; 178 } 179 } 180 | Popular Tags |