1 32 33 package com.jeantessier.diff; 34 35 import java.util.*; 36 37 import org.apache.log4j.*; 38 39 import com.jeantessier.classreader.*; 40 41 public class ClassReport extends Printer implements Comparable { 42 private ClassDifferences differences; 43 44 private Collection removedFields = new TreeSet(); 45 private Collection removedConstructors = new TreeSet(); 46 private Collection removedMethods = new TreeSet(); 47 48 private Collection deprecatedFields = new TreeSet(); 49 private Collection deprecatedConstructors = new TreeSet(); 50 private Collection deprecatedMethods = new TreeSet(); 51 52 private Collection undocumentedFields = new TreeSet(); 53 private Collection undocumentedConstructors = new TreeSet(); 54 private Collection undocumentedMethods = new TreeSet(); 55 56 private Collection modifiedFields = new TreeSet(); 57 private Collection modifiedConstructors = new TreeSet(); 58 private Collection modifiedMethods = new TreeSet(); 59 60 private Collection documentedFields = new TreeSet(); 61 private Collection documentedConstructors = new TreeSet(); 62 private Collection documentedMethods = new TreeSet(); 63 64 private Collection undeprecatedFields = new TreeSet(); 65 private Collection undeprecatedConstructors = new TreeSet(); 66 private Collection undeprecatedMethods = new TreeSet(); 67 68 private Collection newFields = new TreeSet(); 69 private Collection newConstructors = new TreeSet(); 70 private Collection newMethods = new TreeSet(); 71 72 public void visitClassDifferences(ClassDifferences differences) { 73 this.differences = differences; 74 75 Iterator i = differences.getFeatureDifferences().iterator(); 76 while (i.hasNext()) { 77 ((Differences) i.next()).accept(this); 78 } 79 } 80 81 public void visitInterfaceDifferences(InterfaceDifferences differences) { 82 this.differences = differences; 83 84 Iterator i = differences.getFeatureDifferences().iterator(); 85 while (i.hasNext()) { 86 ((Differences) i.next()).accept(this); 87 } 88 } 89 90 public void visitFieldDifferences(FieldDifferences differences) { 91 if (differences.isRemoved()) { 92 removedFields.add(differences); 93 } 94 95 if (differences.isModified()) { 96 modifiedFields.add(differences); 97 } 98 99 if (differences.isNew()) { 100 newFields.add(differences); 101 } 102 103 if (isDeprecated()) { 104 deprecatedFields.add(differences); 105 } 106 107 if (isUndeprecated()) { 108 undeprecatedFields.add(differences); 109 } 110 111 if (isDocumented()) { 112 documentedFields.add(differences); 113 } 114 115 if (isUndocumented()) { 116 undocumentedFields.add(differences); 117 } 118 } 119 120 public void visitConstructorDifferences(ConstructorDifferences differences) { 121 if (differences.isRemoved()) { 122 removedConstructors.add(differences); 123 } 124 125 if (differences.isModified()) { 126 modifiedConstructors.add(differences); 127 } 128 129 if (differences.isNew()) { 130 newConstructors.add(differences); 131 } 132 133 if (isDeprecated()) { 134 deprecatedConstructors.add(differences); 135 } 136 137 if (isUndeprecated()) { 138 undeprecatedConstructors.add(differences); 139 } 140 141 if (isDocumented()) { 142 documentedConstructors.add(differences); 143 } 144 145 if (isUndocumented()) { 146 undocumentedConstructors.add(differences); 147 } 148 } 149 150 public void visitMethodDifferences(MethodDifferences differences) { 151 if (differences.isRemoved()) { 152 removedMethods.add(differences); 153 } 154 155 if (differences.isModified()) { 156 modifiedMethods.add(differences); 157 } 158 159 if (differences.isNew()) { 160 newMethods.add(differences); 161 } 162 163 if (isDeprecated()) { 164 deprecatedMethods.add(differences); 165 } 166 167 if (isUndeprecated()) { 168 undeprecatedMethods.add(differences); 169 } 170 171 if (isDocumented()) { 172 documentedMethods.add(differences); 173 } 174 175 if (isUndocumented()) { 176 undocumentedMethods.add(differences); 177 } 178 } 179 180 public String toString() { 181 raiseIndent(); 182 raiseIndent(); 183 184 indent().append("<class>").eol(); 185 raiseIndent(); 186 187 indent().append("<name>").append(differences.getName()).append("</name>").eol(); 188 189 if (!differences.getOldDeclaration().equals(differences.getNewDeclaration())) { 190 indent().append("<modified-declaration>").eol(); 191 raiseIndent(); 192 193 indent().append("<old-declaration").append(breakdownDeclaration(differences.getOldClass())).append(">").append(differences.getOldDeclaration()).append("</old-declaration>").eol(); 194 indent().append("<new-declaration").append(breakdownDeclaration(differences.getNewClass())).append(">").append(differences.getNewDeclaration()).append("</new-declaration>").eol(); 195 196 lowerIndent(); 197 indent().append("</modified-declaration>").eol(); 198 } 199 200 if (removedFields.size() != 0) { 201 indent().append("<removed-fields>").eol(); 202 raiseIndent(); 203 204 Iterator i = removedFields.iterator(); 205 while (i.hasNext()) { 206 FeatureDifferences fd = (FeatureDifferences) i.next(); 207 indent().append("<declaration").append(breakdownDeclaration((Field_info) fd.getOldFeature())).append(fd.isInherited() ? " inherited=\"yes\"" : "").append(">").append(fd.getOldDeclaration()).append("</declaration>").eol(); 208 } 209 210 lowerIndent(); 211 indent().append("</removed-fields>").eol(); 212 } 213 214 if (removedConstructors.size() != 0) { 215 indent().append("<removed-constructors>").eol(); 216 raiseIndent(); 217 218 Iterator i = removedConstructors.iterator(); 219 while (i.hasNext()) { 220 FeatureDifferences fd = (FeatureDifferences) i.next(); 221 indent().append("<declaration").append(breakdownDeclaration((Method_info) fd.getOldFeature())).append(fd.isInherited() ? " inherited=\"yes\"" : "").append(">").append(fd.getOldDeclaration()).append("</declaration>").eol(); 222 } 223 224 lowerIndent(); 225 indent().append("</removed-constructors>").eol(); 226 } 227 228 if (removedMethods.size() != 0) { 229 indent().append("<removed-methods>").eol(); 230 raiseIndent(); 231 232 Iterator i = removedMethods.iterator(); 233 while (i.hasNext()) { 234 FeatureDifferences fd = (FeatureDifferences) i.next(); 235 indent().append("<declaration").append(breakdownDeclaration((Method_info) fd.getOldFeature())).append(fd.isInherited() ? " inherited=\"yes\"" : "").append(">").append(fd.getOldDeclaration()).append("</declaration>").eol(); 236 } 237 238 lowerIndent(); 239 indent().append("</removed-methods>").eol(); 240 } 241 242 if (deprecatedFields.size() != 0) { 243 indent().append("<deprecated-fields>").eol(); 244 raiseIndent(); 245 246 Iterator i = deprecatedFields.iterator(); 247 while (i.hasNext()) { 248 FeatureDifferences fd = (FeatureDifferences) i.next(); 249 indent().append("<declaration").append(breakdownDeclaration((Field_info) fd.getNewFeature())).append(">").append(fd.getOldDeclaration()).append("</declaration>").eol(); 250 } 251 252 lowerIndent(); 253 indent().append("</deprecated-fields>").eol(); 254 } 255 256 if (deprecatedConstructors.size() != 0) { 257 indent().append("<deprecated-constructors>").eol(); 258 raiseIndent(); 259 260 Iterator i = deprecatedConstructors.iterator(); 261 while (i.hasNext()) { 262 FeatureDifferences fd = (FeatureDifferences) i.next(); 263 indent().append("<declaration").append(breakdownDeclaration((Method_info) fd.getNewFeature())).append(">").append(fd.getOldDeclaration()).append("</declaration>").eol(); 264 } 265 266 lowerIndent(); 267 indent().append("</deprecated-constructors>").eol(); 268 } 269 270 if (deprecatedMethods.size() != 0) { 271 indent().append("<deprecated-methods>").eol(); 272 raiseIndent(); 273 274 Iterator i = deprecatedMethods.iterator(); 275 while (i.hasNext()) { 276 FeatureDifferences fd = (FeatureDifferences) i.next(); 277 indent().append("<declaration").append(breakdownDeclaration((Method_info) fd.getNewFeature())).append(">").append(fd.getOldDeclaration()).append("</declaration>").eol(); 278 } 279 280 lowerIndent(); 281 indent().append("</deprecated-methods>").eol(); 282 } 283 284 if (undocumentedFields.size() != 0) { 285 indent().append("<undocumented-fields>").eol(); 286 raiseIndent(); 287 288 Iterator i = undocumentedFields.iterator(); 289 while (i.hasNext()) { 290 FeatureDifferences fd = (FeatureDifferences) i.next(); 291 indent().append("<declaration").append(breakdownDeclaration((Field_info) fd.getNewFeature())).append(">").append(fd.getOldDeclaration()).append("</declaration>").eol(); 292 } 293 294 lowerIndent(); 295 indent().append("</undocumented-fields>").eol(); 296 } 297 298 if (undocumentedConstructors.size() != 0) { 299 indent().append("<undocumented-constructors>").eol(); 300 raiseIndent(); 301 302 Iterator i = undocumentedConstructors.iterator(); 303 while (i.hasNext()) { 304 FeatureDifferences fd = (FeatureDifferences) i.next(); 305 indent().append("<declaration").append(breakdownDeclaration((Method_info) fd.getNewFeature())).append(">").append(fd.getOldDeclaration()).append("</declaration>").eol(); 306 } 307 308 lowerIndent(); 309 indent().append("</undocumented-constructors>").eol(); 310 } 311 312 if (undocumentedMethods.size() != 0) { 313 indent().append("<undocumented-methods>").eol(); 314 raiseIndent(); 315 316 Iterator i = undocumentedMethods.iterator(); 317 while (i.hasNext()) { 318 FeatureDifferences fd = (FeatureDifferences) i.next(); 319 indent().append("<declaration").append(breakdownDeclaration((Method_info) fd.getNewFeature())).append(">").append(fd.getOldDeclaration()).append("</declaration>").eol(); 320 } 321 322 lowerIndent(); 323 indent().append("</undocumented-methods>").eol(); 324 } 325 326 if (modifiedFields.size() != 0) { 327 indent().append("<modified-fields>").eol(); 328 raiseIndent(); 329 330 Iterator i = modifiedFields.iterator(); 331 while (i.hasNext()) { 332 FeatureDifferences fd = (FeatureDifferences) i.next(); 333 334 indent().append("<feature>").eol(); 335 raiseIndent(); 336 337 indent().append("<name>").append(fd.getName()).append("</name>").eol(); 338 339 indent().append("<modified-declaration>").eol(); 340 raiseIndent(); 341 indent().append("<old-declaration").append(breakdownDeclaration((Field_info) fd.getOldFeature())).append(">").append(fd.getOldDeclaration()).append("</old-declaration>").eol(); 342 indent().append("<new-declaration").append(breakdownDeclaration((Field_info) fd.getNewFeature())).append(">").append(fd.getNewDeclaration()).append("</new-declaration>").eol(); 343 lowerIndent(); 344 indent().append("</modified-declaration>").eol(); 345 346 lowerIndent(); 347 indent().append("</feature>").eol(); 348 } 349 350 lowerIndent(); 351 indent().append("</modified-fields>").eol(); 352 } 353 354 if (modifiedConstructors.size() != 0) { 355 indent().append("<modified-constructors>").eol(); 356 raiseIndent(); 357 358 Iterator i = modifiedConstructors.iterator(); 359 while (i.hasNext()) { 360 FeatureDifferences fd = (FeatureDifferences) i.next(); 361 362 indent().append("<feature>").eol(); 363 raiseIndent(); 364 365 indent().append("<name>").append(fd.getName()).append("</name>").eol(); 366 367 indent().append("<modified-declaration>").eol(); 368 raiseIndent(); 369 indent().append("<old-declaration").append(breakdownDeclaration((Method_info) fd.getOldFeature())).append(">").append(fd.getOldDeclaration()).append("</old-declaration>").eol(); 370 indent().append("<new-declaration").append(breakdownDeclaration((Method_info) fd.getNewFeature())).append(">").append(fd.getNewDeclaration()).append("</new-declaration>").eol(); 371 lowerIndent(); 372 indent().append("</modified-declaration>").eol(); 373 374 lowerIndent(); 375 indent().append("</feature>").eol(); 376 } 377 378 lowerIndent(); 379 indent().append("</modified-constructors>").eol(); 380 } 381 382 if (modifiedMethods.size() != 0) { 383 indent().append("<modified-methods>").eol(); 384 raiseIndent(); 385 386 Iterator i = modifiedMethods.iterator(); 387 while (i.hasNext()) { 388 FeatureDifferences fd = (FeatureDifferences) i.next(); 389 390 indent().append("<feature>").eol(); 391 raiseIndent(); 392 393 indent().append("<name>").append(fd.getName()).append("</name>").eol(); 394 395 indent().append("<modified-declaration>").eol(); 396 raiseIndent(); 397 indent().append("<old-declaration").append(breakdownDeclaration((Method_info) fd.getOldFeature())).append(">").append(fd.getOldDeclaration()).append("</old-declaration>").eol(); 398 indent().append("<new-declaration").append(breakdownDeclaration((Method_info) fd.getNewFeature())).append(">").append(fd.getNewDeclaration()).append("</new-declaration>").eol(); 399 lowerIndent(); 400 indent().append("</modified-declaration>").eol(); 401 402 lowerIndent(); 403 indent().append("</feature>").eol(); 404 } 405 406 lowerIndent(); 407 indent().append("</modified-methods>").eol(); 408 } 409 410 if (documentedFields.size() != 0) { 411 indent().append("<documented-fields>").eol(); 412 raiseIndent(); 413 414 Iterator i = documentedFields.iterator(); 415 while (i.hasNext()) { 416 FeatureDifferences fd = (FeatureDifferences) i.next(); 417 indent().append("<declaration").append(breakdownDeclaration((Field_info) fd.getNewFeature())).append(">").append(fd.getOldDeclaration()).append("</declaration>").eol(); 418 } 419 420 lowerIndent(); 421 indent().append("</documented-fields>").eol(); 422 } 423 424 if (documentedConstructors.size() != 0) { 425 indent().append("<documented-constructors>").eol(); 426 raiseIndent(); 427 428 Iterator i = documentedConstructors.iterator(); 429 while (i.hasNext()) { 430 FeatureDifferences fd = (FeatureDifferences) i.next(); 431 indent().append("<declaration").append(breakdownDeclaration((Method_info) fd.getNewFeature())).append(">").append(fd.getOldDeclaration()).append("</declaration>").eol(); 432 } 433 434 lowerIndent(); 435 indent().append("</documented-constructors>").eol(); 436 } 437 438 if (documentedMethods.size() != 0) { 439 indent().append("<documented-methods>").eol(); 440 raiseIndent(); 441 442 Iterator i = documentedMethods.iterator(); 443 while (i.hasNext()) { 444 FeatureDifferences fd = (FeatureDifferences) i.next(); 445 indent().append("<declaration").append(breakdownDeclaration((Method_info) fd.getNewFeature())).append(">").append(fd.getOldDeclaration()).append("</declaration>").eol(); 446 } 447 448 lowerIndent(); 449 indent().append("</documented-methods>").eol(); 450 } 451 452 if (undeprecatedFields.size() != 0) { 453 indent().append("<undeprecated-fields>").eol(); 454 raiseIndent(); 455 456 Iterator i = undeprecatedFields.iterator(); 457 while (i.hasNext()) { 458 FeatureDifferences fd = (FeatureDifferences) i.next(); 459 indent().append("<declaration").append(breakdownDeclaration((Field_info) fd.getNewFeature())).append(">").append(fd.getOldDeclaration()).append("</declaration>").eol(); 460 } 461 462 lowerIndent(); 463 indent().append("</undeprecated-fields>").eol(); 464 } 465 466 if (undeprecatedConstructors.size() != 0) { 467 indent().append("<undeprecated-constructors>").eol(); 468 raiseIndent(); 469 470 Iterator i = undeprecatedConstructors.iterator(); 471 while (i.hasNext()) { 472 FeatureDifferences fd = (FeatureDifferences) i.next(); 473 indent().append("<declaration").append(breakdownDeclaration((Method_info) fd.getNewFeature())).append(">").append(fd.getOldDeclaration()).append("</declaration>").eol(); 474 } 475 476 lowerIndent(); 477 indent().append("</undeprecated-constructors>").eol(); 478 } 479 480 if (undeprecatedMethods.size() != 0) { 481 indent().append("<undeprecated-methods>").eol(); 482 raiseIndent(); 483 484 Iterator i = undeprecatedMethods.iterator(); 485 while (i.hasNext()) { 486 FeatureDifferences fd = (FeatureDifferences) i.next(); 487 indent().append("<declaration").append(breakdownDeclaration((Method_info) fd.getNewFeature())).append(">").append(fd.getOldDeclaration()).append("</declaration>").eol(); 488 } 489 490 lowerIndent(); 491 indent().append("</undeprecated-methods>").eol(); 492 } 493 494 if (newFields.size() != 0) { 495 indent().append("<new-fields>").eol(); 496 raiseIndent(); 497 498 Iterator i = newFields.iterator(); 499 while (i.hasNext()) { 500 FeatureDifferences fd = (FeatureDifferences) i.next(); 501 indent().append("<declaration").append(breakdownDeclaration((Field_info) fd.getNewFeature())).append(">").append(fd.getNewDeclaration()).append("</declaration>").eol(); 502 } 503 504 lowerIndent(); 505 indent().append("</new-fields>").eol(); 506 } 507 508 if (newConstructors.size() != 0) { 509 indent().append("<new-constructors>").eol(); 510 raiseIndent(); 511 512 Iterator i = newConstructors.iterator(); 513 while (i.hasNext()) { 514 FeatureDifferences fd = (FeatureDifferences) i.next(); 515 indent().append("<declaration").append(breakdownDeclaration((Method_info) fd.getNewFeature())).append(">").append(fd.getNewDeclaration()).append("</declaration>").eol(); 516 } 517 518 lowerIndent(); 519 indent().append("</new-constructors>").eol(); 520 } 521 522 if (newMethods.size() != 0) { 523 indent().append("<new-methods>").eol(); 524 raiseIndent(); 525 526 Iterator i = newMethods.iterator(); 527 while (i.hasNext()) { 528 FeatureDifferences fd = (FeatureDifferences) i.next(); 529 indent().append("<declaration").append(breakdownDeclaration((Method_info) fd.getNewFeature())).append(">").append(fd.getNewDeclaration()).append("</declaration>").eol(); 530 } 531 532 lowerIndent(); 533 indent().append("</new-methods>").eol(); 534 } 535 536 lowerIndent(); 537 indent().append("</class>").eol(); 538 539 return super.toString(); 540 } 541 542 private static final String breakdownDeclaration(Classfile element) { 543 StringBuffer result = new StringBuffer (); 544 545 if (element != null) { 546 if (element.isPublic()) result.append(" visibility=\"public\""); 547 if (element.isPackage()) result.append(" visibility=\"package\""); 548 if (element.isFinal()) result.append(" final=\"yes\""); 549 if (element.isSuper()) result.append(" super=\"yes\""); 550 if (element.isSynthetic()) result.append(" synthetic=\"yes\""); 551 if (element.isDeprecated()) result.append(" deprecated=\"yes\""); 552 553 result.append(" name=\"").append(element.getClassName()).append("\""); 554 555 if (element.isInterface()) { 556 result.append(" interface=\"yes\""); 557 558 result.append(" extends=\""); 559 Iterator i = element.getAllInterfaces().iterator(); 560 while (i.hasNext()) { 561 result.append(i.next()); 562 if (i.hasNext()) { 563 result.append(", "); 564 } 565 } 566 result.append("\""); 567 } else { 568 if (element.isAbstract()) result.append(" abstract=\"yes\""); 569 570 result.append(" extends=\"").append(element.getSuperclassName()).append("\""); 571 572 result.append(" implements=\""); 573 Iterator i = element.getAllInterfaces().iterator(); 574 while (i.hasNext()) { 575 result.append(i.next()); 576 if (i.hasNext()) { 577 result.append(", "); 578 } 579 } 580 result.append("\""); 581 } 582 } 583 584 return result.toString(); 585 } 586 587 private static final String breakdownDeclaration(Field_info element) { 588 StringBuffer result = new StringBuffer (); 589 590 if (element != null) { 591 if (element.isPublic()) result.append(" visibility=\"public\""); 592 if (element.isProtected()) result.append(" visibility=\"protected\""); 593 if (element.isPackage()) result.append(" visibility=\"package\""); 594 if (element.isPrivate()) result.append(" visibility=\"private\""); 595 if (element.isStatic()) result.append(" static=\"yes\""); 596 if (element.isFinal()) result.append(" final=\"yes\""); 597 if (element.isVolatile()) result.append(" volatile=\"yes\""); 598 if (element.isTransient()) result.append(" transient=\"yes\""); 599 if (element.isSynthetic()) result.append(" synthetic=\"yes\""); 600 if (element.isDeprecated()) result.append(" deprecated=\"yes\""); 601 602 result.append(" type=\"").append(element.getType()).append("\""); 603 result.append(" name=\"").append(element.getName()).append("\""); 604 result.append(" signature=\"").append(element.getSignature()).append("\""); 605 result.append(" full-signature=\"").append(element.getFullSignature()).append("\""); 606 } 607 608 return result.toString(); 609 } 610 611 private static String breakdownDeclaration(Method_info element) { 612 StringBuffer result = new StringBuffer (); 613 614 if (element != null) { 615 if (element.isPublic()) result.append(" visibility=\"public\""); 616 if (element.isProtected()) result.append(" visibility=\"protected\""); 617 if (element.isPackage()) result.append(" visibility=\"package\""); 618 if (element.isPrivate()) result.append(" visibility=\"private\""); 619 if (element.isStatic()) result.append(" static=\"yes\""); 620 if (element.isFinal()) result.append(" final=\"yes\""); 621 if (element.isSynchronized()) result.append(" synchronized=\"yes\""); 622 if (element.isNative()) result.append(" native=\"yes\""); 623 if (element.isAbstract()) result.append(" abstract=\"yes\""); 624 if (element.isStrict()) result.append(" strict=\"yes\""); 625 if (element.isSynthetic()) result.append(" synthetic=\"yes\""); 626 if (element.isDeprecated()) result.append(" deprecated=\"yes\""); 627 628 if (!element.getName().equals("<init>") && !element.getName().equals("<clinit>")) { 629 result.append(" return-type=\"").append(element.getReturnType()).append("\""); 630 } 631 632 result.append(" signature=\"").append(element.getSignature()).append("\""); 633 result.append(" full-signature=\"").append(element.getFullSignature()).append("\""); 634 635 result.append(" throws=\""); 636 Iterator i = element.getExceptions().iterator(); 637 while (i.hasNext()) { 638 result.append(i.next()); 639 if (i.hasNext()) { 640 result.append(", "); 641 } 642 } 643 result.append("\""); 644 } 645 646 return result.toString(); 647 } 648 649 public int compareTo(Object other) { 650 int result = 0; 651 652 if (other instanceof ClassReport) { 653 result = differences.compareTo(((ClassReport) other).differences); 654 } else { 655 throw new ClassCastException ("Unable to compare ClassReport to " + other.getClass().getName()); 656 } 657 658 return result; 659 } 660 } 661 | Popular Tags |