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  package org.apache.bcel.verifier.statics;
18  
19  import java.util.Arrays;
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 the names and the types of the local variables in a given method.
27   */
28  public class LocalVariablesInfo {
29  
30      /** The information about the local variables is stored here. */
31      private final LocalVariableInfo[] localVariableInfos;
32  
33      /** The constructor. */
34      LocalVariablesInfo(final int maxLocals) {
35          localVariableInfos = new LocalVariableInfo[maxLocals];
36          Arrays.setAll(localVariableInfos, i -> new LocalVariableInfo());
37      }
38  
39      /**
40       * Adds information about the local variable in slot 'slot'. Automatically adds information for slot+1 if 't' is
41       * Type.LONG or Type.DOUBLE.
42       *
43       * @param slot Slot number for local variable information
44       * @param name variable name
45       * @param startPc Range in which the variable is valid.
46       * @param length length of ...
47       * @param type variable type
48       * @throws LocalVariableInfoInconsistentException if the new information conflicts with already gathered information.
49       */
50      public void add(final int slot, final String name, final int startPc, final int length, final Type type) throws LocalVariableInfoInconsistentException {
51          // The add operation on LocalVariableInfo may throw the '...Inconsistent...' exception, we don't throw it explicitly
52          // here.
53  
54          if (slot < 0 || slot >= localVariableInfos.length) {
55              throw new AssertionViolatedException("Slot number for local variable information out of range.");
56          }
57  
58          localVariableInfos[slot].add(name, startPc, length, type);
59          if (type == Type.LONG) {
60              localVariableInfos[slot + 1].add(name, startPc, length, LONG_Upper.theInstance());
61          }
62          if (type == Type.DOUBLE) {
63              localVariableInfos[slot + 1].add(name, startPc, length, DOUBLE_Upper.theInstance());
64          }
65      }
66  
67      /**
68       * Returns the LocalVariableInfo for the given slot.
69       *
70       * @param slot Slot to query.
71       * @return The LocalVariableInfo for the given slot.
72       */
73      public LocalVariableInfo getLocalVariableInfo(final int slot) {
74          if (slot < 0 || slot >= localVariableInfos.length) {
75              throw new AssertionViolatedException("Slot number for local variable information out of range.");
76          }
77          return localVariableInfos[slot];
78      }
79  }