1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18 package org.apache.bcel.verifier.statics;
19
20
21 import org.apache.bcel.generic.Type;
22 import org.apache.bcel.verifier.exc.AssertionViolatedException;
23 import org.apache.bcel.verifier.exc.LocalVariableInfoInconsistentException;
24
25 /**
26 * A utility class holding the information about
27 * the names and the types of the local variables in
28 * a given method.
29 *
30 * @version $Id: LocalVariablesInfo.java 947879 2010-05-25 00:48:30Z sebb $
31 * @author Enver Haase
32 */
33 public class LocalVariablesInfo{
34
35 /** The information about the local variables is stored here. */
36 private LocalVariableInfo[] localVariableInfos;
37
38 /** The constructor. */
39 LocalVariablesInfo(int max_locals){
40 localVariableInfos = new LocalVariableInfo[max_locals];
41 for (int i=0; i<max_locals; i++){
42 localVariableInfos[i] = new LocalVariableInfo();
43 }
44 }
45
46 /** Returns the LocalVariableInfo for the given slot. */
47 public LocalVariableInfo getLocalVariableInfo(int slot){
48 if (slot < 0 || slot >= localVariableInfos.length){
49 throw new AssertionViolatedException("Slot number for local variable information out of range.");
50 }
51 return localVariableInfos[slot];
52 }
53
54 /**
55 * Adds information about the local variable in slot 'slot'. Automatically
56 * adds information for slot+1 if 't' is Type.LONG or Type.DOUBLE.
57 * @throws LocalVariableInfoInconsistentException if the new information conflicts
58 * with already gathered information.
59 */
60 public void add(int slot, String name, int startpc, int length, Type t) throws LocalVariableInfoInconsistentException{
61 // The add operation on LocalVariableInfo may throw the '...Inconsistent...' exception, we don't throw it explicitely here.
62
63 if (slot < 0 || slot >= localVariableInfos.length){
64 throw new AssertionViolatedException("Slot number for local variable information out of range.");
65 }
66
67 localVariableInfos[slot].add(name, startpc, length, t);
68 if (t == Type.LONG) {
69 localVariableInfos[slot+1].add(name, startpc, length, LONG_Upper.theInstance());
70 }
71 if (t == Type.DOUBLE) {
72 localVariableInfos[slot+1].add(name, startpc, length, DOUBLE_Upper.theInstance());
73 }
74 }
75 }