1. package com.sun.org.apache.bcel.internal.classfile;
  2. /* ====================================================================
  3. * The Apache Software License, Version 1.1
  4. *
  5. * Copyright (c) 2001 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Apache" and "Apache Software Foundation" and
  28. * "Apache BCEL" must not be used to endorse or promote products
  29. * derived from this software without prior written permission. For
  30. * written permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * "Apache BCEL", nor may "Apache" appear in their name, without
  34. * prior written permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation. For more
  52. * information on the Apache Software Foundation, please see
  53. * <http://www.apache.org/>.
  54. */
  55. import java.util.Stack;
  56. /**
  57. * Traverses a JavaClass with another Visitor object 'piggy-backed'
  58. * that is applied to all components of a JavaClass object. I.e. this
  59. * class supplies the traversal strategy, other classes can make use
  60. * of it.
  61. *
  62. * @version $Id: DescendingVisitor.java,v 1.1.1.1 2001/10/29 20:00:00 jvanzyl Exp $
  63. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  64. */
  65. public class DescendingVisitor implements Visitor {
  66. private JavaClass clazz;
  67. private Visitor visitor;
  68. private Stack stack = new Stack();
  69. /** @return container of current entitity, i.e., predecessor during traversal
  70. */
  71. public Object predecessor() {
  72. return predecessor(0);
  73. }
  74. /**
  75. * @param level nesting level, i.e., 0 returns the direct predecessor
  76. * @return container of current entitity, i.e., predecessor during traversal
  77. */
  78. public Object predecessor(int level) {
  79. int size = stack.size();
  80. if((size < 2) || (level < 0))
  81. return null;
  82. else
  83. return stack.elementAt(size - (level + 2)); // size - 1 == current
  84. }
  85. /** @return current object
  86. */
  87. public Object current() {
  88. return stack.peek();
  89. }
  90. /**
  91. * @param clazz Class to traverse
  92. * @param visitor visitor object to apply to all components
  93. */
  94. public DescendingVisitor(JavaClass clazz, Visitor visitor) {
  95. this.clazz = clazz;
  96. this.visitor = visitor;
  97. }
  98. /**
  99. * Start traversal.
  100. */
  101. public void visit() { clazz.accept(this); }
  102. public void visitJavaClass(JavaClass clazz) {
  103. stack.push(clazz);
  104. clazz.accept(visitor);
  105. Field[] fields = clazz.getFields();
  106. for(int i=0; i < fields.length; i++)
  107. fields[i].accept(this);
  108. Method[] methods = clazz.getMethods();
  109. for(int i=0; i < methods.length; i++)
  110. methods[i].accept(this);
  111. Attribute[] attributes = clazz.getAttributes();
  112. for(int i=0; i < attributes.length; i++)
  113. attributes[i].accept(this);
  114. clazz.getConstantPool().accept(this);
  115. stack.pop();
  116. }
  117. public void visitField(Field field) {
  118. stack.push(field);
  119. field.accept(visitor);
  120. Attribute[] attributes = field.getAttributes();
  121. for(int i=0; i < attributes.length; i++)
  122. attributes[i].accept(this);
  123. stack.pop();
  124. }
  125. public void visitConstantValue(ConstantValue cv) {
  126. stack.push(cv);
  127. cv.accept(visitor);
  128. stack.pop();
  129. }
  130. public void visitMethod(Method method) {
  131. stack.push(method);
  132. method.accept(visitor);
  133. Attribute[] attributes = method.getAttributes();
  134. for(int i=0; i < attributes.length; i++)
  135. attributes[i].accept(this);
  136. stack.pop();
  137. }
  138. public void visitExceptionTable(ExceptionTable table) {
  139. stack.push(table);
  140. table.accept(visitor);
  141. stack.pop();
  142. }
  143. public void visitCode(Code code) {
  144. stack.push(code);
  145. code.accept(visitor);
  146. CodeException[] table = code.getExceptionTable();
  147. for(int i=0; i < table.length; i++)
  148. table[i].accept(this);
  149. Attribute[] attributes = code.getAttributes();
  150. for(int i=0; i < attributes.length; i++)
  151. attributes[i].accept(this);
  152. stack.pop();
  153. }
  154. public void visitCodeException(CodeException ce) {
  155. stack.push(ce);
  156. ce.accept(visitor);
  157. stack.pop();
  158. }
  159. public void visitLineNumberTable(LineNumberTable table) {
  160. stack.push(table);
  161. table.accept(visitor);
  162. LineNumber[] numbers = table.getLineNumberTable();
  163. for(int i=0; i < numbers.length; i++)
  164. numbers[i].accept(this);
  165. stack.pop();
  166. }
  167. public void visitLineNumber(LineNumber number) {
  168. stack.push(number);
  169. number.accept(visitor);
  170. stack.pop();
  171. }
  172. public void visitLocalVariableTable(LocalVariableTable table) {
  173. stack.push(table);
  174. table.accept(visitor);
  175. LocalVariable[] vars = table.getLocalVariableTable();
  176. for(int i=0; i < vars.length; i++)
  177. vars[i].accept(this);
  178. stack.pop();
  179. }
  180. public void visitStackMap(StackMap table) {
  181. stack.push(table);
  182. table.accept(visitor);
  183. StackMapEntry[] vars = table.getStackMap();
  184. for(int i=0; i < vars.length; i++)
  185. vars[i].accept(this);
  186. stack.pop();
  187. }
  188. public void visitStackMapEntry(StackMapEntry var) {
  189. stack.push(var);
  190. var.accept(visitor);
  191. stack.pop();
  192. }
  193. public void visitLocalVariable(LocalVariable var) {
  194. stack.push(var);
  195. var.accept(visitor);
  196. stack.pop();
  197. }
  198. public void visitConstantPool(ConstantPool cp) {
  199. stack.push(cp);
  200. cp.accept(visitor);
  201. Constant[] constants = cp.getConstantPool();
  202. for(int i=1; i < constants.length; i++) {
  203. if(constants[i] != null)
  204. constants[i].accept(this);
  205. }
  206. stack.pop();
  207. }
  208. public void visitConstantClass(ConstantClass constant) {
  209. stack.push(constant);
  210. constant.accept(visitor);
  211. stack.pop();
  212. }
  213. public void visitConstantDouble(ConstantDouble constant) {
  214. stack.push(constant);
  215. constant.accept(visitor);
  216. stack.pop();
  217. }
  218. public void visitConstantFieldref(ConstantFieldref constant) {
  219. stack.push(constant);
  220. constant.accept(visitor);
  221. stack.pop();
  222. }
  223. public void visitConstantFloat(ConstantFloat constant) {
  224. stack.push(constant);
  225. constant.accept(visitor);
  226. stack.pop();
  227. }
  228. public void visitConstantInteger(ConstantInteger constant) {
  229. stack.push(constant);
  230. constant.accept(visitor);
  231. stack.pop();
  232. }
  233. public void visitConstantInterfaceMethodref(ConstantInterfaceMethodref constant) {
  234. stack.push(constant);
  235. constant.accept(visitor);
  236. stack.pop();
  237. }
  238. public void visitConstantLong(ConstantLong constant) {
  239. stack.push(constant);
  240. constant.accept(visitor);
  241. stack.pop();
  242. }
  243. public void visitConstantMethodref(ConstantMethodref constant) {
  244. stack.push(constant);
  245. constant.accept(visitor);
  246. stack.pop();
  247. }
  248. public void visitConstantNameAndType(ConstantNameAndType constant) {
  249. stack.push(constant);
  250. constant.accept(visitor);
  251. stack.pop();
  252. }
  253. public void visitConstantString(ConstantString constant) {
  254. stack.push(constant);
  255. constant.accept(visitor);
  256. stack.pop();
  257. }
  258. public void visitConstantUtf8(ConstantUtf8 constant) {
  259. stack.push(constant);
  260. constant.accept(visitor);
  261. stack.pop();
  262. }
  263. public void visitInnerClasses(InnerClasses ic) {
  264. stack.push(ic);
  265. ic.accept(visitor);
  266. InnerClass[] ics = ic.getInnerClasses();
  267. for(int i=0; i < ics.length; i++)
  268. ics[i].accept(this);
  269. stack.pop();
  270. }
  271. public void visitInnerClass(InnerClass inner) {
  272. stack.push(inner);
  273. inner.accept(visitor);
  274. stack.pop();
  275. }
  276. public void visitDeprecated(Deprecated attribute) {
  277. stack.push(attribute);
  278. attribute.accept(visitor);
  279. stack.pop();
  280. }
  281. public void visitSourceFile(SourceFile attribute) {
  282. stack.push(attribute);
  283. attribute.accept(visitor);
  284. stack.pop();
  285. }
  286. public void visitSynthetic(Synthetic attribute) {
  287. stack.push(attribute);
  288. attribute.accept(visitor);
  289. stack.pop();
  290. }
  291. public void visitUnknown(Unknown attribute) {
  292. stack.push(attribute);
  293. attribute.accept(visitor);
  294. stack.pop();
  295. }
  296. }