KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > bytecode > LineNumbersAttr


1 // Copyright (c) 1997 Per M.A. Bothner.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.bytecode;
5 import java.io.*;
6
7 /**
8   * Represents the contents of a standard "LineNumberTable" attribute.
9   * @author Per Bothner
10   */

11
12 public class LineNumbersAttr extends Attribute
13 {
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   {
24     super("LineNumberTable");
25     addToFrontOf(code);
26     code.lines = this;
27   }
28
29   public LineNumbersAttr(short[] numbers, CodeAttr code)
30   {
31     this(code);
32     linenumber_table = numbers;
33     linenumber_count = numbers.length >> 1;
34   }
35
36   /** Add a new line number entry.
37     * @param linenumber the number in the source file for this entry
38     * @param PC the byte code location for the code for this line number. */

39   public void put (int linenumber, int PC)
40   {
41     if (linenumber_table == null)
42       linenumber_table = new short[32];
43     else if (2 * linenumber_count >= linenumber_table.length)
44       {
45     short[] new_linenumbers = new short [2 * linenumber_table.length];
46     System.arraycopy (linenumber_table, 0, new_linenumbers, 0,
47               2 * linenumber_count);
48     linenumber_table = new_linenumbers;
49       }
50     linenumber_table[2 * linenumber_count] = (short) PC;
51     linenumber_table[2 * linenumber_count + 1] = (short) linenumber;
52     linenumber_count++;
53   }
54
55   /** Get the number of line number entries. */
56   public final int getLength() { return 2 + 4 * linenumber_count; }
57
58   public int getLineCount () { return linenumber_count; }
59   public short[] getLineNumberTable () { return linenumber_table; }
60
61   public void write (DataOutputStream dstr) throws java.io.IOException JavaDoc
62   {
63     dstr.writeShort (linenumber_count);
64     int count = 2 * linenumber_count;
65     for (int i = 0; i < count; i++)
66       {
67     dstr.writeShort(linenumber_table[i]);
68       }
69   }
70
71   public void print (ClassTypeWriter dst)
72   {
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       {
81     dst.print(" line: ");
82     dst.print(linenumber_table[2 * i + 1] & 0xFFFF); // line number
83
dst.print(" at pc: ");
84     dst.println(linenumber_table[2 * i] & 0xFFFF); // start_pc
85
}
86   }
87 }
88
Popular Tags