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.

LineNumberAttribute.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2003 Shigeru Chiba. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later.
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. */
  15. package javassist.bytecode;
  16. import java.io.DataInputStream;
  17. import java.io.IOException;
  18. import java.util.Map;
  19. /**
  20. * <code>LineNumberTablec_attribute</code>.
  21. */
  22. public class LineNumberAttribute extends AttributeInfo {
  23. /**
  24. * The name of this attribute <code>"LineNumberTable"</code>.
  25. */
  26. public static final String tag = "LineNumberTable";
  27. LineNumberAttribute(ConstPool cp, int n, DataInputStream in)
  28. throws IOException
  29. {
  30. super(cp, n, in);
  31. }
  32. private LineNumberAttribute(ConstPool cp, byte[] i) {
  33. super(cp, tag, i);
  34. }
  35. /**
  36. * Returns <code>line_number_table_length</code>.
  37. * This represents the number of entries in the table.
  38. */
  39. public int tableLength() {
  40. return ByteArray.readU16bit(info, 0);
  41. }
  42. /**
  43. * Returns <code>line_number_table[i].start_pc</code>.
  44. * This represents the index into the code array at which the code
  45. * for a new line in the original source file begins.
  46. *
  47. * @param i the i-th entry.
  48. */
  49. public int startPc(int i) {
  50. return ByteArray.readU16bit(info, i * 4 + 2);
  51. }
  52. /**
  53. * Returns <code>line_number_table[i].line_number</code>.
  54. * This represents the corresponding line number in the original
  55. * source file.
  56. *
  57. * @param i the i-th entry.
  58. */
  59. public int lineNumber(int i) {
  60. return ByteArray.readU16bit(info, i * 4 + 4);
  61. }
  62. /**
  63. * Returns the line number corresponding to the specified bytecode.
  64. *
  65. * @param pc the index into the code array.
  66. */
  67. public int toLineNumber(int pc) {
  68. int n = tableLength();
  69. int i = 0;
  70. for (; i < n; ++i)
  71. if (pc < startPc(i))
  72. if (i == 0)
  73. return lineNumber(0);
  74. else
  75. break;
  76. return lineNumber(i - 1);
  77. }
  78. /**
  79. * Returns the index into the code array at which the code for
  80. * the specified line begins.
  81. *
  82. * @param line the line number.
  83. * @return -1 if the specified line is not found.
  84. */
  85. public int toStartPc(int line) {
  86. int n = tableLength();
  87. for (int i = 0; i < n; ++i)
  88. if (line == lineNumber(i))
  89. return startPc(i);
  90. return -1;
  91. }
  92. /**
  93. * Makes a copy.
  94. *
  95. * @param newCp the constant pool table used by the new copy.
  96. * @param classnames should be null.
  97. */
  98. public AttributeInfo copy(ConstPool newCp, Map classnames) {
  99. byte[] src = info;
  100. int num = src.length;
  101. byte[] dest = new byte[num];
  102. for (int i = 0; i < num; ++i)
  103. dest[i] = src[i];
  104. LineNumberAttribute attr = new LineNumberAttribute(newCp, dest);
  105. return attr;
  106. }
  107. }