This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-bcel.git
The following commit(s) were added to refs/heads/master by this push:
new 3785212a Internal refactoring
3785212a is described below
commit 3785212afe9483e1a182a8ec8decfb930652231a
Author: Gary Gregory <[email protected]>
AuthorDate: Sat Jul 18 07:55:08 2026 -0700
Internal refactoring
---
.../java/org/apache/bcel/generic/Instruction.java | 28 +++++++++++++++++++++
.../apache/bcel/generic/INVOKEINTERFACETest.java | 8 +-----
.../bcel/generic/LocalVariableInstructionTest.java | 29 +++++-----------------
3 files changed, 35 insertions(+), 30 deletions(-)
diff --git a/src/main/java/org/apache/bcel/generic/Instruction.java
b/src/main/java/org/apache/bcel/generic/Instruction.java
index e7123109..3e07fce9 100644
--- a/src/main/java/org/apache/bcel/generic/Instruction.java
+++ b/src/main/java/org/apache/bcel/generic/Instruction.java
@@ -18,6 +18,7 @@
*/
package org.apache.bcel.generic;
+import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
@@ -67,6 +68,19 @@ public abstract class Instruction implements Cloneable {
return value >= Short.MIN_VALUE && value <= Short.MAX_VALUE;
}
+ /**
+ * Reads an instruction from (byte code) input stream and return the
appropriate object.
+ *
+ * @param code byte array containing the instruction to read.
+ * @return instruction object being read.
+ * @throws IOException Thrown when an I/O exception of some sort has
occurred.
+ */
+ static Instruction readInstruction(final byte[] code) throws IOException {
+ try (ByteSequence bytes = new ByteSequence(code)) {
+ return readInstruction(bytes);
+ }
+ }
+
/**
* Reads an instruction from (byte code) input stream and return the
appropriate object.
* <p>
@@ -495,6 +509,20 @@ public abstract class Instruction implements Cloneable {
out.writeByte(opcode); // Common for all instructions
}
+ /**
+ * Dumps this instruction to a byte array.
+ *
+ * @return the byte array containing the dumped instruction
+ * @throws IOException if an I/O error occurs.
+ */
+ byte[] dumpToByteArray() throws IOException {
+ final ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ try (DataOutputStream dos = new DataOutputStream(bos)) {
+ dump(dos);
+ }
+ return bos.toByteArray();
+ }
+
/**
* Tests for equality, delegated to comparator
*
diff --git a/src/test/java/org/apache/bcel/generic/INVOKEINTERFACETest.java
b/src/test/java/org/apache/bcel/generic/INVOKEINTERFACETest.java
index 5d5ac4cd..9d8525df 100644
--- a/src/test/java/org/apache/bcel/generic/INVOKEINTERFACETest.java
+++ b/src/test/java/org/apache/bcel/generic/INVOKEINTERFACETest.java
@@ -22,8 +22,6 @@ package org.apache.bcel.generic;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
-import java.io.ByteArrayOutputStream;
-import java.io.DataOutputStream;
import java.io.IOException;
import org.apache.bcel.Const;
@@ -37,12 +35,8 @@ import org.junit.jupiter.api.Test;
class INVOKEINTERFACETest {
private static int dumpedCountByte(final INVOKEINTERFACE instruction)
throws IOException {
- final ByteArrayOutputStream bos = new ByteArrayOutputStream();
- try (DataOutputStream dos = new DataOutputStream(bos)) {
- instruction.dump(dos);
- }
// opcode, u2 index, u1 count, u1 zero
- return bos.toByteArray()[3] & 0xFF;
+ return instruction.dumpToByteArray()[3] & 0xFF;
}
@Test
diff --git
a/src/test/java/org/apache/bcel/generic/LocalVariableInstructionTest.java
b/src/test/java/org/apache/bcel/generic/LocalVariableInstructionTest.java
index 77cf7b0d..76931894 100644
--- a/src/test/java/org/apache/bcel/generic/LocalVariableInstructionTest.java
+++ b/src/test/java/org/apache/bcel/generic/LocalVariableInstructionTest.java
@@ -21,12 +21,9 @@ package org.apache.bcel.generic;
import static org.junit.jupiter.api.Assertions.assertEquals;
-import java.io.ByteArrayOutputStream;
-import java.io.DataOutputStream;
import java.io.IOException;
import org.apache.bcel.Const;
-import org.apache.bcel.util.ByteSequence;
import org.junit.jupiter.api.Test;
/**
@@ -34,30 +31,16 @@ import org.junit.jupiter.api.Test;
*/
class LocalVariableInstructionTest {
- private static int dumpedLength(final Instruction instruction) throws
IOException {
- final ByteArrayOutputStream bos = new ByteArrayOutputStream();
- try (DataOutputStream dos = new DataOutputStream(bos)) {
- instruction.dump(dos);
- }
- return bos.toByteArray().length;
- }
-
- private static Instruction readInstruction(final byte[] code) throws
IOException {
- try (ByteSequence bytes = new ByteSequence(code)) {
- return Instruction.readInstruction(bytes);
- }
- }
-
/**
* A {@code wide} prefix whose index does not fit in a byte must keep the
four-byte encoding.
*/
@Test
void testWideLoadWithLargeIndexStaysWide() throws IOException {
// wide, iload, index 0x012C (300)
- final Instruction instruction = readInstruction(new byte[] {(byte)
Const.WIDE, (byte) Const.ILOAD, 0x01, 0x2C});
+ final Instruction instruction = Instruction.readInstruction(new byte[]
{(byte) Const.WIDE, (byte) Const.ILOAD, 0x01, 0x2C});
assertEquals(300, ((ILOAD) instruction).getIndex());
assertEquals(4, instruction.getLength());
- assertEquals(instruction.getLength(), dumpedLength(instruction));
+ assertEquals(instruction.getLength(),
instruction.dumpToByteArray().length);
}
/**
@@ -67,9 +50,9 @@ class LocalVariableInstructionTest {
@Test
void testWideLoadWithSmallIndexLengthMatchesDump() throws IOException {
// wide, iload, index 0x0005
- final Instruction instruction = readInstruction(new byte[] {(byte)
Const.WIDE, (byte) Const.ILOAD, 0x00, 0x05});
+ final Instruction instruction = Instruction.readInstruction(new byte[]
{(byte) Const.WIDE, (byte) Const.ILOAD, 0x00, 0x05});
assertEquals(5, ((ILOAD) instruction).getIndex());
- assertEquals(dumpedLength(instruction), instruction.getLength());
+ assertEquals(instruction.dumpToByteArray().length,
instruction.getLength());
}
/**
@@ -78,8 +61,8 @@ class LocalVariableInstructionTest {
@Test
void testWideStoreWithSmallIndexLengthMatchesDump() throws IOException {
// wide, istore, index 0x0007
- final Instruction instruction = readInstruction(new byte[] {(byte)
Const.WIDE, (byte) Const.ISTORE, 0x00, 0x07});
+ final Instruction instruction = Instruction.readInstruction(new byte[]
{(byte) Const.WIDE, (byte) Const.ISTORE, 0x00, 0x07});
assertEquals(7, ((ISTORE) instruction).getIndex());
- assertEquals(dumpedLength(instruction), instruction.getLength());
+ assertEquals(instruction.dumpToByteArray().length,
instruction.getLength());
}
}