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

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