View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   package com.sun.org.apache.bcel.internal.classfile;
6   
7   /* ====================================================================
8    * The Apache Software License, Version 1.1
9    *
10   * Copyright (c) 2001 The Apache Software Foundation.  All rights
11   * reserved.
12   *
13   * Redistribution and use in source and binary forms, with or without
14   * modification, are permitted provided that the following conditions
15   * are met:
16   *
17   * 1. Redistributions of source code must retain the above copyright
18   *    notice, this list of conditions and the following disclaimer.
19   *
20   * 2. Redistributions in binary form must reproduce the above copyright
21   *    notice, this list of conditions and the following disclaimer in
22   *    the documentation and/or other materials provided with the
23   *    distribution.
24   *
25   * 3. The end-user documentation included with the redistribution,
26   *    if any, must include the following acknowledgment:
27   *       "This product includes software developed by the
28   *        Apache Software Foundation (http://www.apache.org/)."
29   *    Alternately, this acknowledgment may appear in the software itself,
30   *    if and wherever such third-party acknowledgments normally appear.
31   *
32   * 4. The names "Apache" and "Apache Software Foundation" and
33   *    "Apache BCEL" must not be used to endorse or promote products
34   *    derived from this software without prior written permission. For
35   *    written permission, please contact apache@apache.org.
36   *
37   * 5. Products derived from this software may not be called "Apache",
38   *    "Apache BCEL", nor may "Apache" appear in their name, without
39   *    prior written permission of the Apache Software Foundation.
40   *
41   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52   * SUCH DAMAGE.
53   * ====================================================================
54   *
55   * This software consists of voluntary contributions made by many
56   * individuals on behalf of the Apache Software Foundation.  For more
57   * information on the Apache Software Foundation, please see
58   * <http://www.apache.org/>.
59   */
60  
61  import  com.sun.org.apache.bcel.internal.Constants;
62  import  java.io.*;
63  
64  /**
65   * This class represents a table of line numbers for debugging
66   * purposes. This attribute is used by the <em>Code</em> attribute. It
67   * contains pairs of PCs and line numbers.
68   *
69   * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
70   * @see     Code
71   * @see LineNumber
72   */
73  public final class LineNumberTable extends Attribute {
74    private int          line_number_table_length;
75    private LineNumber[] line_number_table; // Table of line/numbers pairs
76  
77    /*
78     * Initialize from another object. Note that both objects use the same
79     * references (shallow copy). Use copy() for a physical copy.
80     */
81    public LineNumberTable(LineNumberTable c) {
82      this(c.getNameIndex(), c.getLength(), c.getLineNumberTable(),
83           c.getConstantPool());
84    }
85  
86    /*
87     * @param name_index Index of name
88     * @param length Content length in bytes
89     * @param line_number_table Table of line/numbers pairs
90     * @param constant_pool Array of constants
91     */
92    public LineNumberTable(int name_index, int length,
93                           LineNumber[] line_number_table,
94                           ConstantPool constant_pool)
95    {
96      super(Constants.ATTR_LINE_NUMBER_TABLE, name_index, length, constant_pool);
97      setLineNumberTable(line_number_table);
98    }
99  
100   /**
101    * Construct object from file stream.
102    * @param name_index Index of name
103    * @param length Content length in bytes
104    * @param file Input stream
105    * @throws IOException
106    * @param constant_pool Array of constants
107    */
108   LineNumberTable(int name_index, int length, DataInputStream file,
109                   ConstantPool constant_pool) throws IOException
110   {
111     this(name_index, length, (LineNumber[])null, constant_pool);
112     line_number_table_length = (file.readUnsignedShort());
113     line_number_table = new LineNumber[line_number_table_length];
114 
115     for(int i=0; i < line_number_table_length; i++)
116       line_number_table[i] = new LineNumber(file);
117   }
118   /**
119    * Called by objects that are traversing the nodes of the tree implicitely
120    * defined by the contents of a Java class. I.e., the hierarchy of methods,
121    * fields, attributes, etc. spawns a tree of objects.
122    *
123    * @param v Visitor object
124    */
125   public void accept(Visitor v) {
126     v.visitLineNumberTable(this);
127   }
128   /**
129    * Dump line number table attribute to file stream in binary format.
130    *
131    * @param file Output file stream
132    * @throws IOException
133    */
134   public final void dump(DataOutputStream file) throws IOException
135   {
136     super.dump(file);
137     file.writeShort(line_number_table_length);
138     for(int i=0; i < line_number_table_length; i++)
139       line_number_table[i].dump(file);
140   }
141 
142   /**
143    * @return Array of (pc offset, line number) pairs.
144    */
145   public final LineNumber[] getLineNumberTable() { return line_number_table; }
146 
147   /**
148    * @param line_number_table.
149    */
150   public final void setLineNumberTable(LineNumber[] line_number_table) {
151     this.line_number_table = line_number_table;
152 
153     line_number_table_length = (line_number_table == null)? 0 :
154       line_number_table.length;
155   }
156 
157   /**
158    * @return String representation.
159    */
160   public final String toString() {
161     StringBuffer buf  = new StringBuffer();
162     StringBuffer line = new StringBuffer();
163 
164     for(int i=0; i < line_number_table_length; i++) {
165       line.append(line_number_table[i].toString());
166 
167       if(i < line_number_table_length - 1)
168         line.append(", ");
169 
170       if(line.length() > 72) {
171         line.append('\n');
172         buf.append(line);
173         line.setLength(0);
174       }
175     }
176 
177     buf.append(line);
178 
179     return buf.toString();
180   }
181 
182   /**
183    * Map byte code positions to source code lines.
184    *
185    * @param pos byte code offset
186    * @return corresponding line in source code
187    */
188   public int getSourceLine(int pos) {
189     int l = 0, r = line_number_table_length-1;
190 
191     if(r < 0) // array is empty
192       return -1;
193 
194     int min_index = -1, min=-1;
195 
196     /* Do a binary search since the array is ordered.
197      */
198     do {
199       int i = (l + r) / 2;
200       int j = line_number_table[i].getStartPC();
201 
202       if(j == pos)
203         return line_number_table[i].getLineNumber();
204       else if(pos < j) // else constrain search area
205         r = i - 1;
206       else // pos > j
207         l = i + 1;
208 
209       /* If exact match can't be found (which is the most common case)
210        * return the line number that corresponds to the greatest index less
211        * than pos.
212        */
213       if(j < pos && j > min) {
214         min       = j;
215         min_index = i;
216       }
217     } while(l <= r);
218 
219     /* It's possible that we did not find any valid entry for the bytecode
220      * offset we were looking for.
221      */
222     if (min_index < 0)
223       return -1;
224 
225     return line_number_table[min_index].getLineNumber();
226   }
227 
228   /**
229    * @return deep copy of this attribute
230    */
231   public Attribute copy(ConstantPool constant_pool) {
232     LineNumberTable c = (LineNumberTable)clone();
233 
234     c.line_number_table = new LineNumber[line_number_table_length];
235     for(int i=0; i < line_number_table_length; i++)
236       c.line_number_table[i] = line_number_table[i].copy();
237 
238     c.constant_pool = constant_pool;
239     return c;
240   }
241 
242   public final int getTableLength() { return line_number_table_length; }
243 }