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
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
027 * the names and the types of the local variables in
028 * a given method.
029 *
030 * @version $Id: LocalVariablesInfo.java 1749600 2016-06-21 20:43:38Z ggregory $
031 */
032public class LocalVariablesInfo{
033
034    /** The information about the local variables is stored here. */
035    private final LocalVariableInfo[] localVariableInfos;
036
037    /** The constructor. */
038    LocalVariablesInfo(final int max_locals) {
039        localVariableInfos = new LocalVariableInfo[max_locals];
040        for (int i=0; i<max_locals; i++) {
041            localVariableInfos[i] = new LocalVariableInfo();
042        }
043    }
044
045    /** Returns the LocalVariableInfo for the given slot. */
046    public LocalVariableInfo getLocalVariableInfo(final int slot) {
047        if (slot < 0 || slot >= localVariableInfos.length) {
048            throw new AssertionViolatedException("Slot number for local variable information out of range.");
049        }
050        return localVariableInfos[slot];
051    }
052
053    /**
054     * Adds information about the local variable in slot 'slot'. Automatically 
055     * adds information for slot+1 if 't' is Type.LONG or Type.DOUBLE.
056     * @throws LocalVariableInfoInconsistentException if the new information conflicts
057     *         with already gathered information.
058     */
059    public void add(final int slot, final String name, final int startpc, final int length, final Type t) throws LocalVariableInfoInconsistentException{
060        // The add operation on LocalVariableInfo may throw the '...Inconsistent...' exception, we don't throw it explicitely here.
061
062        if (slot < 0 || slot >= localVariableInfos.length) {
063            throw new AssertionViolatedException("Slot number for local variable information out of range.");
064        }
065
066        localVariableInfos[slot].add(name, startpc, length, t);
067        if (t == Type.LONG) {
068            localVariableInfos[slot+1].add(name, startpc, length, LONG_Upper.theInstance());
069        }
070        if (t == Type.DOUBLE) {
071            localVariableInfos[slot+1].add(name, startpc, length, DOUBLE_Upper.theInstance());
072        }
073    }
074}