View Javadoc
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.html 992795 2016-07-14 11:53:52Z britter $
31   */
32  public class LocalVariablesInfo{
33  
34      /** The information about the local variables is stored here. */
35      private final LocalVariableInfo[] localVariableInfos;
36  
37      /** The constructor. */
38      LocalVariablesInfo(final int max_locals) {
39          localVariableInfos = new LocalVariableInfo[max_locals];
40          for (int i=0; i<max_locals; i++) {
41              localVariableInfos[i] = new LocalVariableInfo();
42          }
43      }
44  
45      /** Returns the LocalVariableInfo for the given slot. */
46      public LocalVariableInfo getLocalVariableInfo(final int slot) {
47          if (slot < 0 || slot >= localVariableInfos.length) {
48              throw new AssertionViolatedException("Slot number for local variable information out of range.");
49          }
50          return localVariableInfos[slot];
51      }
52  
53      /**
54       * Adds information about the local variable in slot 'slot'. Automatically 
55       * adds information for slot+1 if 't' is Type.LONG or Type.DOUBLE.
56       * @throws LocalVariableInfoInconsistentException if the new information conflicts
57       *         with already gathered information.
58       */
59      public void add(final int slot, final String name, final int startpc, final int length, final Type t) throws LocalVariableInfoInconsistentException{
60          // The add operation on LocalVariableInfo may throw the '...Inconsistent...' exception, we don't throw it explicitely here.
61  
62          if (slot < 0 || slot >= localVariableInfos.length) {
63              throw new AssertionViolatedException("Slot number for local variable information out of range.");
64          }
65  
66          localVariableInfos[slot].add(name, startpc, length, t);
67          if (t == Type.LONG) {
68              localVariableInfos[slot+1].add(name, startpc, length, LONG_Upper.theInstance());
69          }
70          if (t == Type.DOUBLE) {
71              localVariableInfos[slot+1].add(name, startpc, length, DOUBLE_Upper.theInstance());
72          }
73      }
74  }