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 14KB

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