View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   *
17   */
18  package org.apache.bcel.classfile;
19  
20  import java.io.DataInput;
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  
24  import org.apache.bcel.Const;
25  
26  /**
27   * This class represents a table of line numbers for debugging
28   * purposes. This attribute is used by the <em>Code</em> attribute. It
29   * contains pairs of PCs and line numbers.
30   *
31   * @see     Code
32   * @see LineNumber
33   */
34  public final class LineNumberTable extends Attribute {
35  
36      private static final int MAX_LINE_LENGTH = 72;
37      private LineNumber[] lineNumberTable; // Table of line/numbers pairs
38  
39  
40      /*
41       * Initialize from another object. Note that both objects use the same
42       * references (shallow copy). Use copy() for a physical copy.
43       */
44      public LineNumberTableNumberTable.html#LineNumberTable">LineNumberTable(final LineNumberTable c) {
45          this(c.getNameIndex(), c.getLength(), c.getLineNumberTable(), c.getConstantPool());
46      }
47  
48  
49      /*
50       * @param name_index Index of name
51       * @param length Content length in bytes
52       * @param lineNumberTable Table of line/numbers pairs
53       * @param constant_pool Array of constants
54       */
55      public LineNumberTable(final int name_index, final int length, final LineNumber[] line_number_table,
56              final ConstantPool constant_pool) {
57          super(Const.ATTR_LINE_NUMBER_TABLE, name_index, length, constant_pool);
58          this.lineNumberTable = line_number_table;
59      }
60  
61  
62      /**
63       * Construct object from input stream.
64       * @param name_index Index of name
65       * @param length Content length in bytes
66       * @param input Input stream
67       * @param constant_pool Array of constants
68       * @throws IOException if an I/O Exception occurs in readUnsignedShort
69       */
70      LineNumberTable(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool)
71              throws IOException {
72          this(name_index, length, (LineNumber[]) null, constant_pool);
73          final int line_number_table_length = input.readUnsignedShort();
74          lineNumberTable = new LineNumber[line_number_table_length];
75          for (int i = 0; i < line_number_table_length; i++) {
76              lineNumberTable[i] = new LineNumber(input);
77          }
78      }
79  
80  
81      /**
82       * Called by objects that are traversing the nodes of the tree implicitely
83       * defined by the contents of a Java class. I.e., the hierarchy of methods,
84       * fields, attributes, etc. spawns a tree of objects.
85       *
86       * @param v Visitor object
87       */
88      @Override
89      public void accept( final Visitor v ) {
90          v.visitLineNumberTable(this);
91      }
92  
93  
94      /**
95       * Dump line number table attribute to file stream in binary format.
96       *
97       * @param file Output file stream
98       * @throws IOException if an I/O Exception occurs in writeShort
99       */
100     @Override
101     public void dump( final DataOutputStream file ) throws IOException {
102         super.dump(file);
103         file.writeShort(lineNumberTable.length);
104         for (final LineNumber lineNumber : lineNumberTable) {
105             lineNumber.dump(file);
106         }
107     }
108 
109 
110     /**
111      * @return Array of (pc offset, line number) pairs.
112      */
113     public LineNumber[] getLineNumberTable() {
114         return lineNumberTable;
115     }
116 
117 
118     /**
119      * @param lineNumberTable the line number entries for this table
120      */
121     public void setLineNumberTable( final LineNumber[] lineNumberTable ) {
122         this.lineNumberTable = lineNumberTable;
123     }
124 
125 
126     /**
127      * @return String representation.
128      */
129     @Override
130     public String toString() {
131         final StringBuilder buf = new StringBuilder();
132         final StringBuilder line = new StringBuilder();
133         final String newLine = System.getProperty("line.separator", "\n");
134         for (int i = 0; i < lineNumberTable.length; i++) {
135             line.append(lineNumberTable[i].toString());
136             if (i < lineNumberTable.length - 1) {
137                 line.append(", ");
138             }
139             if ((line.length() > MAX_LINE_LENGTH) && (i < lineNumberTable.length - 1)) {
140                 line.append(newLine);
141                 buf.append(line);
142                 line.setLength(0);
143             }
144         }
145         buf.append(line);
146         return buf.toString();
147     }
148 
149 
150     /**
151      * Map byte code positions to source code lines.
152      *
153      * @param pos byte code offset
154      * @return corresponding line in source code
155      */
156     public int getSourceLine( final int pos ) {
157         int l = 0;
158         int r = lineNumberTable.length - 1;
159         if (r < 0) {
160             return -1;
161         }
162         int min_index = -1;
163         int min = -1;
164         /* Do a binary search since the array is ordered.
165          */
166         do {
167             final int i = (l + r) >>> 1;
168             final int j = lineNumberTable[i].getStartPC();
169             if (j == pos) {
170                 return lineNumberTable[i].getLineNumber();
171             } else if (pos < j) {
172                 r = i - 1;
173             } else {
174                 l = i + 1;
175             }
176             /* If exact match can't be found (which is the most common case)
177              * return the line number that corresponds to the greatest index less
178              * than pos.
179              */
180             if (j < pos && j > min) {
181                 min = j;
182                 min_index = i;
183             }
184         } while (l <= r);
185         /* It's possible that we did not find any valid entry for the bytecode
186          * offset we were looking for.
187          */
188         if (min_index < 0) {
189             return -1;
190         }
191         return lineNumberTable[min_index].getLineNumber();
192     }
193 
194 
195     /**
196      * @return deep copy of this attribute
197      */
198     @Override
199     public Attribute copy( final ConstantPool _constant_pool ) {
200         // TODO could use the lower level constructor and thereby allow
201         // lineNumberTable to be made final
202         final LineNumberTable./../../org/apache/bcel/classfile/LineNumberTable.html#LineNumberTable">LineNumberTable c = (LineNumberTable) clone();
203         c.lineNumberTable = new LineNumber[lineNumberTable.length];
204         for (int i = 0; i < lineNumberTable.length; i++) {
205             c.lineNumberTable[i] = lineNumberTable[i].copy();
206         }
207         c.setConstantPool(_constant_pool);
208         return c;
209     }
210 
211 
212     public int getTableLength() {
213         return lineNumberTable == null ? 0 : lineNumberTable.length;
214     }
215 }