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     */ 
018    package org.apache.bcel.verifier.statics;
019    
020    
021    import org.apache.bcel.generic.Type;
022    import org.apache.bcel.verifier.exc.AssertionViolatedException;
023    import 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 947879 2010-05-25 00:48:30Z sebb $
031     * @author Enver Haase
032     */
033    public class LocalVariablesInfo{
034            
035            /** The information about the local variables is stored here. */
036            private LocalVariableInfo[] localVariableInfos;
037    
038            /** The constructor. */
039            LocalVariablesInfo(int max_locals){
040                    localVariableInfos = new LocalVariableInfo[max_locals];
041                    for (int i=0; i<max_locals; i++){
042                            localVariableInfos[i] = new LocalVariableInfo();
043                    }
044            }
045    
046            /** Returns the LocalVariableInfo for the given slot. */
047            public LocalVariableInfo getLocalVariableInfo(int slot){
048                    if (slot < 0 || slot >= localVariableInfos.length){
049                            throw new AssertionViolatedException("Slot number for local variable information out of range.");
050                    }
051                    return localVariableInfos[slot];
052            }
053    
054            /**
055             * Adds information about the local variable in slot 'slot'. Automatically 
056             * adds information for slot+1 if 't' is Type.LONG or Type.DOUBLE.
057             * @throws LocalVariableInfoInconsistentException if the new information conflicts
058             *         with already gathered information.
059             */
060            public void add(int slot, String name, int startpc, int length, Type t) throws LocalVariableInfoInconsistentException{
061                    // The add operation on LocalVariableInfo may throw the '...Inconsistent...' exception, we don't throw it explicitely here.
062                    
063                    if (slot < 0 || slot >= localVariableInfos.length){
064                            throw new AssertionViolatedException("Slot number for local variable information out of range.");
065                    }
066    
067                    localVariableInfos[slot].add(name, startpc, length, t);
068                    if (t == Type.LONG) {
069                localVariableInfos[slot+1].add(name, startpc, length, LONG_Upper.theInstance());
070            }
071                    if (t == Type.DOUBLE) {
072                localVariableInfos[slot+1].add(name, startpc, length, DOUBLE_Upper.theInstance());
073            }
074            }
075    }