    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one
    003 * or more contributor license agreements.  See the NOTICE file
    004 * distributed with this work for additional information
    005 * regarding copyright ownership.  The ASF licenses this file
    006 * to you under the Apache License, Version 2.0 (the
    007 * "License"); you may not use this file except in compliance
    008 * with the License.  You may obtain a copy of the License at
    009 *
    010 *   https://www.apache.org/licenses/LICENSE-2.0
    011 *
    012 * Unless required by applicable law or agreed to in writing,
    013 * software distributed under the License is distributed on an
    014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    015 * KIND, either express or implied.  See the License for the
    016 * specific language governing permissions and limitations
    017 * under the License.
    018 */
    019package org.apache.bcel.classfile;
    020
    021import java.io.DataInput;
    022import java.io.DataOutputStream;
    023import java.io.IOException;
    024import java.util.Arrays;
    025import java.util.Iterator;
    026import java.util.stream.Stream;
    027
    028import org.apache.bcel.Const;
    029import org.apache.bcel.util.Args;
    030
    031/**
    032 * This class represents colection of local variables in a method. This attribute is contained in the <em>Code</em>
    033 * attribute.
    034 *
    035 * @see Code
    036 * @see LocalVariable
    037 */
    038public class LocalVariableTable extends Attribute implements Iterable<LocalVariable> {
    039
    040    private static final LocalVariable[] EMPTY_ARRAY = {};
    041
    042    private LocalVariable[] localVariableTable; // variables
    043
    044    /**
    045     * Constructs object from input stream.
    046     *
    047     * @param nameIndex Index in constant pool.
    048     * @param length Content length in bytes.
    049     * @param input Input stream.
    050     * @param constantPool Array of constants.
    051     * @throws IOException if an I/O error occurs.
    052     */
    053    LocalVariableTable(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
    054        this(nameIndex, length, (LocalVariable[]) null, constantPool);
    055        final int localVariableTableLength = input.readUnsignedShort();
    056        localVariableTable = new LocalVariable[localVariableTableLength];
    057        for (int i = 0; i < localVariableTableLength; i++) {
    058            localVariableTable[i] = new LocalVariable(input, constantPool);
    059        }
    060    }
    061
    062    /**
    063     * @param nameIndex Index in constant pool to 'LocalVariableTable'.
    064     * @param length Content length in bytes.
    065     * @param localVariableTable Table of local variables.
    066     * @param constantPool Array of constants.
    067     */
    068    public LocalVariableTable(final int nameIndex, final int length, final LocalVariable[] localVariableTable, final ConstantPool constantPool) {
    069        super(Const.ATTR_LOCAL_VARIABLE_TABLE, nameIndex, length, constantPool);
    070        this.localVariableTable = localVariableTable != null ? localVariableTable : EMPTY_ARRAY;
    071        Args.requireU2(this.localVariableTable.length, "localVariableTable.length");
    072    }
    073
    074    /**
    075     * Initialize from another object. Note that both objects use the same references (shallow copy). Use copy() for a
    076     * physical copy.
    077     *
    078     * @param c Source to copy.
    079     */
    080    public LocalVariableTable(final LocalVariableTable c) {
    081        this(c.getNameIndex(), c.getLength(), c.getLocalVariableTable(), c.getConstantPool());
    082    }
    083
    084    /**
    085     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
    086     * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
    087     *
    088     * @param v Visitor object.
    089     */
    090    @Override
    091    public void accept(final Visitor v) {
    092        v.visitLocalVariableTable(this);
    093    }
    094
    095    /**
    096     * @return deep copy of this attribute.
    097     */
    098    @Override
    099    public Attribute copy(final ConstantPool constantPool) {
    100        final LocalVariableTable c = (LocalVariableTable) clone();
    101        c.localVariableTable = new LocalVariable[localVariableTable.length];
    102        Arrays.setAll(c.localVariableTable, i -> localVariableTable[i].copy());
    103        c.setConstantPool(constantPool);
    104        return c;
    105    }
    106
    107    /**
    108     * Dumps local variable table attribute to file stream in binary format.
    109     *
    110     * @param file Output file stream.
    111     * @throws IOException if an I/O error occurs.
    112     */
    113    @Override
    114    public final void dump(final DataOutputStream file) throws IOException {
    115        super.dump(file);
    116        file.writeShort(localVariableTable.length);
    117        for (final LocalVariable variable : localVariableTable) {
    118            variable.dump(file);
    119        }
    120    }
    121
    122    /**
    123     * @param index the variable slot.
    124     * @return the first LocalVariable that matches the slot or null if not found.
    125     * @deprecated Since 5.2 because multiple variables can share the same slot, use getLocalVariable(int index, int pc) instead.
    126     */
    127    @java.lang.Deprecated
    128    public final LocalVariable getLocalVariable(final int index) {
    129        for (final LocalVariable variable : localVariableTable) {
    130            if (variable.getIndex() == index) {
    131                return variable;
    132            }
    133        }
    134        return null;
    135    }
    136
    137    /**
    138     *
    139     * @param index the variable slot.
    140     * @param pc the current pc that this variable is alive.
    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 startPc = variable.getStartPC();
    147                final int endPc = startPc + variable.getLength();
    148                if (pc >= startPc && pc <= endPc) {
    149                    return variable;
    150                }
    151            }
    152        }
    153        return null;
    154    }
    155
    156    /**
    157     * @return Array of local variables of method.
    158     */
    159    public final LocalVariable[] getLocalVariableTable() {
    160        return localVariableTable;
    161    }
    162
    163    public final int getTableLength() {
    164        return localVariableTable.length;
    165    }
    166
    167    @Override
    168    public Iterator<LocalVariable> iterator() {
    169        return Stream.of(localVariableTable).iterator();
    170    }
    171
    172    public final void setLocalVariableTable(final LocalVariable[] localVariableTable) {
    173        this.localVariableTable = localVariableTable != null ? localVariableTable : EMPTY_ARRAY;
    174    }
    175
    176    /**
    177     * @return String representation.
    178     */
    179    @Override
    180    public final String toString() {
    181        final StringBuilder buf = new StringBuilder();
    182        for (int i = 0; i < localVariableTable.length; i++) {
    183            buf.append(localVariableTable[i]);
    184            if (i < localVariableTable.length - 1) {
    185                buf.append('\n');
    186            }
    187        }
    188        return buf.toString();
    189    }
    190}