    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 colection of local variables in a
    028 * method. This attribute is contained in the <em>Code</em> attribute.
    029 *
    030 * @see     Code
    031 * @see LocalVariable
    032 */
    033public class LocalVariableTable extends Attribute {
    034
    035    private LocalVariable[] localVariableTable; // variables
    036
    037
    038    /**
    039     * Initialize from another object. Note that both objects use the same
    040     * references (shallow copy). Use copy() for a physical copy.
    041     */
    042    public LocalVariableTable(final LocalVariableTable c) {
    043        this(c.getNameIndex(), c.getLength(), c.getLocalVariableTable(), c.getConstantPool());
    044    }
    045
    046
    047    /**
    048     * @param nameIndex Index in constant pool to `LocalVariableTable'
    049     * @param length Content length in bytes
    050     * @param localVariableTable Table of local variables
    051     * @param constantPool Array of constants
    052     */
    053    public LocalVariableTable(final int nameIndex, final int length, final LocalVariable[] localVariableTable,
    054            final ConstantPool constantPool) {
    055        super(Const.ATTR_LOCAL_VARIABLE_TABLE, nameIndex, length, constantPool);
    056        this.localVariableTable = localVariableTable;
    057    }
    058
    059
    060    /**
    061     * Construct object from input stream.
    062     * @param name_index Index in constant pool
    063     * @param length Content length in bytes
    064     * @param input Input stream
    065     * @param constant_pool Array of constants
    066     * @throws IOException
    067     */
    068    LocalVariableTable(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool)
    069            throws IOException {
    070        this(name_index, length, (LocalVariable[]) null, constant_pool);
    071        final int local_variable_table_length = input.readUnsignedShort();
    072        localVariableTable = new LocalVariable[local_variable_table_length];
    073        for (int i = 0; i < local_variable_table_length; i++) {
    074            localVariableTable[i] = new LocalVariable(input, constant_pool);
    075        }
    076    }
    077
    078
    079    /**
    080     * Called by objects that are traversing the nodes of the tree implicitely
    081     * defined by the contents of a Java class. I.e., the hierarchy of methods,
    082     * fields, attributes, etc. spawns a tree of objects.
    083     *
    084     * @param v Visitor object
    085     */
    086    @Override
    087    public void accept( final Visitor v ) {
    088        v.visitLocalVariableTable(this);
    089    }
    090
    091
    092    /**
    093     * Dump local variable table attribute to file stream in binary format.
    094     *
    095     * @param file Output file stream
    096     * @throws IOException
    097     */
    098    @Override
    099    public final void dump( final DataOutputStream file ) throws IOException {
    100        super.dump(file);
    101        file.writeShort(localVariableTable.length);
    102        for (final LocalVariable variable : localVariableTable) {
    103            variable.dump(file);
    104        }
    105    }
    106
    107
    108    /**
    109     * @return Array of local variables of method.
    110     */
    111    public final LocalVariable[] getLocalVariableTable() {
    112        return localVariableTable;
    113    }
    114
    115
    116    /**
    117     *
    118     * @param index the variable slot
    119     *
    120     * @return the first LocalVariable that matches the slot or null if not found
    121     *
    122     * @deprecated since 5.2 because multiple variables can share the
    123     *             same slot, use getLocalVariable(int index, int pc) instead.
    124     */
    125    @java.lang.Deprecated
    126    public final LocalVariable getLocalVariable( final int index ) {
    127        for (final LocalVariable variable : localVariableTable) {
    128            if (variable.getIndex() == index) {
    129                return variable;
    130            }
    131        }
    132        return null;
    133    }
    134
    135
    136    /**
    137     *
    138     * @param index the variable slot
    139     * @param pc the current pc that this variable is alive
    140     *
    141     * @return the LocalVariable that matches or null if not found
    142     */
    143    public final LocalVariable getLocalVariable( final int index, final int pc ) {
    144        for (final LocalVariable variable : localVariableTable) {
    145            if (variable.getIndex() == index) {
    146                final int start_pc = variable.getStartPC();
    147                final int end_pc = start_pc + variable.getLength();
    148                if ((pc >= start_pc) && (pc <= end_pc)) {
    149                    return variable;
    150                }
    151            }
    152        }
    153        return null;
    154    }
    155
    156
    157    public final void setLocalVariableTable( final LocalVariable[] local_variable_table ) {
    158        this.localVariableTable = local_variable_table;
    159    }
    160
    161
    162    /**
    163     * @return String representation.
    164     */
    165    @Override
    166    public final String toString() {
    167        final StringBuilder buf = new StringBuilder();
    168        for (int i = 0; i < localVariableTable.length; i++) {
    169            buf.append(localVariableTable[i]);
    170            if (i < localVariableTable.length - 1) {
    171                buf.append('\n');
    172            }
    173        }
    174        return buf.toString();
    175    }
    176
    177
    178    /**
    179     * @return deep copy of this attribute
    180     */
    181    @Override
    182    public Attribute copy( final ConstantPool _constant_pool ) {
    183        final LocalVariableTable c = (LocalVariableTable) clone();
    184        c.localVariableTable = new LocalVariable[localVariableTable.length];
    185        for (int i = 0; i < localVariableTable.length; i++) {
    186            c.localVariableTable[i] = localVariableTable[i].copy();
    187        }
    188        c.setConstantPool(_constant_pool);
    189        return c;
    190    }
    191
    192
    193    public final int getTableLength() {
    194        return localVariableTable == null ? 0 : localVariableTable.length;
    195    }
    196}