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.

Code.java 12KB

14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. package org.aspectj.apache.bcel.classfile;
  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.io.DataInputStream;
  56. import java.io.DataOutputStream;
  57. import java.io.IOException;
  58. import org.aspectj.apache.bcel.Constants;
  59. /**
  60. * This class represents a chunk of Java byte code contained in a method. It is instantiated by the
  61. * <em>Attribute.readAttribute()</em> method. A <em>Code</em> attribute contains informations about operand stack, local variables,
  62. * byte code and the exceptions handled within this method.
  63. *
  64. * This attribute has attributes itself, namely <em>LineNumberTable</em> which is used for debugging purposes and
  65. * <em>LocalVariableTable</em> which contains information about the local variables.
  66. *
  67. * @version $Id: Code.java,v 1.9 2009/10/05 17:35:36 aclement Exp $
  68. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  69. * @see Attribute
  70. * @see CodeException
  71. * @see LineNumberTable
  72. * @see LocalVariableTable
  73. */
  74. public final class Code extends Attribute {
  75. private int maxStack; // Maximum size of stack used by this method
  76. private int maxLocals; // Number of local variables
  77. private byte[] code; // Actual byte code
  78. private CodeException[] exceptionTable;
  79. private Attribute[] attributes;
  80. private static final CodeException[] NO_EXCEPTIONS = new CodeException[] {};
  81. /**
  82. * Initialize from another object. Note that both objects use the same references (shallow copy). Use copy() for a physical
  83. * copy.
  84. */
  85. public Code(Code c) {
  86. this(c.getNameIndex(), c.getLength(), c.getMaxStack(), c.getMaxLocals(), c.getCode(), c.getExceptionTable(), c
  87. .getAttributes(), c.getConstantPool());
  88. }
  89. Code(int name_index, int length, DataInputStream file, ConstantPool constant_pool) throws IOException {
  90. // Initialize with some default values which will be overwritten later
  91. this(name_index, length, file.readUnsignedShort(), file.readUnsignedShort(), (byte[]) null, (CodeException[]) null,
  92. (Attribute[]) null, constant_pool);
  93. int len = file.readInt();
  94. code = new byte[len]; // Read byte code
  95. file.readFully(code);
  96. /*
  97. * Read exception table that contains all regions where an exception handler is active, i.e., a try { ... } catch() block.
  98. */
  99. len = file.readUnsignedShort();
  100. if (len == 0) {
  101. exceptionTable = NO_EXCEPTIONS;
  102. } else {
  103. exceptionTable = new CodeException[len];
  104. for (int i = 0; i < len; i++) {
  105. exceptionTable[i] = new CodeException(file);
  106. }
  107. }
  108. // Read all attributes, eg: LineNumberTable, LocalVariableTable
  109. attributes = AttributeUtils.readAttributes(file, constant_pool);
  110. /*
  111. * Adjust length, because of setAttributes in this(), s.b. length is incorrect, because it didn't take the internal
  112. * attributes into account yet! Very subtle bug, fixed in 3.1.1.
  113. */
  114. this.length = length;
  115. }
  116. /**
  117. * @param name_index Index pointing to the name <em>Code</em>
  118. * @param length Content length in bytes
  119. * @param max_stack Maximum size of stack
  120. * @param max_locals Number of local variables
  121. * @param code Actual byte code
  122. * @param exception_table Table of handled exceptions
  123. * @param attributes Attributes of code: LineNumber or LocalVariable
  124. * @param constant_pool Array of constants
  125. */
  126. public Code(int name_index, int length, int max_stack, int max_locals, byte[] code, CodeException[] exception_table,
  127. Attribute[] attributes, ConstantPool constant_pool) {
  128. super(Constants.ATTR_CODE, name_index, length, constant_pool);
  129. this.maxStack = max_stack;
  130. this.maxLocals = max_locals;
  131. setCode(code);
  132. setExceptionTable(exception_table);
  133. setAttributes(attributes); // Overwrites length!
  134. }
  135. /**
  136. * Called by objects that are traversing the nodes of the tree implicitely defined by the contents of a Java class. I.e., the
  137. * hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
  138. *
  139. * @param v Visitor object
  140. */
  141. @Override
  142. public void accept(ClassVisitor v) {
  143. v.visitCode(this);
  144. }
  145. /**
  146. * Dump code attribute to file stream in binary format.
  147. *
  148. * @param file Output file stream
  149. * @throws IOException
  150. */
  151. @Override
  152. public final void dump(DataOutputStream file) throws IOException {
  153. super.dump(file);
  154. file.writeShort(maxStack);
  155. file.writeShort(maxLocals);
  156. file.writeInt(code.length);
  157. file.write(code, 0, code.length);
  158. file.writeShort(exceptionTable.length);
  159. for (CodeException e : exceptionTable) {
  160. e.dump(file);
  161. }
  162. file.writeShort(attributes.length);
  163. for (Attribute attribute : attributes) {
  164. attribute.dump(file);
  165. }
  166. }
  167. /**
  168. * @return Collection of code attributes.
  169. * @see Attribute
  170. */
  171. public final Attribute[] getAttributes() {
  172. return attributes;
  173. }
  174. /**
  175. * @return LineNumberTable of Code, if it has one
  176. */
  177. public LineNumberTable getLineNumberTable() {
  178. for (Attribute attribute : attributes) {
  179. if (attribute.tag == Constants.ATTR_LINE_NUMBER_TABLE) {
  180. return (LineNumberTable) attribute;
  181. }
  182. }
  183. return null;
  184. }
  185. /**
  186. * @return LocalVariableTable of Code, if it has one
  187. */
  188. public LocalVariableTable getLocalVariableTable() {
  189. for (Attribute attribute : attributes) {
  190. if (attribute.tag == Constants.ATTR_LOCAL_VARIABLE_TABLE) {
  191. return (LocalVariableTable) attribute;
  192. }
  193. }
  194. return null;
  195. }
  196. /**
  197. * @return Actual byte code of the method.
  198. */
  199. public final byte[] getCode() {
  200. return code;
  201. }
  202. /**
  203. * @return Table of handled exceptions.
  204. * @see CodeException
  205. */
  206. public final CodeException[] getExceptionTable() {
  207. return exceptionTable;
  208. }
  209. /**
  210. * @return Number of local variables.
  211. */
  212. public final int getMaxLocals() {
  213. return maxLocals;
  214. }
  215. /**
  216. * @return Maximum size of stack used by this method.
  217. */
  218. public final int getMaxStack() {
  219. return maxStack;
  220. }
  221. /**
  222. * @return the internal length of this code attribute (minus the first 6 bytes) and excluding all its attributes
  223. */
  224. private final int getInternalLength() {
  225. return 2 /* max_stack */+ 2 /* max_locals */+ 4 /* code length */
  226. + (code == null ? 0 : code.length) /* byte-code */
  227. + 2 /* exception-table length */
  228. + 8 * (exceptionTable == null ? 0 : exceptionTable.length) /* exception table */
  229. + 2 /* attributes count */;
  230. }
  231. /**
  232. * @return the full size of this code attribute, minus its first 6 bytes, including the size of all its contained attributes
  233. */
  234. private final int calculateLength() {
  235. int len = 0;
  236. if (attributes != null) {
  237. for (Attribute attribute : attributes) {
  238. len += attribute.length + 6 /* attribute header size */;
  239. }
  240. }
  241. return len + getInternalLength();
  242. }
  243. /**
  244. * @param attributes.
  245. */
  246. public final void setAttributes(Attribute[] attributes) {
  247. this.attributes = attributes;
  248. length = calculateLength(); // Adjust length
  249. }
  250. /**
  251. * @param code byte code
  252. */
  253. public final void setCode(byte[] code) {
  254. this.code = code;
  255. }
  256. /**
  257. * @param exception_table exception table
  258. */
  259. public final void setExceptionTable(CodeException[] exception_table) {
  260. this.exceptionTable = exception_table;
  261. }
  262. /**
  263. * @param max_locals maximum number of local variables
  264. */
  265. public final void setMaxLocals(int max_locals) {
  266. this.maxLocals = max_locals;
  267. }
  268. /**
  269. * @param max_stack maximum stack size
  270. */
  271. public final void setMaxStack(int max_stack) {
  272. this.maxStack = max_stack;
  273. }
  274. /**
  275. * @return String representation of code chunk.
  276. */
  277. public final String toString(boolean verbose) {
  278. StringBuilder buf;
  279. buf = new StringBuilder("Code(max_stack = " + maxStack + ", max_locals = " + maxLocals + ", code_length = " + code.length
  280. + ")\n" + Utility.codeToString(code, cpool, 0, -1, verbose));
  281. if (exceptionTable.length > 0) {
  282. buf.append("\nException handler(s) = \n" + "From\tTo\tHandler\tType\n");
  283. for (CodeException e : exceptionTable) {
  284. buf.append(e.toString(cpool, verbose) + "\n");
  285. }
  286. }
  287. if (attributes.length > 0) {
  288. buf.append("\nAttribute(s) = \n");
  289. for (Attribute attribute : attributes) {
  290. buf.append(attribute.toString() + "\n");
  291. }
  292. }
  293. return buf.toString();
  294. }
  295. /**
  296. * @return String representation of code chunk.
  297. */
  298. @Override
  299. public final String toString() {
  300. return toString(true);
  301. }
  302. // /**
  303. // * @return deep copy of this attribute
  304. // */
  305. // public Attribute copy(ConstantPool constant_pool) {
  306. // Code c = (Code)clone();
  307. // c.code = (byte[])code.clone();
  308. // c.cpool = constant_pool;
  309. //
  310. // c.exceptionTable = new CodeException[exceptionTable.length];
  311. // for(int i=0; i < exceptionTable.length; i++)
  312. // c.exceptionTable[i] = exceptionTable[i].copy();
  313. //
  314. // c.attributes = new Attribute[attributes.length];
  315. // for(int i=0; i < attributes.length; i++)
  316. // c.attributes[i] = attributes[i].copy(constant_pool);
  317. //
  318. // return c;
  319. // }
  320. /**
  321. * Returns the same as toString(true) except that the attribute information isn't included (line numbers). Can be used to check
  322. * whether two pieces of code are equivalent.
  323. */
  324. public String getCodeString() {
  325. StringBuilder codeString = new StringBuilder();
  326. codeString.append("Code(max_stack = ").append(maxStack);
  327. codeString.append(", max_locals = ").append(maxLocals);
  328. codeString.append(", code_length = ").append(code.length).append(")\n");
  329. codeString.append(Utility.codeToString(code, cpool, 0, -1, true));
  330. if (exceptionTable.length > 0) {
  331. codeString.append("\n").append("Exception entries = ").append(exceptionTable.length).append("\n");
  332. for (CodeException exc : exceptionTable) {
  333. int type = exc.getCatchType();
  334. String name = "finally";
  335. if (type != 0) {
  336. name = this.cpool.getConstantString(type, Constants.CONSTANT_Class);
  337. }
  338. codeString.append(name).append("[");
  339. codeString.append(exc.getStartPC()).append(">").append(exc.getEndPC()).append("]\n");
  340. }
  341. }
  342. return codeString.toString();
  343. }
  344. }