1 18 package org.apache.activemq.tool.properties; 19 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 23 import java.util.Iterator ; 24 import java.util.StringTokenizer ; 25 import java.util.Properties ; 26 import java.util.List ; 27 import java.util.ArrayList ; 28 import java.lang.reflect.Method ; 29 import java.lang.reflect.InvocationTargetException ; 30 import java.lang.reflect.Constructor ; 31 32 public final class ReflectionUtil { 33 private static final Log log = LogFactory.getLog(ReflectionUtil.class); 34 35 private ReflectionUtil() { 36 } 37 38 public static void configureClass(Object obj, String key, String val) { 39 try { 40 String debugInfo; 41 42 Object target = obj; 43 Class targetClass = obj.getClass(); 44 45 debugInfo = "Invoking: " + targetClass.getName(); 47 48 StringTokenizer tokenizer = new StringTokenizer (key, "."); 49 String keySubString = key; 50 int tokenCount = tokenizer.countTokens(); 51 52 for (int j=0; j<tokenCount-1; j++) { 54 String name = tokenizer.nextToken(); 56 57 if (target instanceof ReflectionConfigurable && !((ReflectionConfigurable)target).acceptConfig(keySubString, val)) { 59 return; 60 } else { 61 keySubString = keySubString.substring(name.length() + 1); } 67 68 String getMethod = "get" + name.substring(0,1).toUpperCase() + name.substring(1); 69 Method method = targetClass.getMethod(getMethod, new Class [] {}); 70 target = method.invoke(target, null); 71 targetClass = target.getClass(); 72 73 74 debugInfo += ("." + getMethod + "()"); 75 } 76 77 String property = tokenizer.nextToken(); 79 if (target instanceof ReflectionConfigurable && !((ReflectionConfigurable)target).acceptConfig(property, val)) { 81 return; 82 } 83 84 Method setterMethod = findSetterMethod(targetClass, property); 86 87 if (setterMethod == null) { 89 throw new IllegalAccessException ("Unable to find appropriate setter method signature for property: " + property); 90 } 91 Class paramType = setterMethod.getParameterTypes()[0]; 92 93 debugInfo += ("." + setterMethod + "(" + paramType.getName() + ": " + val + ")"); 95 if (paramType.isPrimitive()) { 96 if (paramType == Boolean.TYPE) { 97 setterMethod.invoke(target, new Object [] {Boolean.valueOf(val)}); 98 } else if (paramType == Integer.TYPE) { 99 setterMethod.invoke(target, new Object [] {Integer.valueOf(val)}); 100 } else if (paramType == Long.TYPE) { 101 setterMethod.invoke(target, new Object [] {Long.valueOf(val)}); 102 } else if (paramType == Double.TYPE) { 103 setterMethod.invoke(target, new Object [] {Double.valueOf(val)}); 104 } else if (paramType == Float.TYPE) { 105 setterMethod.invoke(target, new Object [] {Float.valueOf(val)}); 106 } else if (paramType == Short.TYPE) { 107 setterMethod.invoke(target, new Object [] {Short.valueOf(val)}); 108 } else if (paramType == Byte.TYPE) { 109 setterMethod.invoke(target, new Object [] {Byte.valueOf(val)}); 110 } else if (paramType == Character.TYPE) { 111 setterMethod.invoke(target, new Object [] {new Character (val.charAt(0))}); 112 } 113 } else { 114 if (paramType == String .class) { 116 setterMethod.invoke(target, new Object [] {val}); 117 118 } else { 120 Constructor c = paramType.getConstructor(new Class [] {String .class}); 121 Object paramObject = c.newInstance(new Object [] {val}); 122 123 setterMethod.invoke(target, new Object [] {paramObject}); 124 } 125 } 126 log.debug(debugInfo); 127 128 } catch (Exception e) { 129 log.warn(e); 130 } 131 } 132 133 public static void configureClass(Object obj, Properties props) { 134 for (Iterator i = props.keySet().iterator(); i.hasNext();) { 135 try { 136 String key = (String )i.next(); 137 String val = props.getProperty(key); 138 139 configureClass(obj, key, val); 140 } catch (Throwable t) { 141 t.printStackTrace(); 143 } 144 } 145 } 146 147 public static Properties retrieveObjectProperties(Object obj) { 148 Properties props = new Properties (); 149 try { 150 props.putAll(retrieveClassProperties("", obj.getClass(), obj)); 151 } catch (Exception e) { 152 log.warn(e); 153 } 154 return props; 155 } 156 157 protected static Properties retrieveClassProperties(String prefix, Class targetClass, Object targetObject) { 158 if (targetClass == null || targetObject == null) { 159 return new Properties (); 160 } else { 161 Properties props = new Properties (); 162 Method [] getterMethods = findAllGetterMethods(targetClass); 163 for (int i=0; i<getterMethods.length; i++) { 164 try { 165 String propertyName = getPropertyName(getterMethods[i].getName()); 166 Class retType = getterMethods[i].getReturnType(); 167 168 if (retType.isPrimitive() || retType == String .class) { 170 if (findSetterMethod(targetClass, propertyName) != null) { 172 Object val = null; 173 try { 174 val = getterMethods[i].invoke(targetObject, null); 175 } catch (InvocationTargetException e) { 176 e.printStackTrace(); 177 } catch (IllegalAccessException e) { 178 e.printStackTrace(); 179 } 180 props.setProperty(prefix + propertyName, val + ""); 181 } 182 } else { 183 try { 184 Object val = getterMethods[i].invoke(targetObject, null); 185 if (val != null) { 186 props.putAll(retrieveClassProperties(propertyName + ".", val.getClass(), val)); 187 } 188 } catch (InvocationTargetException e) { 189 e.printStackTrace(); 190 } catch (IllegalAccessException e) { 191 e.printStackTrace(); 192 } 193 } 194 } catch (Throwable t) { 195 t.printStackTrace(); 197 } 198 } 199 return props; 200 } 201 } 202 203 private static Method findSetterMethod(Class targetClass, String propertyName) { 204 String methodName = "set" + propertyName.substring(0,1).toUpperCase() + propertyName.substring(1); 205 206 Method [] methods = targetClass.getMethods(); 207 for (int i=0; i<methods.length; i++) { 208 if (methods[i].getName().equals(methodName) && isSetterMethod(methods[i])) { 209 return methods[i]; 210 } 211 } 212 return null; 213 } 214 215 private static Method findGetterMethod(Class targetClass, String propertyName) { 216 String methodName1 = "get" + propertyName.substring(0,1).toUpperCase() + propertyName.substring(1); 217 String methodName2 = "is" + propertyName.substring(0,1).toUpperCase() + propertyName.substring(1); 218 219 Method [] methods = targetClass.getMethods(); 220 for (int i=0; i<methods.length; i++) { 221 if ((methods[i].getName().equals(methodName1) || methods[i].getName().equals(methodName2)) && isGetterMethod(methods[i])) { 222 return methods[i]; 223 } 224 } 225 return null; 226 } 227 228 private static Method [] findAllGetterMethods(Class targetClass) { 229 List getterMethods = new ArrayList (); 230 Method [] methods = targetClass.getMethods(); 231 232 for (int i=0; i<methods.length; i++) { 233 if (isGetterMethod(methods[i])) { 234 getterMethods.add(methods[i]); 235 } 236 } 237 238 return (Method [])getterMethods.toArray(new Method [] {}); 239 } 240 241 private static boolean isGetterMethod(Method method) { 242 return (((method.getName().startsWith("is") && method.getReturnType() == Boolean.TYPE) || 248 (method.getName().startsWith("get") && method.getReturnType() != Void.TYPE)) && 249 (method.getParameterTypes().length == 0) && method.getDeclaringClass() != Object .class); 250 } 251 252 private static boolean isSetterMethod(Method method) { 253 if (method.getName().startsWith("set") && method.getReturnType() == Void.TYPE) { 255 Class [] paramType = method.getParameterTypes(); 256 if (paramType.length == 1) { 258 if (paramType[0].isPrimitive() || paramType[0] == String .class) { 260 return true; 261 } else { 262 try { 264 if (paramType[0].getConstructor(new Class [] {String .class}) != null) { 265 return true; 266 } 267 } catch (NoSuchMethodException e) { 268 } 270 } 271 } 272 } 273 return false; 274 } 275 276 private static String getPropertyName(String methodName) { 277 String name; 278 if (methodName.startsWith("get")) { 279 name = methodName.substring(3); 280 } else if (methodName.startsWith("set")) { 281 name = methodName.substring(3); 282 } else if (methodName.startsWith("is")) { 283 name = methodName.substring(2); 284 } else { 285 name = ""; 286 } 287 288 return name.substring(0,1).toLowerCase() + name.substring(1); 289 } 290 } 291 | Popular Tags |