1 5 package xdoclet.modules.java.javabean; 6 7 import java.beans.Introspector ; 8 import java.text.MessageFormat ; 9 import java.util.Properties ; 10 11 import org.apache.commons.collections.Predicate; 12 import org.apache.commons.logging.Log; 13 import xjavadoc.XClass; 14 import xjavadoc.XMethod; 15 import xjavadoc.XParameter; 16 import xjavadoc.XTag; 17 import xjavadoc.XType; 18 19 import xdoclet.XDocletException; 20 import xdoclet.XDocletTagSupport; 21 import xdoclet.util.LogUtil; 22 23 32 public class JavaBeanTagsHandler extends XDocletTagSupport 33 { 34 public static String getBeanInfoClassFor(XClass clazz) throws XDocletException 35 { 36 XTag beanTag = clazz.getDoc().getTag("javabean.class"); 37 String className = null; 38 39 if (beanTag != null) { 40 className = beanTag.getAttributeValue("class"); 42 } 43 44 if (className == null) { 45 className = clazz.getQualifiedName(); 46 } 47 48 return MessageFormat.format(BeanInfoSubTask.GENERATED_BEANINFO_CLASS_NAME, new Object []{className}); 49 } 50 51 59 private static boolean isPossiblePropertyAccessor(XMethod method) 60 { 61 return (method.getParameters().size() == 0 && !method.getReturnType().getType().isA("void")); 62 } 63 64 private static boolean isPossiblePropertyAccessor(XMethod method, XType propertyType) 65 { 66 return isPossiblePropertyAccessor(method) && method.getReturnType().getType().equals(propertyType); 67 } 68 69 70 77 private static boolean isPossiblePropertyMutator(XMethod method) 78 { 79 return (method.getParameters().size() == 1); 80 } 81 82 private static boolean isPossiblePropertyMutator(XMethod method, XType propertyType) 83 { 84 if (isPossiblePropertyMutator(method)) { 85 XParameter firstParam = (XParameter) method.getParameters().get(0); 86 87 return firstParam.getType().equals(propertyType); 88 } 89 return false; 90 } 91 92 private static boolean isExplicitlyReadOnlyProperty(XMethod method) 93 { 94 XTag tag = method.getDoc().getTag("javabean.property"); 95 96 return "true".equalsIgnoreCase(tag.getAttributeValue("readonly")); 97 } 98 99 100 107 private static XType getPropertyType(XMethod method) 108 { 109 XType result = null; 110 111 String name = method.getDoc().getTag("javabean.property").getAttributeValue("name"); 113 114 if ((name == null) || (name.length() <= 0)) { 115 if (isPossiblePropertyMutator(method)) { 116 XParameter parameter = (XParameter) method.getParameters().get(0); 117 118 result = parameter.getType(); 119 } 120 else if (isPossiblePropertyAccessor(method)) { 121 result = method.getReturnType().getType(); 122 } 123 } 124 else { 125 result = method.getPropertyType().getType(); 126 } 127 return result; 128 } 129 130 144 private static XMethod getGetterMethod(final String propertyName, final XType propertyType, XMethod method) 145 { 146 Log log = LogUtil.getLog(JavaBeanTagsHandler.class, "getGetterMethod"); 147 148 if ((propertyName == null) || (propertyName.length() == 0)) { 150 log.error("invalid property name: " + propertyName); 151 return null; 152 } 153 if (propertyType == null) { 154 log.error("invalid property type: " + propertyType); 155 return null; 156 } 157 if (method == null) { 158 log.error("invalid method: " + method); 159 return null; 160 } 161 162 if (log.isInfoEnabled()) { 164 log.info("===find getter method==="); 165 log.info("method name: " + method.getName()); 166 log.info("property name: " + propertyName); 167 log.info("property type: " + propertyType); 168 } 169 170 XMethod getterMethod = null; 171 172 final String explicitMethodName = method.getDoc().getTag("javabean.property").getAttributeValue("getter"); 174 175 if ((explicitMethodName != null) && (explicitMethodName.length() > 0)) { 176 java.util.List methodList = getCurrentClass().getMethods( 179 new Predicate() 180 { 181 public boolean evaluate(Object obj) 182 { 183 XMethod method = (XMethod) obj; 184 185 return isPossiblePropertyAccessor(method, propertyType) && 186 method.getName().equals(explicitMethodName); 187 } 188 }, false); 189 190 192 if (methodList.size() == 1) { 193 getterMethod = (XMethod) methodList.get(0); 194 if (log.isInfoEnabled()) { 195 log.info("found explicit getter " + getterMethod.getName()); 196 } 197 if (isPossiblePropertyAccessor(method, propertyType) 198 && (getterMethod != method)) { 199 log.warn("explicit getter " + getterMethod.getName() + " (should be passed method)"); 200 } 201 } 202 else { 203 log.warn("no explicit getter " + explicitMethodName); 205 } 206 } 207 else if (isPossiblePropertyAccessor(method, propertyType)) { 208 getterMethod = method; 210 log.info("using the passed method"); 211 } 212 else { 213 java.util.List methodList = getCurrentClass().getMethods( 216 new Predicate() 217 { 218 public boolean evaluate(Object obj) 219 { 220 XMethod method = (XMethod) obj; 221 222 return isPossiblePropertyAccessor(method, propertyType) && 223 method.getPropertyName().equals(propertyName); 224 } 225 }, false); 226 227 if (methodList.size() == 1) { 228 getterMethod = (XMethod) methodList.get(0); 229 if (log.isInfoEnabled()) { 230 log.info("found standard getter " + getterMethod.getName()); 231 } 232 } 233 else { 234 log.warn("no standard getter"); 235 } 236 } 237 return getterMethod; 238 } 239 240 private static XMethod getSetterMethod(final String propertyName, final XType propertyType, XMethod method) 241 { 242 Log log = LogUtil.getLog(JavaBeanTagsHandler.class, "getSetterMethod"); 243 244 if ((propertyName == null) || (propertyName.length() == 0)) { 246 log.error("invalid property name: " + propertyName); 247 return null; 248 } 249 if (propertyType == null) { 250 log.error("invalid property type: " + propertyType); 251 return null; 252 } 253 if (method == null) { 254 log.error("invalid method: " + method); 255 return null; 256 } 257 258 XMethod setterMethod = null; 260 261 if (log.isInfoEnabled()) { 262 log.info("===find setter method==="); 263 log.info("method name: " + method.getName()); 264 log.info("property name: " + propertyName); 265 log.info("property type: " + propertyType); 266 } 267 268 final String explicitMethodName = method.getDoc().getTag("javabean.property").getAttributeValue("setter"); 270 271 if (isExplicitlyReadOnlyProperty(method)) { 272 log.info("explicit read only"); 273 } 274 else if ((explicitMethodName != null) && (explicitMethodName.length() > 0)) { 275 java.util.List methodList = getCurrentClass().getMethods( 278 new Predicate() 279 { 280 public boolean evaluate(Object obj) 281 { 282 XMethod method = (XMethod) obj; 283 284 return isPossiblePropertyMutator(method, propertyType) && 285 method.getName().equals(explicitMethodName); 286 } 287 }, false); 288 289 if (methodList.size() == 1) { 290 setterMethod = (XMethod) methodList.get(0); 291 if (log.isInfoEnabled()) { 292 log.info("found explicit setter " + setterMethod.getName()); 293 } 294 if (isPossiblePropertyMutator(method, propertyType) 295 && (setterMethod != method)) { 296 log.warn("explicit setter " + setterMethod.getName() + " (should be passed method)"); 297 } 298 } 299 else { 300 log.warn("no explicit setter " + explicitMethodName); 302 } 303 } 304 else if (isPossiblePropertyMutator(method, propertyType)) { 305 setterMethod = method; 307 log.info("using the passed method"); 308 } 309 else { 310 java.util.List methodList = getCurrentClass().getMethods( 313 new Predicate() 314 { 315 public boolean evaluate(Object obj) 316 { 317 XMethod method = (XMethod) obj; 318 319 return isPossiblePropertyMutator(method, propertyType) && 320 propertyName.equals(method.getPropertyName()); 321 } 322 }, false); 323 324 if (methodList.size() == 1) { 325 setterMethod = (XMethod) methodList.get(0); 326 if (log.isInfoEnabled()) { 327 log.info("found standard setter " + setterMethod.getName()); 328 } 329 } 330 else { 331 log.info("no standard setter (not tagged readonly)"); 332 } 333 } 334 return setterMethod; 335 } 336 337 344 public String getterPrefix(Properties attributes) throws XDocletException 345 { 346 String name = getTagValue(attributes, FOR_CLASS); 347 348 if ("boolean".equals(name)) { 349 return "is"; 350 } 351 if ("java.lang.Boolean".equals(name)) { 352 return "is"; 353 } 354 return "get"; 355 } 356 357 362 public String getGetterMethodNameQuoted() 363 { 364 XMethod currentMethod = getCurrentMethod(); 365 String propertyName = getPropertyName(currentMethod); 366 XType propertyType = getPropertyType(currentMethod); 367 368 if (propertyName != null) { 369 } 370 371 XMethod getterMethod = getGetterMethod(propertyName, propertyType, currentMethod); 372 373 374 if (getterMethod == null) { 375 return "null"; 376 } 377 else { 378 return "\"" + getterMethod.getName() + "\""; 379 } 380 } 381 382 387 public String getSetterMethodNameQuoted() 388 { 389 XMethod currentMethod = getCurrentMethod(); 390 String propertyName = getPropertyName(currentMethod); 391 XType propertyType = getPropertyType(currentMethod); 392 393 XMethod setterMethod = getSetterMethod(propertyName, propertyType, currentMethod); 394 395 if (setterMethod == null) { 396 return "null"; 397 } 398 else { 399 return "\"" + setterMethod.getName() + "\""; 400 } 401 } 402 403 408 public String getPropertyNameQuoted() 409 { 410 String name = getPropertyName(getCurrentMethod()); 411 412 if (name == null) { 413 return "null"; 414 } 415 else { 416 return "\"" + name + "\""; 417 } 418 } 419 420 427 public String beanClass(Properties attributes) throws XDocletException 428 { 429 if (getTagValue(FOR_CLASS, "javabean.class", "class", null, null, false, false) != null) { 430 return getTagValue(FOR_CLASS, "javabean.class", "class", null, null, false, false); 431 } 432 else { 433 return getCurrentClass().getQualifiedName(); 434 } 435 } 436 437 444 public String capitalizeClassTag(Properties attributes) throws XDocletException 445 { 446 String name = getTagValue(attributes, FOR_CLASS); 447 448 if (name == null || name.length() == 0) { 449 return name; 450 } 451 if (name.length() > 1 && Character.isUpperCase(name.charAt(1)) && 452 Character.isUpperCase(name.charAt(0))) { 453 return name; 454 } 455 456 char chars[] = name.toCharArray(); 457 458 chars[0] = Character.toUpperCase(chars[0]); 459 return new String (chars); 460 } 461 462 468 private String getPropertyName(XMethod currentMethod) 469 { 470 String name = null; 471 XTag tag = currentMethod.getDoc().getTag("javabean.property"); 472 473 if (tag != null) { 474 name = tag.getAttributeValue("name"); 476 if ((name == null) || (name.length() <= 0)) { 478 name = getCurrentMethod().getPropertyName(); 481 } 482 else { 483 name = Introspector.decapitalize(name); 484 } 485 } 486 return name; 487 } 488 } 489 | Popular Tags |