Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/org/apache/bcel/generic/INVOKEDYNAMIC.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class INVOKEDYNAMIC extends InvokeInstruction {
*/
public INVOKEDYNAMIC(final int index) {
super(Const.INVOKEDYNAMIC, index);
super.setLength(5);
}

/**
Expand Down
56 changes: 56 additions & 0 deletions src/test/java/org/apache/bcel/generic/INVOKEDYNAMICTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.bcel.generic;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

/**
* Tests {@link INVOKEDYNAMIC}.
*
* @see <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5.invokedynamic">The invokedynamic instruction</a>
*/
class INVOKEDYNAMICTest {

/**
* {@code invokedynamic} occupies 5 bytes (opcode, u2 index, two zero bytes), which is what {@code dump()} writes and
* {@code initFromFile()} sets, so the constructed instruction must also report a length of 5.
*/
@Test
void testConstructorSetsLength() {
assertEquals(5, new INVOKEDYNAMIC(1).getLength());
}

/**
* {@link InstructionList#setPositions()} adds up {@code getLength()} to place each instruction, so an understated
* invokedynamic length shifts the offset of every following instruction.
*/
@Test
void testInstructionListPositionsAccountForFullLength() {
final InstructionList il = new InstructionList();
il.append(new NOP());
il.append(new INVOKEDYNAMIC(1));
final InstructionHandle after = il.append(new NOP());
il.setPositions();
// NOP (1) + invokedynamic (5) places the following NOP at offset 6
assertEquals(6, after.getPosition());
}
}
Loading