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.

преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 (https://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. * <https://www.apache.org/>.
  54. */
  55. import java.io.ByteArrayInputStream;
  56. import java.io.DataInputStream;
  57. import java.io.DataOutputStream;
  58. import java.io.IOException;
  59. import org.aspectj.apache.bcel.Constants;
  60. /**
  61. * This class represents a table of line numbers for debugging purposes. This attribute is used by the <em>Code</em> attribute. It
  62. * contains pairs of PCs and line numbers.
  63. *
  64. * @version $Id: LineNumberTable.java,v 1.8 2009/09/15 19:40:12 aclement Exp $
  65. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  66. * @see Code changes: asc Feb06 Made unpacking lazy
  67. */
  68. public final class LineNumberTable extends Attribute {
  69. // if 'isInPackedState' then this data needs unpacking
  70. private boolean isInPackedState = false;
  71. private byte[] data; // discarded once unpacked
  72. private int tableLength;
  73. private LineNumber[] table;
  74. /*
  75. * Initialize from another object. Note that both objects use the same references (shallow copy). Use copy() for a physical
  76. * copy.
  77. */
  78. public LineNumberTable(LineNumberTable c) {
  79. this(c.getNameIndex(), c.getLength(), c.getLineNumberTable(), c.getConstantPool());
  80. }
  81. public LineNumberTable(int nameIndex, int length, LineNumber[] lineNumberTable, ConstantPool constantPool) {
  82. super(Constants.ATTR_LINE_NUMBER_TABLE, nameIndex, length, constantPool);
  83. setLineNumberTable(lineNumberTable);
  84. isInPackedState = false;
  85. }
  86. /**
  87. * Construct object from file stream.
  88. *
  89. * @param name_index Index of name
  90. * @param length Content length in bytes
  91. * @param file Input stream
  92. * @throws IOException
  93. * @param constant_pool Array of constants
  94. */
  95. LineNumberTable(int name_index, int length, DataInputStream file, ConstantPool constant_pool) throws IOException {
  96. this(name_index, length, (LineNumber[]) null, constant_pool);
  97. data = new byte[length];
  98. file.readFully(data);
  99. isInPackedState = true;
  100. // assert(bytesRead==length)
  101. }
  102. // Unpacks the byte array into the table
  103. private void unpack() {
  104. if (isInPackedState) {
  105. try {
  106. ByteArrayInputStream bs = new ByteArrayInputStream(data);
  107. DataInputStream dis = new DataInputStream(bs);
  108. tableLength = (dis.readUnsignedShort());
  109. table = new LineNumber[tableLength];
  110. for (int i = 0; i < tableLength; i++) {
  111. table[i] = new LineNumber(dis);
  112. }
  113. dis.close();
  114. data = null; // throw it away now
  115. } catch (IOException e) {
  116. throw new RuntimeException("Unpacking of LineNumberTable attribute failed");
  117. }
  118. isInPackedState = false;
  119. }
  120. }
  121. /**
  122. * Called by objects that are traversing the nodes of the tree implicitely defined by the contents of a Java class. I.e., the
  123. * hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
  124. *
  125. * @param v Visitor object
  126. */
  127. @Override
  128. public void accept(ClassVisitor v) {
  129. unpack();
  130. v.visitLineNumberTable(this);
  131. }
  132. /**
  133. * Dump line number table attribute to file stream in binary format.
  134. *
  135. * @param file Output file stream
  136. * @throws IOException
  137. */
  138. @Override
  139. public final void dump(DataOutputStream file) throws IOException {
  140. super.dump(file);
  141. if (isInPackedState) {
  142. file.write(data);
  143. } else {
  144. file.writeShort(tableLength);
  145. for (int i = 0; i < tableLength; i++) {
  146. table[i].dump(file);
  147. }
  148. }
  149. }
  150. /**
  151. * @return Array of (pc offset, line number) pairs.
  152. */
  153. public final LineNumber[] getLineNumberTable() {
  154. unpack();
  155. return table;
  156. }
  157. /**
  158. * @param line_number_table.
  159. */
  160. public final void setLineNumberTable(LineNumber[] line_number_table) {
  161. this.data = null;
  162. this.isInPackedState = false;
  163. this.table = line_number_table;
  164. this.tableLength = (line_number_table == null) ? 0 : line_number_table.length;
  165. }
  166. /**
  167. * @return String representation.
  168. */
  169. @Override
  170. public final String toString() {
  171. unpack();
  172. StringBuilder buf = new StringBuilder();
  173. StringBuilder line = new StringBuilder();
  174. for (int i = 0; i < tableLength; i++) {
  175. line.append(table[i].toString());
  176. if (i < tableLength - 1) {
  177. line.append(", ");
  178. }
  179. if (line.length() > 72) {
  180. line.append('\n');
  181. buf.append(line);
  182. line.setLength(0);
  183. }
  184. }
  185. buf.append(line);
  186. return buf.toString();
  187. }
  188. /**
  189. * Map byte code positions to source code lines.
  190. *
  191. * @param pos byte code offset
  192. * @return corresponding line in source code
  193. */
  194. public int getSourceLine(int pos) {
  195. unpack();
  196. int l = 0, r = tableLength - 1;
  197. if (r < 0) // array is empty
  198. return -1;
  199. int min_index = -1, min = -1;
  200. /*
  201. * Do a binary search since the array is ordered.
  202. */
  203. do {
  204. int i = (l + r) / 2;
  205. int j = table[i].getStartPC();
  206. if (j == pos)
  207. return table[i].getLineNumber();
  208. else if (pos < j) // else constrain search area
  209. r = i - 1;
  210. else
  211. // pos > j
  212. l = i + 1;
  213. /*
  214. * If exact match can't be found (which is the most common case) return the line number that corresponds to the greatest
  215. * index less than pos.
  216. */
  217. if (j < pos && j > min) {
  218. min = j;
  219. min_index = i;
  220. }
  221. } while (l <= r);
  222. /*
  223. * It's possible that we did not find any valid entry for the bytecode offset we were looking for.
  224. */
  225. if (min_index < 0)
  226. return -1;
  227. return table[min_index].getLineNumber();
  228. }
  229. /**
  230. * @return deep copy of this attribute
  231. */
  232. // @Override
  233. // public Attribute copy(ConstantPool constant_pool) {
  234. // unpack();
  235. // LineNumberTable newTable = (LineNumberTable) clone();
  236. // newTable.table = new LineNumber[tableLength];
  237. // for (int i = 0; i < tableLength; i++) {
  238. // newTable.table[i] = table[i].copy();
  239. // }
  240. // newTable.cpool = constant_pool;
  241. // return newTable;
  242. // }
  243. public final int getTableLength() {
  244. unpack();
  245. return tableLength;
  246. }
  247. }