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
20 package org.apache.bcel.classfile;
21
22 import static org.junit.jupiter.api.Assertions.assertNull;
23 import static org.junit.jupiter.api.Assertions.assertSame;
24
25 import org.junit.jupiter.api.Test;
26
27 /**
28 * Tests {@link LocalVariableTable#getLocalVariable(int, int)}.
29 */
30 class LocalVariableTableTest {
31
32 /**
33 * The live range of a local variable is the half-open interval {@code [start_pc, start_pc + length)} per
34 * <a href="https://docs.oracle.com/javase/specs/jvms/se25/html/jvms-4.html#jvms-4.7.13">JVMS 4.7.13</a>, so a pc equal to
35 * {@code start_pc + length} is out of scope and must not match.
36 */
37 @Test
38 void testGetLocalVariableLiveRangeEndIsExclusive() {
39 final ConstantPool constantPool = new ConstantPool(new Constant[] {null, new ConstantUtf8("i"), new ConstantUtf8("I")});
40 // Variable in slot 1, live at pcs 2, 3, 4 (start 2, length 3), with valid name and signature indices.
41 final LocalVariable variable = new LocalVariable(2, 3, 1, 2, 1, constantPool);
42 final LocalVariableTable table = new LocalVariableTable(0, 0, new LocalVariable[] {variable}, constantPool);
43 assertNull(table.getLocalVariable(1, 1), "pc before start");
44 assertSame(variable, table.getLocalVariable(1, 2), "pc at start");
45 assertSame(variable, table.getLocalVariable(1, 4), "pc at last valid position");
46 assertNull(table.getLocalVariable(1, 5), "pc at start + length is out of scope");
47 }
48 }