1 5 package xdoclet; 6 7 import java.beans.Introspector ; 8 import java.io.Serializable ; 9 import java.lang.reflect.InvocationTargetException ; 10 import java.lang.reflect.Method ; 11 import java.lang.reflect.Modifier ; 12 import java.util.HashMap ; 13 14 import org.apache.commons.logging.Log; 15 16 import xdoclet.util.LogUtil; 17 18 25 public final class ConfigParamIntrospector 26 { 27 public final static Object NULL = new NullObject(); 28 29 35 public static String capitalize(String name) 36 { 37 if (name == null || name.trim().length() == 0) { 38 return name; 39 } 40 41 if (name.length() > 1 && Character.isUpperCase(name.charAt(1)) && 42 Character.isLowerCase(name.charAt(0))) { 43 return name; 44 } 45 46 char chars[] = name.toCharArray(); 47 48 chars[0] = Character.toUpperCase(chars[0]); 49 50 return new String (chars); 51 } 52 53 60 public static Method findGetterMethod(Object javabean, String capitalPropertyName) 61 { 62 Log log = LogUtil.getLog(ConfigParamIntrospector.class, "findGetterMethod"); 63 64 Method getterMethod = null; 65 66 capitalPropertyName = capitalize(capitalPropertyName); 67 68 try { 69 getterMethod = javabean.getClass().getMethod("get" + capitalPropertyName, null); 70 } 71 catch (NoSuchMethodException e) { 72 if (log.isDebugEnabled()) { 73 log.error("Method get" + capitalPropertyName + " not found."); 74 } 75 } 76 77 if (getterMethod == null) { 79 81 try { 82 getterMethod = javabean.getClass().getMethod("is" + capitalPropertyName, null); 83 } 84 catch (NoSuchMethodException e) { 85 if (log.isDebugEnabled()) { 86 log.error("Method is" + capitalPropertyName + " not found."); 87 } 88 } 89 } 90 91 return getterMethod; 92 } 93 94 100 static void fillConfigParamsFor(DocletTask task, HashMap configs) 101 { 102 fillConfigParamsHashMapUsingReflectionFor(task, configs, ""); 103 } 104 105 111 static void fillConfigParamsFor(SubTask subtask, HashMap configs) 112 { 113 fillConfigParamsHashMapUsingReflectionFor(subtask, configs, subtask.getSubTaskName() + '.'); 114 } 115 116 123 private static String getPropertyName(String methodName, String prefix) 124 { 125 int start = prefix.length(); 126 127 return Introspector.decapitalize(methodName.substring(start)); 128 } 129 130 138 private static void fillConfigParamsHashMapUsingReflectionFor(Object javabean, HashMap configs, String propertyPrefix) 139 { 140 Log log = LogUtil.getLog(ConfigParamIntrospector.class, "fillConfigParamsHashMapUsingReflectionFor"); 141 142 if (log.isDebugEnabled()) { 143 log.debug("javabean=" + javabean); 144 log.debug("javabean.getClass()=" + javabean.getClass()); 145 log.debug("configs.size()=" + configs.size()); 146 } 147 148 try { 149 Method [] methods = javabean.getClass().getMethods(); 150 151 for (int i = 0; i < methods.length; i++) { 152 final Method method = methods[i]; 153 final String name = method.getName(); 154 Class returnType = method.getReturnType(); 155 Class [] args = method.getParameterTypes(); 156 157 if (name.startsWith("set") && 159 Modifier.isPublic(method.getModifiers()) && 160 Void.TYPE.equals(returnType) && 161 args.length == 1) { 162 163 String propertyName = getPropertyName(name, "set"); 164 String capitalPropertyName = capitalize(propertyName); 165 Method getterMethod = null; 166 167 if (log.isDebugEnabled()) { 168 log.debug("name=" + name); 169 log.debug("propertyName=" + propertyName); 170 log.debug("capitalPropertyName=" + capitalPropertyName); 171 } 172 173 getterMethod = findGetterMethod(javabean, capitalPropertyName); 174 175 if (getterMethod == null) { 177 if (log.isDebugEnabled()) { 178 log.error("Getter method not found."); 179 } 180 continue; 181 } 182 183 Object propertyValue = null; 185 186 try { 187 propertyValue = getterMethod.invoke(javabean, null); 188 } 189 catch (IllegalAccessException e) { 190 if (log.isDebugEnabled()) { 191 log.error("IllegalAccessException", e); 192 } 193 continue; 194 } 195 catch (IllegalArgumentException e) { 196 if (log.isDebugEnabled()) { 197 log.error("IllegalArgumentException", e); 198 } 199 continue; 200 } 201 catch (InvocationTargetException e) { 202 if (log.isDebugEnabled()) { 203 log.error("InvocationTargetException", e); 204 } 205 continue; 206 } 207 208 if (propertyValue == null) { 209 propertyValue = NULL; 211 } 212 213 if (!(propertyValue instanceof Serializable )) { 216 continue; 217 } 218 219 if (log.isDebugEnabled()) { 220 log.debug("putting propertyName=" + (propertyPrefix + propertyName)); 221 log.debug("putting propertyValue=" + propertyValue); 222 } 223 224 configs.put((propertyPrefix + propertyName).toLowerCase(), propertyValue); 225 } 226 } 227 } 228 catch (SecurityException e) { 229 log.error("A SecurityException exception!!", e); 230 } 231 232 log.debug("configs.size()=" + configs.size()); 233 } 234 235 239 private final static class NullObject implements Serializable 240 { 241 247 public boolean equals(Object obj) 248 { 249 return obj instanceof NullObject; 250 } 251 } 252 } 253 | Popular Tags |