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