Source code

    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[] local_variable_table; // 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 name_index Index in constant pool to `LocalVariableTable'
    049     * @param length Content length in bytes
    050     * @param local_variable_table Table of local variables
    051     * @param constant_pool Array of constants
    052     */
    053    public LocalVariableTable(final int name_index, final int length, final LocalVariable[] local_variable_table,
    054            final ConstantPool constant_pool) {
    055        super(Const.ATTR_LOCAL_VARIABLE_TABLE, name_index, length, constant_pool);
    056        this.local_variable_table = local_variable_table;
    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        local_variable_table = new LocalVariable[local_variable_table_length];
    073        for (int i = 0; i < local_variable_table_length; i++) {
    074            local_variable_table[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(local_variable_table.length);
    102        for (final LocalVariable variable : local_variable_table) {
    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 local_variable_table;
    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 : local_variable_table) {
    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 : local_variable_table) {
    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.local_variable_table = 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 < local_variable_table.length; i++) {
    169            buf.append(local_variable_table[i]);
    170            if (i < local_variable_table.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.local_variable_table = new LocalVariable[local_variable_table.length];
    185        for (int i = 0; i < local_variable_table.length; i++) {
    186            c.local_variable_table[i] = local_variable_table[i].copy();
    187        }
    188        c.setConstantPool(_constant_pool);
    189        return c;
    190    }
    191
    192
    193    public final int getTableLength() {
    194        return local_variable_table == null ? 0 : local_variable_table.length;
    195    }
    196}