/Users/lyon/j4p/src/javassist/bytecode/LineNumberAttribute.java
|
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
16 package javassist.bytecode;
17
18 import java.io.DataInputStream;
19 import java.io.IOException;
20 import java.util.Map;
21
22 /**
23 * <code>LineNumberTablec_attribute</code>.
24 */
25 public class LineNumberAttribute extends AttributeInfo {
26 /**
27 * The name of this attribute <code>"LineNumberTable"</code>.
28 */
29 public static final String tag = "LineNumberTable";
30
31 LineNumberAttribute(ConstPool cp, int n, DataInputStream in)
32 throws IOException {
33 super(cp, n, in);
34 }
35
36 private LineNumberAttribute(ConstPool cp, byte[] i) {
37 super(cp, tag, i);
38 }
39
40 /**
41 * Returns <code>line_number_table_length</code>.
42 * This represents the number of entries in the table.
43 */
44 public int tableLength() {
45 return ByteArray.readU16bit(info, 0);
46 }
47
48 /**
49 * Returns <code>line_number_table[i].start_pc</code>.
50 * This represents the index into the code array at which the code
51 * for a new line in the original source file begins.
52 *
53 * @param i the i-th entry.
54 */
55 public int startPc(int i) {
56 return ByteArray.readU16bit(info, i * 4 + 2);
57 }
58
59 /**
60 * Returns <code>line_number_table[i].line_number</code>.
61 * This represents the corresponding line number in the original
62 * source file.
63 *
64 * @param i the i-th entry.
65 */
66 public int lineNumber(int i) {
67 return ByteArray.readU16bit(info, i * 4 + 4);
68 }
69
70 /**
71 * Returns the line number corresponding to the specified bytecode.
72 *
73 * @param pc the index into the code array.
74 */
75 public int toLineNumber(int pc) {
76 int n = tableLength();
77 int i = 0;
78 for (; i < n; ++i)
79 if (pc < startPc(i))
80 if (i == 0)
81 return lineNumber(0);
82 else
83 break;
84
85 return lineNumber(i - 1);
86 }
87
88 /**
89 * Returns the index into the code array at which the code for
90 * the specified line begins.
91 *
92 * @param line the line number.
93 * @return -1 if the specified line is not found.
94 */
95 public int toStartPc(int line) {
96 int n = tableLength();
97 for (int i = 0; i < n; ++i)
98 if (line == lineNumber(i))
99 return startPc(i);
100
101 return -1;
102 }
103
104 /**
105 * Makes a copy.
106 *
107 * @param newCp the constant pool table used by the new copy.
108 * @param classnames should be null.
109 */
110 public AttributeInfo copy(ConstPool newCp, Map classnames) {
111 byte[] src = info;
112 int num = src.length;
113 byte[] dest = new byte[num];
114 for (int i = 0; i < num; ++i)
115 dest[i] = src[i];
116
117 LineNumberAttribute attr = new LineNumberAttribute(newCp, dest);
118 return attr;
119 }
120 }
121