01: // Copyright (c) 1997 Per M.A. Bothner.
02: // This is free software; for terms and warranty disclaimer see ./COPYING.
03:
04: package gnu.bytecode;
05:
06: import java.io.*;
07:
08: /**
09: * Represents the contents of a standard "LineNumberTable" attribute.
10: * @author Per Bothner
11: */
12:
13: public class LineNumbersAttr extends Attribute {
14: // The line number table. Each even entry (starting with index 0) is a PC,
15: // and the following odd entry is the linenumber. Each number is
16: // actually unsigned, so should be masked with 0xFFFF.
17: short[] linenumber_table;
18: // The number of linenumber (pairs) in linenumber_table.
19: int linenumber_count;
20:
21: /** Add a new LineNumbersAttr to a CodeAttr. */
22: public LineNumbersAttr(CodeAttr code) {
23: super ("LineNumberTable");
24: addToFrontOf(code);
25: code.lines = this ;
26: }
27:
28: public LineNumbersAttr(short[] numbers, CodeAttr code) {
29: this (code);
30: linenumber_table = numbers;
31: linenumber_count = numbers.length >> 1;
32: }
33:
34: /** Add a new line number entry.
35: * @param linenumber the number in the source file for this entry
36: * @param PC the byte code location for the code for this line number. */
37: public void put(int linenumber, int PC) {
38: if (linenumber_table == null)
39: linenumber_table = new short[32];
40: else if (2 * linenumber_count >= linenumber_table.length) {
41: short[] new_linenumbers = new short[2 * linenumber_table.length];
42: System.arraycopy(linenumber_table, 0, new_linenumbers, 0,
43: 2 * linenumber_count);
44: linenumber_table = new_linenumbers;
45: }
46: linenumber_table[2 * linenumber_count] = (short) PC;
47: linenumber_table[2 * linenumber_count + 1] = (short) linenumber;
48: linenumber_count++;
49: }
50:
51: /** Get the number of line number entries. */
52: public final int getLength() {
53: return 2 + 4 * linenumber_count;
54: }
55:
56: public int getLineCount() {
57: return linenumber_count;
58: }
59:
60: public short[] getLineNumberTable() {
61: return linenumber_table;
62: }
63:
64: public void write(DataOutputStream dstr) throws java.io.IOException {
65: dstr.writeShort(linenumber_count);
66: int count = 2 * linenumber_count;
67: for (int i = 0; i < count; i++) {
68: dstr.writeShort(linenumber_table[i]);
69: }
70: }
71:
72: public void print(ClassTypeWriter dst) {
73: dst.print("Attribute \"");
74: dst.print(getName());
75: dst.print("\", length:");
76: dst.print(getLength());
77: dst.print(", count: ");
78: dst.println(linenumber_count);
79: for (int i = 0; i < linenumber_count; i++) {
80: dst.print(" line: ");
81: dst.print(linenumber_table[2 * i + 1] & 0xFFFF); // line number
82: dst.print(" at pc: ");
83: dst.println(linenumber_table[2 * i] & 0xFFFF); // start_pc
84: }
85: }
86: }
|