1. /*
  2. * @(#)SourceOrderDeclScanner.java 1.4 04/05/03
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.mirror.util;
  8. import com.sun.mirror.declaration.*;
  9. import java.util.SortedSet;
  10. import java.util.TreeSet;
  11. /**
  12. * A visitor for declarations that scans declarations contained within
  13. * the given declaration in source code order. For example, when
  14. * visiting a class, the methods, fields, constructors, and nested
  15. * types of the class are also visited.
  16. *
  17. * To control the processing done on a declaration, users of this
  18. * class pass in their own visitors for pre and post processing. The
  19. * preprocessing visitor is called before the contained declarations
  20. * are scanned; the postprocessing visitor is called after the
  21. * contained declarations are scanned.
  22. *
  23. * @author Joseph D. Darcy
  24. * @author Scott Seligman
  25. * @version 1.4 04/05/03
  26. * @since 1.5
  27. */
  28. class SourceOrderDeclScanner extends DeclarationScanner {
  29. static class SourceOrderComparator implements java.util.Comparator<Declaration> {
  30. SourceOrderComparator(){}
  31. public int compare(Declaration d1, Declaration d2) {
  32. if (d1 == d2)
  33. return 0;
  34. SourcePosition p1 = d1.getPosition();
  35. SourcePosition p2 = d2.getPosition();
  36. if (p1 == null)
  37. return (p2 == null) ?0:1 ;
  38. else {
  39. if (p2 == null)
  40. return -1;
  41. int fileComp = p1.file().compareTo(p2.file()) ;
  42. if (fileComp == 0) {
  43. long diff = (long)p1.line() - (long)p2.line();
  44. if (diff == 0) {
  45. diff = Long.signum((long)p1.column() - (long)p2.column());
  46. if (diff != 0)
  47. return (int)diff;
  48. else {
  49. // declarations may be two
  50. // compiler-generated members with the
  51. // same source position
  52. return (int)( Long.signum((long)System.identityHashCode(d1) -
  53. (long)System.identityHashCode(d2)));
  54. }
  55. } else
  56. return (diff<0)? -1:1;
  57. } else
  58. return fileComp;
  59. }
  60. }
  61. }
  62. final static java.util.Comparator<Declaration> comparator = new SourceOrderComparator();
  63. SourceOrderDeclScanner(DeclarationVisitor pre, DeclarationVisitor post) {
  64. super(pre, post);
  65. }
  66. /**
  67. * Visits a type declaration.
  68. *
  69. * @param d the declaration to visit
  70. */
  71. public void visitTypeDeclaration(TypeDeclaration d) {
  72. d.accept(pre);
  73. SortedSet<Declaration> decls = new
  74. TreeSet<Declaration>(SourceOrderDeclScanner.comparator) ;
  75. for(TypeParameterDeclaration tpDecl: d.getFormalTypeParameters()) {
  76. decls.add(tpDecl);
  77. }
  78. for(FieldDeclaration fieldDecl: d.getFields()) {
  79. decls.add(fieldDecl);
  80. }
  81. for(MethodDeclaration methodDecl: d.getMethods()) {
  82. decls.add(methodDecl);
  83. }
  84. for(TypeDeclaration typeDecl: d.getNestedTypes()) {
  85. decls.add(typeDecl);
  86. }
  87. for(Declaration decl: decls )
  88. decl.accept(this);
  89. d.accept(post);
  90. }
  91. /**
  92. * Visits a class declaration.
  93. *
  94. * @param d the declaration to visit
  95. */
  96. public void visitClassDeclaration(ClassDeclaration d) {
  97. d.accept(pre);
  98. SortedSet<Declaration> decls = new
  99. TreeSet<Declaration>(SourceOrderDeclScanner.comparator) ;
  100. for(TypeParameterDeclaration tpDecl: d.getFormalTypeParameters()) {
  101. decls.add(tpDecl);
  102. }
  103. for(FieldDeclaration fieldDecl: d.getFields()) {
  104. decls.add(fieldDecl);
  105. }
  106. for(MethodDeclaration methodDecl: d.getMethods()) {
  107. decls.add(methodDecl);
  108. }
  109. for(TypeDeclaration typeDecl: d.getNestedTypes()) {
  110. decls.add(typeDecl);
  111. }
  112. for(ConstructorDeclaration ctorDecl: d.getConstructors()) {
  113. decls.add(ctorDecl);
  114. }
  115. for(Declaration decl: decls )
  116. decl.accept(this);
  117. d.accept(post);
  118. }
  119. public void visitExecutableDeclaration(ExecutableDeclaration d) {
  120. d.accept(pre);
  121. SortedSet<Declaration> decls = new
  122. TreeSet<Declaration>(SourceOrderDeclScanner.comparator) ;
  123. for(TypeParameterDeclaration tpDecl: d.getFormalTypeParameters())
  124. decls.add(tpDecl);
  125. for(ParameterDeclaration pDecl: d.getParameters())
  126. decls.add(pDecl);
  127. for(Declaration decl: decls )
  128. decl.accept(this);
  129. d.accept(post);
  130. }
  131. }