1 5 package xdoclet.tagshandler; 6 7 import java.util.*; 8 9 import org.apache.commons.logging.Log; 10 11 import xjavadoc.*; 12 13 import xdoclet.XDocletException; 14 import xdoclet.util.LogUtil; 15 16 24 public class PropertyTagsHandler extends AbstractProgramElementTagsHandler 25 { 26 32 public static XMethod getXMethodForMethodName(String methodName) 33 { 34 return getXMethodForMethodName(methodName, false); 35 } 36 37 44 public static XMethod getXMethodForMethodName(String methodName, boolean superclasses) 45 { 46 if (methodName != null) { 47 return extractXMethod(getCurrentClass(), methodName, superclasses); 48 } 49 50 return null; 51 } 52 53 61 private static XMethod extractXMethod(XClass clazz, String methodName, boolean superclasses) 62 { 63 Collection methods = clazz.getMethods(); 64 65 for (Iterator i = methods.iterator(); i.hasNext(); ) { 66 XMethod method = (XMethod) i.next(); 67 68 if (method.getName().equals(methodName)) { 69 return method; 70 } 71 } 72 73 if (superclasses) { 74 if (clazz.getSuperclass() != null) { 75 return extractXMethod(clazz.getSuperclass(), methodName, superclasses); 76 } 77 } 78 79 return null; 80 } 81 82 97 public void forAllPropertiesWithTag(String template, Properties attributes) throws XDocletException 98 { 99 Log log = LogUtil.getLog(PropertyTagsHandler.class, "forAllPropertiesWithTag"); 100 101 log.debug("in forAllPropertiesWithTag"); 102 103 String requiredTag = attributes.getProperty("tagName"); 104 105 if (requiredTag == null) { 106 throw new XDocletException("missing required tag parameter in forAllPropertiesHavingTag"); 107 } 108 109 XClass oldClass = getCurrentClass(); 110 XClass superclass = null; 111 Collection already = new ArrayList(); 112 113 do { 115 XMethod oldCurrentMethod = getCurrentMethod(); 116 117 Collection methods = getCurrentClass().getMethods(); 118 119 for (Iterator j = methods.iterator(); j.hasNext(); ) { 120 XMethod currentMethod = (XMethod) j.next(); 121 122 log.debug("looking at method " + currentMethod.getName()); 123 if (currentMethod.getDoc().hasTag(requiredTag)) { 124 setCurrentMethod(currentMethod); 125 126 String propertyName = currentMethod.getPropertyName(); 127 128 log.debug("property identified " + propertyName); 129 130 if (!already.contains(propertyName)) { 131 generate(template); 132 133 already.add(propertyName); 134 } 135 } 136 137 setCurrentMethod(oldCurrentMethod); 138 } 139 superclass = getCurrentClass().getSuperclass(); 141 142 if (superclass != null) { 143 pushCurrentClass(superclass); 144 } 145 146 } while (superclass != null); 147 148 setCurrentClass(oldClass); 149 } 150 151 163 public void ifHasGetMethodWithTag(String template, Properties attributes) throws XDocletException 164 { 165 XMethod getMethod = getGetMethodWithTag(attributes); 166 167 if (getMethod != null) { 168 XMethod oldMethod = getCurrentMethod(); 169 170 setCurrentMethod(getMethod); 171 try { 172 generate(template); 173 } 174 finally { 175 setCurrentMethod(oldMethod); 176 } 177 } 178 } 179 180 192 public void ifHasSetMethodWithTag(String template, Properties attributes) throws XDocletException 193 { 194 XMethod setMethod = getSetMethodWithTag(attributes); 195 196 if (setMethod != null) { 197 XMethod oldMethod = getCurrentMethod(); 198 199 setCurrentMethod(setMethod); 200 201 try { 202 generate(template); 203 } 204 finally { 205 setCurrentMethod(oldMethod); 206 } 207 } 208 } 209 210 222 public String propertyTypeWithTag(Properties attributes) throws XDocletException 223 { 224 XMethod getter = getGetMethodWithTag(attributes); 225 226 if (getter != null) { 227 return MethodTagsHandler.getMethodTypeFor(getter); 228 } 229 230 XMethod setter = getSetMethodWithTag(attributes); 231 232 if (setter != null) { 233 XParameter parameter = (XParameter) setter.getParameters().iterator().next(); 234 235 return parameter.getType().getQualifiedName(); 236 } 237 throw new XDocletException("no current property found for method " + getCurrentMethod().getName()); 238 } 239 240 241 256 public String paramValueWithTag(Properties attributes) throws XDocletException 257 { 258 259 XMethod oldMethod = getCurrentMethod(); 260 261 XMethod getter = getGetMethodWithTag(attributes); 262 263 if (getter != null) { 264 setCurrentMethod(getter); 265 266 String value = delimit(getTagValue(attributes, FOR_METHOD), attributes); 267 268 if (value != null) { 269 setCurrentMethod(oldMethod); 270 return value; 271 } 272 } 274 275 XMethod setter = getSetMethodWithTag(attributes); 276 277 if (setter != null) { 278 setCurrentMethod(setter); 279 280 String value = delimit(getTagValue(attributes, FOR_METHOD), attributes); 281 282 if (value != null) { 283 setCurrentMethod(oldMethod); 284 return value; 285 } 286 } 288 setCurrentMethod(oldMethod); 289 return attributes.getProperty("default"); 290 } 291 292 305 public void ifHasParamWithTag(String template, Properties attributes) throws XDocletException 306 { 307 308 XMethod oldMethod = getCurrentMethod(); 309 310 XMethod getter = getGetMethodWithTag(attributes); 311 312 if (getter != null) { 313 setCurrentMethod(getter); 314 315 boolean value = hasTag(attributes, FOR_METHOD); 316 317 if (value) { 318 setCurrentMethod(oldMethod); 319 generate(template); 320 return; 321 } 322 323 } 324 325 XMethod setter = getSetMethodWithTag(attributes); 326 327 if (setter != null) { 328 setCurrentMethod(setter); 329 330 boolean value = hasTag(attributes, FOR_METHOD); 331 332 if (value) { 333 setCurrentMethod(oldMethod); 334 generate(template); 335 return; 336 } 337 } 338 setCurrentMethod(oldMethod); 339 } 340 341 342 350 private XMethod getGetMethodWithTag(Properties attributes) throws XDocletException 351 { 352 String requiredTag = attributes.getProperty("tagName"); 353 354 if (requiredTag == null) { 355 throw new XDocletException("missing required tag parameter in forAllPropertiesHavingTag"); 356 } 357 358 XMethod currentMethod = getCurrentMethod(); 359 360 if (currentMethod.getName().startsWith("get") || currentMethod.getName().startsWith("is")) { 361 if (currentMethod.getDoc().hasTag(requiredTag)) { 362 return currentMethod; 363 } 364 365 return null; 366 } 367 368 String attributeName = MethodTagsHandler.getMethodNameWithoutPrefixFor(currentMethod); 369 XMethod getter = getXMethodForMethodName("get" + attributeName); 370 371 if (getter != null) { 372 if (getter.getDoc().hasTag(requiredTag)) { 373 374 return getter; 375 } 376 377 return null; 378 } 379 getter = getXMethodForMethodName("is" + attributeName); 380 381 if (getter != null && getter.getDoc().hasTag(requiredTag)) { 383 return getter; 384 } 385 return null; 386 } 387 388 396 private XMethod getSetMethodWithTag(Properties attributes) throws XDocletException 397 { 398 String requiredTag = attributes.getProperty("tagName"); 399 400 if (requiredTag == null) { 401 throw new XDocletException("missing required tag parameter in forAllPropertiesHavingTag"); 402 } 403 404 XMethod currentMethod = getCurrentMethod(); 405 406 if (currentMethod.getName().startsWith("set")) { 407 if (currentMethod.getDoc().hasTag(requiredTag)) { 408 return currentMethod; 409 } 410 411 return null; 412 } 413 414 String attributeName = MethodTagsHandler.getMethodNameWithoutPrefixFor(currentMethod); 415 XMethod setter = getXMethodForMethodName("set" + attributeName); 416 417 if (setter != null && setter.getDoc().hasTag(requiredTag)) { 418 return setter; 419 } 420 421 return null; 422 } 423 } 424 | Popular Tags |