1. package com.sun.org.apache.bcel.internal.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 com.sun.org.apache.bcel.internal.Constants;
  56. import java.io.*;
  57. /**
  58. * This class represents a table of line numbers for debugging
  59. * purposes. This attribute is used by the <em>Code</em> attribute. It
  60. * contains pairs of PCs and line numbers.
  61. *
  62. * @version $Id: LineNumberTable.java,v 1.1.1.1 2001/10/29 20:00:02 jvanzyl Exp $
  63. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  64. * @see Code
  65. * @see LineNumber
  66. */
  67. public final class LineNumberTable extends Attribute {
  68. private int line_number_table_length;
  69. private LineNumber[] line_number_table; // Table of line/numbers pairs
  70. /*
  71. * Initialize from another object. Note that both objects use the same
  72. * references (shallow copy). Use copy() for a physical copy.
  73. */
  74. public LineNumberTable(LineNumberTable c) {
  75. this(c.getNameIndex(), c.getLength(), c.getLineNumberTable(),
  76. c.getConstantPool());
  77. }
  78. /*
  79. * @param name_index Index of name
  80. * @param length Content length in bytes
  81. * @param line_number_table Table of line/numbers pairs
  82. * @param constant_pool Array of constants
  83. */
  84. public LineNumberTable(int name_index, int length,
  85. LineNumber[] line_number_table,
  86. ConstantPool constant_pool)
  87. {
  88. super(Constants.ATTR_LINE_NUMBER_TABLE, name_index, length, constant_pool);
  89. setLineNumberTable(line_number_table);
  90. }
  91. /**
  92. * Construct object from file stream.
  93. * @param name_index Index of name
  94. * @param length Content length in bytes
  95. * @param file Input stream
  96. * @throw IOException
  97. * @param constant_pool Array of constants
  98. */
  99. LineNumberTable(int name_index, int length, DataInputStream file,
  100. ConstantPool constant_pool) throws IOException
  101. {
  102. this(name_index, length, (LineNumber[])null, constant_pool);
  103. line_number_table_length = (file.readUnsignedShort());
  104. line_number_table = new LineNumber[line_number_table_length];
  105. for(int i=0; i < line_number_table_length; i++)
  106. line_number_table[i] = new LineNumber(file);
  107. }
  108. /**
  109. * Called by objects that are traversing the nodes of the tree implicitely
  110. * defined by the contents of a Java class. I.e., the hierarchy of methods,
  111. * fields, attributes, etc. spawns a tree of objects.
  112. *
  113. * @param v Visitor object
  114. */
  115. public void accept(Visitor v) {
  116. v.visitLineNumberTable(this);
  117. }
  118. /**
  119. * Dump line number table attribute to file stream in binary format.
  120. *
  121. * @param file Output file stream
  122. * @throw IOException
  123. */
  124. public final void dump(DataOutputStream file) throws IOException
  125. {
  126. super.dump(file);
  127. file.writeShort(line_number_table_length);
  128. for(int i=0; i < line_number_table_length; i++)
  129. line_number_table[i].dump(file);
  130. }
  131. /**
  132. * @return Array of (pc offset, line number) pairs.
  133. */
  134. public final LineNumber[] getLineNumberTable() { return line_number_table; }
  135. /**
  136. * @param line_number_table.
  137. */
  138. public final void setLineNumberTable(LineNumber[] line_number_table) {
  139. this.line_number_table = line_number_table;
  140. line_number_table_length = (line_number_table == null)? 0 :
  141. line_number_table.length;
  142. }
  143. /**
  144. * @return String representation.
  145. */
  146. public final String toString() {
  147. StringBuffer buf = new StringBuffer();
  148. StringBuffer line = new StringBuffer();
  149. for(int i=0; i < line_number_table_length; i++) {
  150. line.append(line_number_table[i].toString());
  151. if(i < line_number_table_length - 1)
  152. line.append(", ");
  153. if(line.length() > 72) {
  154. line.append('\n');
  155. buf.append(line);
  156. line.setLength(0);
  157. }
  158. }
  159. buf.append(line);
  160. return buf.toString();
  161. }
  162. /**
  163. * Map byte code positions to source code lines.
  164. *
  165. * @param pos byte code offset
  166. * @return corresponding line in source code
  167. */
  168. public int getSourceLine(int pos) {
  169. int l = 0, r = line_number_table_length-1;
  170. if(r < 0) // array is empty
  171. return -1;
  172. int min_index = -1, min=-1;
  173. /* Do a binary search since the array is ordered.
  174. */
  175. do {
  176. int i = (l + r) / 2;
  177. int j = line_number_table[i].getStartPC();
  178. if(j == pos)
  179. return line_number_table[i].getLineNumber();
  180. else if(pos < j) // else constrain search area
  181. r = i - 1;
  182. else // pos > j
  183. l = i + 1;
  184. /* If exact match can't be found (which is the most common case)
  185. * return the line number that corresponds to the greatest index less
  186. * than pos.
  187. */
  188. if(j < pos && j > min) {
  189. min = j;
  190. min_index = i;
  191. }
  192. } while(l <= r);
  193. return line_number_table[min_index].getLineNumber();
  194. }
  195. /**
  196. * @return deep copy of this attribute
  197. */
  198. public Attribute copy(ConstantPool constant_pool) {
  199. LineNumberTable c = (LineNumberTable)clone();
  200. c.line_number_table = new LineNumber[line_number_table_length];
  201. for(int i=0; i < line_number_table_length; i++)
  202. c.line_number_table[i] = line_number_table[i].copy();
  203. c.constant_pool = constant_pool;
  204. return c;
  205. }
  206. public final int getTableLength() { return line_number_table_length; }
  207. }