1. /*
  2. * @(#)DeclarationScanner.java 1.5 04/04/20
  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. /**
  10. * A visitor for declarations that scans declarations contained within
  11. * the given declaration. For example, when visiting a class, the
  12. * methods, fields, constructors, and nested types of the class are
  13. * also visited.
  14. *
  15. * <p> To control the processing done on a declaration, users of this
  16. * class pass in their own visitors for pre and post processing. The
  17. * preprocessing visitor is called before the contained declarations
  18. * are scanned; the postprocessing visitor is called after the
  19. * contained declarations are scanned.
  20. *
  21. * @author Joseph D. Darcy
  22. * @author Scott Seligman
  23. * @version 1.5 04/04/20
  24. * @since 1.5
  25. */
  26. class DeclarationScanner implements DeclarationVisitor {
  27. protected DeclarationVisitor pre;
  28. protected DeclarationVisitor post;
  29. DeclarationScanner(DeclarationVisitor pre, DeclarationVisitor post) {
  30. this.pre = pre;
  31. this.post = post;
  32. }
  33. /**
  34. * Visits a declaration.
  35. *
  36. * @param d the declaration to visit
  37. */
  38. public void visitDeclaration(Declaration d) {
  39. d.accept(pre);
  40. d.accept(post);
  41. }
  42. /**
  43. * Visits a package declaration.
  44. *
  45. * @param d the declaration to visit
  46. */
  47. public void visitPackageDeclaration(PackageDeclaration d) {
  48. d.accept(pre);
  49. for(ClassDeclaration classDecl: d.getClasses()) {
  50. classDecl.accept(this);
  51. }
  52. for(InterfaceDeclaration interfaceDecl: d.getInterfaces()) {
  53. interfaceDecl.accept(this);
  54. }
  55. d.accept(post);
  56. }
  57. /**
  58. * Visits a member or constructor declaration.
  59. *
  60. * @param d the declaration to visit
  61. */
  62. public void visitMemberDeclaration(MemberDeclaration d) {
  63. visitDeclaration(d);
  64. }
  65. /**
  66. * Visits a type declaration.
  67. *
  68. * @param d the declaration to visit
  69. */
  70. public void visitTypeDeclaration(TypeDeclaration d) {
  71. d.accept(pre);
  72. for(TypeParameterDeclaration tpDecl: d.getFormalTypeParameters()) {
  73. tpDecl.accept(this);
  74. }
  75. for(FieldDeclaration fieldDecl: d.getFields()) {
  76. fieldDecl.accept(this);
  77. }
  78. for(MethodDeclaration methodDecl: d.getMethods()) {
  79. methodDecl.accept(this);
  80. }
  81. for(TypeDeclaration typeDecl: d.getNestedTypes()) {
  82. typeDecl.accept(this);
  83. }
  84. d.accept(post);
  85. }
  86. /**
  87. * Visits a class declaration.
  88. *
  89. * @param d the declaration to visit
  90. */
  91. public void visitClassDeclaration(ClassDeclaration d) {
  92. d.accept(pre);
  93. for(TypeParameterDeclaration tpDecl: d.getFormalTypeParameters()) {
  94. tpDecl.accept(this);
  95. }
  96. for(FieldDeclaration fieldDecl: d.getFields()) {
  97. fieldDecl.accept(this);
  98. }
  99. for(MethodDeclaration methodDecl: d.getMethods()) {
  100. methodDecl.accept(this);
  101. }
  102. for(TypeDeclaration typeDecl: d.getNestedTypes()) {
  103. typeDecl.accept(this);
  104. }
  105. for(ConstructorDeclaration ctorDecl: d.getConstructors()) {
  106. ctorDecl.accept(this);
  107. }
  108. d.accept(post);
  109. }
  110. /**
  111. * Visits an enum declaration.
  112. *
  113. * @param d the declaration to visit
  114. */
  115. public void visitEnumDeclaration(EnumDeclaration d) {
  116. visitClassDeclaration(d);
  117. }
  118. /**
  119. * Visits an interface declaration.
  120. *
  121. * @param d the declaration to visit
  122. */
  123. public void visitInterfaceDeclaration(InterfaceDeclaration d) {
  124. visitTypeDeclaration(d);
  125. }
  126. /**
  127. * Visits an annotation type declaration.
  128. *
  129. * @param d the declaration to visit
  130. */
  131. public void visitAnnotationTypeDeclaration(AnnotationTypeDeclaration d) {
  132. visitInterfaceDeclaration(d);
  133. }
  134. /**
  135. * Visits a field declaration.
  136. *
  137. * @param d the declaration to visit
  138. */
  139. public void visitFieldDeclaration(FieldDeclaration d) {
  140. visitMemberDeclaration(d);
  141. }
  142. /**
  143. * Visits an enum constant declaration.
  144. *
  145. * @param d the declaration to visit
  146. */
  147. public void visitEnumConstantDeclaration(EnumConstantDeclaration d) {
  148. visitFieldDeclaration(d);
  149. }
  150. /**
  151. * Visits a method or constructor declaration.
  152. *
  153. * @param d the declaration to visit
  154. */
  155. public void visitExecutableDeclaration(ExecutableDeclaration d) {
  156. d.accept(pre);
  157. for(TypeParameterDeclaration tpDecl: d.getFormalTypeParameters()) {
  158. tpDecl.accept(this);
  159. }
  160. for(ParameterDeclaration pDecl: d.getParameters()) {
  161. pDecl.accept(this);
  162. }
  163. d.accept(post);
  164. }
  165. /**
  166. * Visits a constructor declaration.
  167. *
  168. * @param d the declaration to visit
  169. */
  170. public void visitConstructorDeclaration(ConstructorDeclaration d) {
  171. visitExecutableDeclaration(d);
  172. }
  173. /**
  174. * Visits a method declaration.
  175. *
  176. * @param d the declaration to visit
  177. */
  178. public void visitMethodDeclaration(MethodDeclaration d) {
  179. visitExecutableDeclaration(d);
  180. }
  181. /**
  182. * Visits an annotation type element declaration.
  183. *
  184. * @param d the declaration to visit
  185. */
  186. public void visitAnnotationTypeElementDeclaration(
  187. AnnotationTypeElementDeclaration d) {
  188. visitMethodDeclaration(d);
  189. }
  190. /**
  191. * Visits a parameter declaration.
  192. *
  193. * @param d the declaration to visit
  194. */
  195. public void visitParameterDeclaration(ParameterDeclaration d) {
  196. visitDeclaration(d);
  197. }
  198. /**
  199. * Visits a type parameter declaration.
  200. *
  201. * @param d the declaration to visit
  202. */
  203. public void visitTypeParameterDeclaration(TypeParameterDeclaration d) {
  204. visitDeclaration(d);
  205. }
  206. }