1 52 53 package com.go.beandoc.teadoc; 54 55 import java.beans.Introspector ; 56 57 import java.util.Vector ; 58 import java.util.HashMap ; 59 60 68 public class PropertyDoc extends MemberDoc { 69 70 75 public static PropertyDoc[] create(RootDoc root, 76 MethodDoc[] docs) { 77 78 if (docs == null) { 79 return null; 80 } 81 82 Vector propertyDocVector = new Vector (); 84 85 HashMap propertyMap = new HashMap (); 89 90 for (int i = 0; i < docs.length; i++) { 91 92 MethodDoc method = docs[i]; 93 94 if (!method.isPublic() || 95 method.isStatic() || method.isExcluded()) { 96 continue; 97 } 98 99 String methodName = method.getName(); 100 Parameter[] params = method.getParameters(); 101 Type returnType = method.getReturnType(); 102 String typeName = returnType.getQualifiedTypeName(); 103 if (typeName != null) { 104 String dimension = returnType.getDimension(); 105 if (dimension != null && dimension.length() > 0) { 106 typeName += dimension; 107 } 108 } 109 110 114 String propertyName = null; 115 boolean isReadMethod = true; 116 117 if (params == null || (params.length == 0)) { 118 120 if (methodName.startsWith("get") && methodName.length() > 3) { 121 122 propertyName = 123 Introspector.decapitalize(methodName.substring(3)); 124 } 125 else if (methodName.startsWith("is") && 126 methodName.length() > 2) { 127 128 if ("boolean".equalsIgnoreCase(typeName)) { 129 propertyName = 130 Introspector.decapitalize(methodName.substring(2)); 131 } 132 } 133 } 134 else if (params != null && (params.length == 1)) { 135 137 if (methodName.startsWith("set") && methodName.length() > 3) { 138 139 propertyName = 140 Introspector.decapitalize(methodName.substring(3)); 141 isReadMethod = false; 142 } 143 } 144 145 if (propertyName == null) { 146 continue; 148 } 149 150 PropertyDoc pd = (PropertyDoc) propertyMap.get(propertyName); 151 if (pd != null) { 152 MethodDoc readMethod = pd.getReadMethod(); 153 if (readMethod == null) { 154 pd.setReadMethod(method); 155 } 156 else { 157 MethodDoc writeMethod = pd.getWriteMethod(); 158 if (writeMethod == null) { 159 pd.setWriteMethod(method); 160 } 161 } 162 } 163 else { 164 pd = new PropertyDoc(root, method, isReadMethod); 165 pd.setName(propertyName); 166 propertyMap.put(propertyName, pd); 167 propertyDocVector.addElement(pd); 168 } 169 170 } 171 172 PropertyDoc[] propertyDocs = new PropertyDoc[propertyDocVector.size()]; 173 propertyDocVector.copyInto(propertyDocs); 174 175 return propertyDocs; 176 } 177 178 182 private static final String trimFirstWord(String s) { 183 s = s.trim(); 184 char[] chars = s.toCharArray(); 185 int firstSpace = -1; 186 187 for (int i = 0; i < chars.length; i++) { 188 char c = chars[i]; 189 if (Character.isWhitespace(c)) { 190 firstSpace = i; 191 break; 192 } 193 } 194 195 if (firstSpace == -1) { 196 return s; 197 } 198 199 s = s.substring(firstSpace).trim(); 200 s = capitalize(s); 201 202 return s; 203 } 204 205 private static final String capitalize(String s) { 206 207 if (s != null && s.length() > 1) { 208 s = Character.toUpperCase(s.charAt(0)) + s.substring(1); 209 } 210 return s; 211 } 212 213 214 private String mName; 215 private MethodDoc mReadMethod; 216 private MethodDoc mWriteMethod; 217 private String mCommentText; 218 219 public PropertyDoc(RootDoc root, 220 MethodDoc methodDoc, boolean isReadMethod) { 221 222 super(root, (com.sun.javadoc.MethodDoc) methodDoc.getInnerDoc()); 223 224 if (isReadMethod) { 225 setReadMethod(methodDoc); 226 } 227 else { 228 setWriteMethod(methodDoc); 229 } 230 } 231 232 233 public String getName() { 234 return mName; 235 } 236 237 public void setName(String name) { 238 mName = name; 239 } 240 241 public MethodDoc getReadMethod() { 242 return mReadMethod; 243 } 244 245 public void setReadMethod(MethodDoc doc) { 246 mReadMethod = doc; 247 } 248 249 250 public MethodDoc getWriteMethod() { 251 return mWriteMethod; 252 } 253 254 public void setWriteMethod(MethodDoc doc) { 255 mWriteMethod = doc; 256 } 257 258 259 public String getCommentText() { 260 if (mCommentText != null) { 261 return mCommentText; 262 } 263 264 String text = getTagValue("property"); 265 266 if (text == null) { 267 text = getMethodCommentText(); 268 269 if (text != null) { 270 String t = text.toLowerCase(); 271 272 if (t.startsWith("return ") || 273 t.startsWith("returns ") || 274 t.startsWith("get ") || 275 t.startsWith("gets ") || 276 t.startsWith("set ") || 277 t.startsWith("sets ")) { 278 279 text = trimFirstWord(text); 282 } 283 } 284 } 285 286 if (text != null) { 287 text = text.trim(); 288 } 289 mCommentText = capitalize(text); 290 291 return mCommentText; 292 } 293 294 public boolean isBound() { 295 return isTagPresent("bound"); 296 } 297 298 public boolean isConstrained() { 299 return isTagPresent("constrained"); 300 } 301 302 public boolean isDefault() { 303 return isTagPresent("default"); 304 } 305 306 public String getPropertyEditorClassName() { 307 return getTagValue("editor"); 308 } 309 310 public boolean isMethod() { 311 return false; 312 } 313 314 317 public boolean isTagPresent(String tagName) { 318 319 boolean tagPresent = false; 320 321 if (mReadMethod != null) { 322 tagPresent = mReadMethod.isTagPresent(tagName); 323 } 324 if (!tagPresent && mWriteMethod != null) { 325 tagPresent = mWriteMethod.isTagPresent(tagName); 326 } 327 328 return tagPresent; 329 } 330 331 332 public String getTagValue(String tagName) { 334 String value = null; 335 336 if (mReadMethod != null) { 337 value = mReadMethod.getTagValue(tagName); 338 } 339 if (value == null && mWriteMethod != null) { 340 value = mWriteMethod.getTagValue(tagName); 341 } 342 343 return value; 344 } 345 346 347 348 352 353 private String getMethodCommentText() { 355 356 String text = null; 357 358 if (mReadMethod != null) { 360 text = mReadMethod.getCommentText(); 361 } 362 if (text == null && mWriteMethod != null) { 363 text = mWriteMethod.getCommentText(); 365 } 366 367 if (text == null || text.trim().length() == 0) { 368 text = getTagValue("return"); 370 } 371 372 return text; 373 } 374 375 376 } 377 | Popular Tags |