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 */
017package org.apache.bcel.verifier.statics;
018
019import java.util.Arrays;
020
021import org.apache.bcel.generic.Type;
022import org.apache.bcel.verifier.exc.AssertionViolatedException;
023import org.apache.bcel.verifier.exc.LocalVariableInfoInconsistentException;
024
025/**
026 * A utility class holding the information about the names and the types of the local variables in a given method.
027 */
028public class LocalVariablesInfo {
029
030    /** The information about the local variables is stored here. */
031    private final LocalVariableInfo[] localVariableInfos;
032
033    /** The constructor. */
034    LocalVariablesInfo(final int maxLocals) {
035        localVariableInfos = new LocalVariableInfo[maxLocals];
036        Arrays.setAll(localVariableInfos, i -> new LocalVariableInfo());
037    }
038
039    /**
040     * Adds information about the local variable in slot 'slot'. Automatically adds information for slot+1 if 't' is
041     * Type.LONG or Type.DOUBLE.
042     *
043     * @param slot Slot number for local variable information
044     * @param name variable name
045     * @param startPc Range in which the variable is valid.
046     * @param length length of ...
047     * @param type variable type
048     * @throws LocalVariableInfoInconsistentException if the new information conflicts with already gathered information.
049     */
050    public void add(final int slot, final String name, final int startPc, final int length, final Type type) throws LocalVariableInfoInconsistentException {
051        // The add operation on LocalVariableInfo may throw the '...Inconsistent...' exception, we don't throw it explicitly
052        // here.
053
054        if (slot < 0 || slot >= localVariableInfos.length) {
055            throw new AssertionViolatedException("Slot number for local variable information out of range.");
056        }
057
058        localVariableInfos[slot].add(name, startPc, length, type);
059        if (type == Type.LONG) {
060            localVariableInfos[slot + 1].add(name, startPc, length, LONG_Upper.theInstance());
061        }
062        if (type == Type.DOUBLE) {
063            localVariableInfos[slot + 1].add(name, startPc, length, DOUBLE_Upper.theInstance());
064        }
065    }
066
067    /**
068     * Returns the LocalVariableInfo for the given slot.
069     *
070     * @param slot Slot to query.
071     * @return The LocalVariableInfo for the given slot.
072     */
073    public LocalVariableInfo getLocalVariableInfo(final int slot) {
074        if (slot < 0 || slot >= localVariableInfos.length) {
075            throw new AssertionViolatedException("Slot number for local variable information out of range.");
076        }
077        return localVariableInfos[slot];
078    }
079}