001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 *
017 */
018package org.apache.bcel.classfile;
019
020import java.io.DataInput;
021import java.io.DataOutputStream;
022import java.io.IOException;
023
024import org.apache.bcel.Const;
025
026/**
027 * This class represents a table of line numbers for debugging
028 * purposes. This attribute is used by the <em>Code</em> attribute. It
029 * contains pairs of PCs and line numbers.
030 *
031 * @see     Code
032 * @see LineNumber
033 */
034public final class LineNumberTable extends Attribute {
035
036    private static final int MAX_LINE_LENGTH = 72;
037    private LineNumber[] line_number_table; // Table of line/numbers pairs
038
039
040    /*
041     * Initialize from another object. Note that both objects use the same
042     * references (shallow copy). Use copy() for a physical copy.
043     */
044    public LineNumberTable(final LineNumberTable c) {
045        this(c.getNameIndex(), c.getLength(), c.getLineNumberTable(), c.getConstantPool());
046    }
047
048
049    /*
050     * @param name_index Index of name
051     * @param length Content length in bytes
052     * @param line_number_table Table of line/numbers pairs
053     * @param constant_pool Array of constants
054     */
055    public LineNumberTable(final int name_index, final int length, final LineNumber[] line_number_table,
056            final ConstantPool constant_pool) {
057        super(Const.ATTR_LINE_NUMBER_TABLE, name_index, length, constant_pool);
058        this.line_number_table = line_number_table;
059    }
060
061
062    /**
063     * Construct object from input stream.
064     * @param name_index Index of name
065     * @param length Content length in bytes
066     * @param input Input stream
067     * @param constant_pool Array of constants
068     * @throws IOException if an I/O Exception occurs in readUnsignedShort
069     */
070    LineNumberTable(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool)
071            throws IOException {
072        this(name_index, length, (LineNumber[]) null, constant_pool);
073        final int line_number_table_length = input.readUnsignedShort();
074        line_number_table = new LineNumber[line_number_table_length];
075        for (int i = 0; i < line_number_table_length; i++) {
076            line_number_table[i] = new LineNumber(input);
077        }
078    }
079
080
081    /**
082     * Called by objects that are traversing the nodes of the tree implicitely
083     * defined by the contents of a Java class. I.e., the hierarchy of methods,
084     * fields, attributes, etc. spawns a tree of objects.
085     *
086     * @param v Visitor object
087     */
088    @Override
089    public void accept( final Visitor v ) {
090        v.visitLineNumberTable(this);
091    }
092
093
094    /**
095     * Dump line number table attribute to file stream in binary format.
096     *
097     * @param file Output file stream
098     * @throws IOException if an I/O Exception occurs in writeShort
099     */
100    @Override
101    public void dump( final DataOutputStream file ) throws IOException {
102        super.dump(file);
103        file.writeShort(line_number_table.length);
104        for (final LineNumber lineNumber : line_number_table) {
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 line_number_table;
115    }
116
117
118    /**
119     * @param line_number_table the line number entries for this table
120     */
121    public void setLineNumberTable( final LineNumber[] line_number_table ) {
122        this.line_number_table = line_number_table;
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 < line_number_table.length; i++) {
135            line.append(line_number_table[i].toString());
136            if (i < line_number_table.length - 1) {
137                line.append(", ");
138            }
139            if ((line.length() > MAX_LINE_LENGTH) && (i < line_number_table.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 = line_number_table.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 = line_number_table[i].getStartPC();
169            if (j == pos) {
170                return line_number_table[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 line_number_table[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        // line_number_table to be made final
202        final LineNumberTable c = (LineNumberTable) clone();
203        c.line_number_table = new LineNumber[line_number_table.length];
204        for (int i = 0; i < line_number_table.length; i++) {
205            c.line_number_table[i] = line_number_table[i].copy();
206        }
207        c.setConstantPool(_constant_pool);
208        return c;
209    }
210
211
212    public int getTableLength() {
213        return line_number_table == null ? 0 : line_number_table.length;
214    }
215}