    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.verifier.statics;
    019
    020import java.util.Arrays;
    021
    022import org.apache.bcel.generic.Type;
    023import org.apache.bcel.verifier.exc.AssertionViolatedException;
    024import org.apache.bcel.verifier.exc.LocalVariableInfoInconsistentException;
    025
    026/**
    027 * A utility class holding the information about the names and the types of the local variables in a given method.
    028 */
    029public class LocalVariablesInfo {
    030
    031    /** The information about the local variables is stored here. */
    032    private final LocalVariableInfo[] localVariableInfos;
    033
    034    /** The constructor. */
    035    LocalVariablesInfo(final int max_locals) {
    036        localVariableInfos = new LocalVariableInfo[max_locals];
    037        Arrays.setAll(localVariableInfos, i -> new LocalVariableInfo());
    038    }
    039
    040    /**
    041     * Adds information about the local variable in slot 'slot'. Automatically adds information for slot+1 if 't' is
    042     * Type.LONG or Type.DOUBLE.
    043     *
    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 explicitely
    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}