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

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