You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DescendingVisitor.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. package org.aspectj.apache.bcel.verifier;
  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. import org.aspectj.apache.bcel.classfile.AnnotationDefault;
  57. import org.aspectj.apache.bcel.classfile.Attribute;
  58. import org.aspectj.apache.bcel.classfile.AttributeUtils;
  59. import org.aspectj.apache.bcel.classfile.Code;
  60. import org.aspectj.apache.bcel.classfile.CodeException;
  61. import org.aspectj.apache.bcel.classfile.Constant;
  62. import org.aspectj.apache.bcel.classfile.ConstantClass;
  63. import org.aspectj.apache.bcel.classfile.ConstantDouble;
  64. import org.aspectj.apache.bcel.classfile.ConstantFieldref;
  65. import org.aspectj.apache.bcel.classfile.ConstantFloat;
  66. import org.aspectj.apache.bcel.classfile.ConstantInteger;
  67. import org.aspectj.apache.bcel.classfile.ConstantInterfaceMethodref;
  68. import org.aspectj.apache.bcel.classfile.ConstantLong;
  69. import org.aspectj.apache.bcel.classfile.ConstantMethodref;
  70. import org.aspectj.apache.bcel.classfile.ConstantNameAndType;
  71. import org.aspectj.apache.bcel.classfile.ConstantPool;
  72. import org.aspectj.apache.bcel.classfile.ConstantString;
  73. import org.aspectj.apache.bcel.classfile.ConstantUtf8;
  74. import org.aspectj.apache.bcel.classfile.ConstantValue;
  75. import org.aspectj.apache.bcel.classfile.Deprecated;
  76. import org.aspectj.apache.bcel.classfile.EnclosingMethod;
  77. import org.aspectj.apache.bcel.classfile.ExceptionTable;
  78. import org.aspectj.apache.bcel.classfile.Field;
  79. import org.aspectj.apache.bcel.classfile.InnerClass;
  80. import org.aspectj.apache.bcel.classfile.InnerClasses;
  81. import org.aspectj.apache.bcel.classfile.JavaClass;
  82. import org.aspectj.apache.bcel.classfile.LineNumber;
  83. import org.aspectj.apache.bcel.classfile.LineNumberTable;
  84. import org.aspectj.apache.bcel.classfile.LocalVariable;
  85. import org.aspectj.apache.bcel.classfile.LocalVariableTable;
  86. import org.aspectj.apache.bcel.classfile.LocalVariableTypeTable;
  87. import org.aspectj.apache.bcel.classfile.Method;
  88. import org.aspectj.apache.bcel.classfile.Signature;
  89. import org.aspectj.apache.bcel.classfile.SourceFile;
  90. import org.aspectj.apache.bcel.classfile.StackMap;
  91. import org.aspectj.apache.bcel.classfile.StackMapEntry;
  92. import org.aspectj.apache.bcel.classfile.Synthetic;
  93. import org.aspectj.apache.bcel.classfile.Unknown;
  94. import org.aspectj.apache.bcel.classfile.ClassVisitor;
  95. import org.aspectj.apache.bcel.classfile.annotation.RuntimeInvisAnnos;
  96. import org.aspectj.apache.bcel.classfile.annotation.RuntimeInvisParamAnnos;
  97. import org.aspectj.apache.bcel.classfile.annotation.RuntimeVisAnnos;
  98. import org.aspectj.apache.bcel.classfile.annotation.RuntimeVisParamAnnos;
  99. /**
  100. * Traverses a JavaClass with another Visitor object 'piggy-backed'
  101. * that is applied to all components of a JavaClass object. I.e. this
  102. * class supplies the traversal strategy, other classes can make use
  103. * of it.
  104. *
  105. * @version $Id: DescendingVisitor.java,v 1.4 2009/09/15 19:40:22 aclement Exp $
  106. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  107. */
  108. public class DescendingVisitor implements ClassVisitor {
  109. private JavaClass clazz;
  110. private ClassVisitor visitor;
  111. private Stack<Object> stack = new Stack<Object>();
  112. /** @return container of current entitity, i.e., predecessor during traversal
  113. */
  114. public Object predecessor() {
  115. return predecessor(0);
  116. }
  117. /**
  118. * @param level nesting level, i.e., 0 returns the direct predecessor
  119. * @return container of current entitity, i.e., predecessor during traversal
  120. */
  121. public Object predecessor(int level) {
  122. int size = stack.size();
  123. if((size < 2) || (level < 0))
  124. return null;
  125. else
  126. return stack.elementAt(size - (level + 2)); // size - 1 == current
  127. }
  128. /** @return current object
  129. */
  130. public Object current() {
  131. return stack.peek();
  132. }
  133. /**
  134. * @param clazz Class to traverse
  135. * @param visitor visitor object to apply to all components
  136. */
  137. public DescendingVisitor(JavaClass clazz, ClassVisitor visitor) {
  138. this.clazz = clazz;
  139. this.visitor = visitor;
  140. }
  141. /**
  142. * Start traversal.
  143. */
  144. public void visit() { clazz.accept(this); }
  145. public void visitJavaClass(JavaClass clazz) {
  146. stack.push(clazz);
  147. clazz.accept(visitor);
  148. Field[] fields = clazz.getFields();
  149. for(int i=0; i < fields.length; i++)
  150. fields[i].accept(this);
  151. Method[] methods = clazz.getMethods();
  152. for(int i=0; i < methods.length; i++)
  153. methods[i].accept(this);
  154. AttributeUtils.accept(clazz.getAttributes(),visitor);
  155. // clazz.getAttributes().accept(this);
  156. clazz.getConstantPool().accept(this);
  157. stack.pop();
  158. }
  159. public void visitField(Field field) {
  160. stack.push(field);
  161. field.accept(visitor);
  162. AttributeUtils.accept(field.getAttributes(),visitor);
  163. // field.getAttributes().accept(this);
  164. stack.pop();
  165. }
  166. public void visitConstantValue(ConstantValue cv) {
  167. stack.push(cv);
  168. cv.accept(visitor);
  169. stack.pop();
  170. }
  171. public void visitMethod(Method method) {
  172. stack.push(method);
  173. method.accept(visitor);
  174. AttributeUtils.accept(method.getAttributes(),visitor);
  175. stack.pop();
  176. }
  177. public void visitExceptionTable(ExceptionTable table) {
  178. stack.push(table);
  179. table.accept(visitor);
  180. stack.pop();
  181. }
  182. public void visitCode(Code code) {
  183. stack.push(code);
  184. code.accept(visitor);
  185. CodeException[] table = code.getExceptionTable();
  186. for(int i=0; i < table.length; i++)
  187. table[i].accept(this);
  188. Attribute[] attributes = code.getAttributes();
  189. for(int i=0; i < attributes.length; i++)
  190. attributes[i].accept(this);
  191. stack.pop();
  192. }
  193. public void visitCodeException(CodeException ce) {
  194. stack.push(ce);
  195. ce.accept(visitor);
  196. stack.pop();
  197. }
  198. public void visitLineNumberTable(LineNumberTable table) {
  199. stack.push(table);
  200. table.accept(visitor);
  201. LineNumber[] numbers = table.getLineNumberTable();
  202. for(int i=0; i < numbers.length; i++)
  203. numbers[i].accept(this);
  204. stack.pop();
  205. }
  206. public void visitLineNumber(LineNumber number) {
  207. stack.push(number);
  208. number.accept(visitor);
  209. stack.pop();
  210. }
  211. public void visitLocalVariableTable(LocalVariableTable table) {
  212. stack.push(table);
  213. table.accept(visitor);
  214. LocalVariable[] vars = table.getLocalVariableTable();
  215. for(int i=0; i < vars.length; i++)
  216. vars[i].accept(this);
  217. stack.pop();
  218. }
  219. public void visitStackMap(StackMap table) {
  220. stack.push(table);
  221. table.accept(visitor);
  222. StackMapEntry[] vars = table.getStackMap();
  223. for(int i=0; i < vars.length; i++)
  224. vars[i].accept(this);
  225. stack.pop();
  226. }
  227. public void visitStackMapEntry(StackMapEntry var) {
  228. stack.push(var);
  229. var.accept(visitor);
  230. stack.pop();
  231. }
  232. public void visitLocalVariable(LocalVariable var) {
  233. stack.push(var);
  234. var.accept(visitor);
  235. stack.pop();
  236. }
  237. public void visitConstantPool(ConstantPool cp) {
  238. stack.push(cp);
  239. cp.accept(visitor);
  240. Constant[] constants = cp.getConstantPool();
  241. for(int i=1; i < constants.length; i++) {
  242. if(constants[i] != null)
  243. constants[i].accept(this);
  244. }
  245. stack.pop();
  246. }
  247. public void visitConstantClass(ConstantClass constant) {
  248. stack.push(constant);
  249. constant.accept(visitor);
  250. stack.pop();
  251. }
  252. public void visitConstantDouble(ConstantDouble constant) {
  253. stack.push(constant);
  254. constant.accept(visitor);
  255. stack.pop();
  256. }
  257. public void visitConstantFieldref(ConstantFieldref constant) {
  258. stack.push(constant);
  259. constant.accept(visitor);
  260. stack.pop();
  261. }
  262. public void visitConstantFloat(ConstantFloat constant) {
  263. stack.push(constant);
  264. constant.accept(visitor);
  265. stack.pop();
  266. }
  267. public void visitConstantInteger(ConstantInteger constant) {
  268. stack.push(constant);
  269. constant.accept(visitor);
  270. stack.pop();
  271. }
  272. public void visitConstantInterfaceMethodref(ConstantInterfaceMethodref constant) {
  273. stack.push(constant);
  274. constant.accept(visitor);
  275. stack.pop();
  276. }
  277. public void visitConstantLong(ConstantLong constant) {
  278. stack.push(constant);
  279. constant.accept(visitor);
  280. stack.pop();
  281. }
  282. public void visitConstantMethodref(ConstantMethodref constant) {
  283. stack.push(constant);
  284. constant.accept(visitor);
  285. stack.pop();
  286. }
  287. public void visitConstantNameAndType(ConstantNameAndType constant) {
  288. stack.push(constant);
  289. constant.accept(visitor);
  290. stack.pop();
  291. }
  292. public void visitConstantString(ConstantString constant) {
  293. stack.push(constant);
  294. constant.accept(visitor);
  295. stack.pop();
  296. }
  297. public void visitConstantUtf8(ConstantUtf8 constant) {
  298. stack.push(constant);
  299. constant.accept(visitor);
  300. stack.pop();
  301. }
  302. public void visitInnerClasses(InnerClasses ic) {
  303. stack.push(ic);
  304. ic.accept(visitor);
  305. InnerClass[] ics = ic.getInnerClasses();
  306. for(int i=0; i < ics.length; i++)
  307. ics[i].accept(this);
  308. stack.pop();
  309. }
  310. public void visitInnerClass(InnerClass inner) {
  311. stack.push(inner);
  312. inner.accept(visitor);
  313. stack.pop();
  314. }
  315. public void visitDeprecated(Deprecated attribute) {
  316. stack.push(attribute);
  317. attribute.accept(visitor);
  318. stack.pop();
  319. }
  320. public void visitSignature(Signature attribute) {
  321. stack.push(attribute);
  322. attribute.accept(visitor);
  323. stack.pop();
  324. }
  325. // J5SUPPORT:
  326. public void visitEnclosingMethod(EnclosingMethod attribute) {
  327. stack.push(attribute);
  328. attribute.accept(visitor);
  329. stack.pop();
  330. }
  331. public void visitRuntimeVisibleAnnotations(RuntimeVisAnnos attribute) {
  332. stack.push(attribute);
  333. attribute.accept(visitor);
  334. stack.pop();
  335. }
  336. public void visitRuntimeInvisibleAnnotations(RuntimeInvisAnnos attribute) {
  337. stack.push(attribute);
  338. attribute.accept(visitor);
  339. stack.pop();
  340. }
  341. public void visitRuntimeVisibleParameterAnnotations(RuntimeVisParamAnnos attribute) {
  342. stack.push(attribute);
  343. attribute.accept(visitor);
  344. stack.pop();
  345. }
  346. public void visitRuntimeInvisibleParameterAnnotations(RuntimeInvisParamAnnos attribute) {
  347. stack.push(attribute);
  348. attribute.accept(visitor);
  349. stack.pop();
  350. }
  351. public void visitAnnotationDefault(AnnotationDefault attribute) {
  352. stack.push(attribute);
  353. attribute.accept(visitor);
  354. stack.pop();
  355. }
  356. public void visitLocalVariableTypeTable(LocalVariableTypeTable table) {
  357. stack.push(table);
  358. table.accept(visitor);
  359. LocalVariable[] vars = table.getLocalVariableTypeTable();
  360. for(int i=0; i < vars.length; i++)
  361. vars[i].accept(this);
  362. stack.pop();
  363. }
  364. public void visitSourceFile(SourceFile attribute) {
  365. stack.push(attribute);
  366. attribute.accept(visitor);
  367. stack.pop();
  368. }
  369. public void visitSynthetic(Synthetic attribute) {
  370. stack.push(attribute);
  371. attribute.accept(visitor);
  372. stack.pop();
  373. }
  374. public void visitUnknown(Unknown attribute) {
  375. stack.push(attribute);
  376. attribute.accept(visitor);
  377. stack.pop();
  378. }
  379. }