1 cananian 1.1.2.2 // AttributeLineNumberTable.java, created Mon Jan 18 22:44:35 1999 by cananian
 2 cananian 1.1.2.1 // Copyright (C) 1998 C. Scott Ananian <cananian@alumni.princeton.edu>
 3 cananian 1.1.2.1 // Licensed under the terms of the GNU GPL; see COPYING for details.
 4 cananian 1.1.2.1 package harpoon.IR.RawClass;
 5 cananian 1.1.2.1 
 6 cananian 1.1.2.1 /**
 7 cananian 1.1.2.1  * The <code>LineNumberTable</code> attribute is an optional
 8 cananian 1.1.2.1  * variable-length attribute in the <code>attributes</code> table of a
 9 cananian 1.1.2.1  * <code>Code</code> attribute.  It may be used by debuggers to
10 cananian 1.1.2.1  * determine which part of the Java Virtual Machine <code>code</code>
11 cananian 1.1.2.1  * array corresponds to a given line number in the original Java source
12 cananian 1.1.2.1  * file.  If <code>LineNumberTable</code> attributes are present in the
13 cananian 1.1.2.1  * <code>attributes</code> table of a given <code>Code</code> attribute,
14 cananian 1.1.2.1  * then they may appear in any order.  Furthermore, multiple
15 cananian 1.1.2.1  * <code>LineNumberTable</code> attributes may together represent a
16 cananian 1.1.2.1  * given line of a Java source file; that is,
17 cananian 1.1.2.1  * <code>LineNumberTable</code> attributes need not be one-to-one with
18 cananian 1.1.2.1  * source lines.
19 cananian 1.1.2.1  *
20 cananian 1.1.2.1  * @author  C. Scott Ananian <cananian@alumni.princeton.edu>
21 cananian 1.3      * @version $Id: AttributeLineNumberTable.java,v 1.3 2003/09/05 21:45:16 cananian Exp $
22 cananian 1.1.2.1  * @see "The Java Virtual Machine Specification, section 4.7.6"
23 cananian 1.1.2.1  * @see AttributeCode
24 cananian 1.1.2.1  * @see Attribute
25 cananian 1.1.2.1  */
26 cananian 1.1.2.1 public class AttributeLineNumberTable extends Attribute {
27 cananian 1.3       /** The string naming this <code>Attribute</code> type. */
28 cananian 1.3       public static final String ATTRIBUTE_NAME = "LineNumberTable";
29 cananian 1.1.2.1   /** Each entry in the <code>line_number_table</code> array indicates
30 cananian 1.1.2.1       that the line number in the original Java source file changes at
31 cananian 1.1.2.1       a given point in the <code>code</code> array. */
32 cananian 1.1.2.1   public LineNumberTable line_number_table[];
33 cananian 1.1.2.1 
34 cananian 1.1.2.1   /** Constructor. */
35 cananian 1.1.2.1   AttributeLineNumberTable(ClassFile parent, ClassDataInputStream in,
36 cananian 1.1.2.1                            int attribute_name_index) throws java.io.IOException
37 cananian 1.1.2.1   {
38 cananian 1.1.2.1     super(parent, attribute_name_index);
39 cananian 1.1.2.1     long attribute_length = in.read_u4();
40 cananian 1.1.2.1     
41 cananian 1.1.2.1     int line_number_table_length = in.read_u2();
42 cananian 1.1.2.1     line_number_table = new LineNumberTable[line_number_table_length];
43 cananian 1.1.2.1     for (int i=0; i<line_number_table_length; i++)
44 cananian 1.1.2.1       line_number_table[i] = new LineNumberTable(in);
45 cananian 1.1.2.1     
46 cananian 1.1.2.1     if (attribute_length != attribute_length())
47 cananian 1.1.2.1       throw new ClassDataException("LineNumberTable attribute with length "
48 cananian 1.1.2.1                                    + attribute_length);
49 cananian 1.3         assert ATTRIBUTE_NAME.equals(attribute_name());
50 cananian 1.1.2.1   }
51 cananian 1.1.2.1 
52 cananian 1.1.2.1   /** Constructor. */
53 cananian 1.1.2.1   public AttributeLineNumberTable(ClassFile parent, int attribute_name_index,
54 cananian 1.1.2.1                                   LineNumberTable line_number_table[]) {
55 cananian 1.1.2.1     super(parent, attribute_name_index);
56 cananian 1.1.2.1     this.line_number_table = line_number_table;
57 cananian 1.3         assert ATTRIBUTE_NAME.equals(attribute_name());
58 cananian 1.1.2.1   }
59 cananian 1.1.2.1 
60 cananian 1.1.2.1   public long attribute_length() { return 2 + 4*line_number_table_length(); }
61 cananian 1.1.2.1 
62 cananian 1.1.2.1   // Convenience.
63 cananian 1.1.2.1   public int line_number_table_length() { return line_number_table.length; }
64 cananian 1.1.2.1   
65 cananian 1.1.2.1   /** Write to bytecode stream. */
66 cananian 1.1.2.1   public void write(ClassDataOutputStream out) throws java.io.IOException {
67 cananian 1.1.2.1     out.write_u2(attribute_name_index);
68 cananian 1.1.2.1     out.write_u4(attribute_length());
69 cananian 1.1.2.1     
70 cananian 1.1.2.1     out.write_u2(line_number_table_length());
71 cananian 1.1.2.1     for (int i=0; i< line_number_table.length; i++)
72 cananian 1.1.2.1       line_number_table[i].write(out);
73 cananian 1.1.2.1   }
74 cananian 1.1.2.1 
75 cananian 1.1.2.1   /** Pretty-print this attribute.
76 cananian 1.1.2.1    *  @param indent the indentation level to use.
77 cananian 1.1.2.1    */
78 cananian 1.1.2.1   public void print(java.io.PrintWriter pw, int indent) {
79 cananian 1.1.2.1     int in=indent;
80 cananian 1.1.2.1     indent(pw, in, 
81 cananian 1.1.2.1            "LineNumberTable attribute ["+line_number_table_length()+"]:");
82 cananian 1.1.2.1     for (int i=0; i<line_number_table.length; i++)
83 cananian 1.1.2.1       indent(pw, in+1, 
84 cananian 1.1.2.1              "#"+i+": " + line_number_table[i].toString());
85 cananian 1.1.2.1   }
86 cananian 1.2     }