Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/techtrader/modules/tools/bytecode/LineNumberTableAttribute.java


1   package com.techtrader.modules.tools.bytecode;
2   
3   
4   import java.io.DataOutput;
5   import java.io.DataInput;
6   import java.io.IOException;
7   import java.util.List;
8   import java.util.LinkedList;
9   import java.util.Iterator;
10  
11  import com.techtrader.modules.tools.bytecode.visitor.BCVisitor;
12  
13  
14  /**
15   *  A LineNumberTableAttributs holds a table of line number to program counter
16   *  mappings, so that errors can be reported with the correct line number.
17   *  TODO: Allow high-level manipulation of the line numbers.
18   *  
19   *  @author    Abe White
20   */
21  public class LineNumberTableAttribute
22    extends Attribute
23  {
24    private List _lineNumbers = new LinkedList ();
25  
26  
27    /**
28     *  Protected constructor.
29     */
30    public LineNumberTableAttribute (int nameIndex, BCEntity owner)
31    {
32      super (nameIndex, owner);
33    }
34  
35  
36    /**
37     *  Get the line numbers held in this table.
38     */
39    public LineNumber[] getLineNumbers ()
40    {
41      return (LineNumber[]) _lineNumbers.toArray 
42        (new LineNumber[_lineNumbers.size ()]);
43    }
44  
45  
46    /**
47     *  Import a line number from another method.
48     */
49    public LineNumber importLineNumber (LineNumber ln)
50    {
51      if (ln == null)
52        throw new NullPointerException ();
53  
54      LineNumber newLine = addLineNumber ();
55      newLine.setStartPc (ln.getStartPc ());
56      newLine.setLineNumber (ln.getLineNumber ());
57  
58      return newLine;
59    }
60  
61  
62    /**
63     *  Import all line numbers from another method.
64     */
65    public void importLineNumbers (LineNumberTableAttribute lns)
66    {
67      LineNumber[] ln = lns.getLineNumbers ();
68      for (int i = 0; i < ln.length; i++)
69        importLineNumber (ln[i]);
70    }
71  
72  
73    /**
74     *  Add a new line number to this table.
75     *
76     *  @return    the index into the list at which the line number was added
77     */
78    public LineNumber addLineNumber ()
79    {
80      LineNumber lineNumber = new LineNumber (this);
81      _lineNumbers.add (lineNumber);
82  
83      return lineNumber;
84    }
85  
86  
87    /**
88     *  Clear the line numbers.
89     */
90    public void clearLineNumbers ()
91    {
92      _lineNumbers.clear ();
93    }
94  
95  
96    /**
97     *  Remove the given LineNumber.
98     */
99    public boolean removeLineNumber (LineNumber ln)
100   {
101     if (ln == null || !_lineNumbers.remove (ln))
102       return false;
103     
104     ln.invalidate ();
105     return true;
106   }  
107 
108 
109   public int getLength ()
110   {
111     return 2 + 4 * _lineNumbers.size ();
112   }
113 
114 
115   protected void copy (Attribute other)
116   {
117     importLineNumbers ((LineNumberTableAttribute) other);
118   }
119 
120 
121   protected void readData (DataInput in, int length)
122     throws IOException
123   {
124     _lineNumbers.clear ();
125     int numLines = in.readUnsignedShort ();
126 
127     LineNumber lineNumber;
128     for (int i = 0; i < numLines; i++)
129     {
130       lineNumber = addLineNumber ();
131       lineNumber.readData (in);
132     }
133   }
134 
135 
136   protected void writeData (DataOutput out, int length)
137     throws IOException
138   {
139     out.writeShort (_lineNumbers.size ());
140     for (Iterator i = _lineNumbers.iterator (); i.hasNext ();)
141       ((LineNumber) i.next ()).writeData (out);
142   }
143 
144 
145   public void acceptVisit (BCVisitor visit)
146   {
147     visit.enterLineNumberTableAttribute (this);
148 
149     for (Iterator i = _lineNumbers.iterator (); i.hasNext ();)
150       ((LineNumber) i.next ()).acceptVisit (visit);
151 
152     visit.exitLineNumberTableAttribute (this);
153   }
154 }