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

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