1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.bcel.generic;
20
21 import static org.apache.bcel.generic.InstructionFactory.createArrayLoad;
22 import static org.apache.bcel.generic.InstructionFactory.createArrayStore;
23 import static org.apache.bcel.generic.InstructionFactory.createBinaryOperation;
24 import static org.apache.bcel.generic.InstructionFactory.createBranchInstruction;
25 import static org.apache.bcel.generic.InstructionFactory.createLoad;
26 import static org.apache.bcel.generic.InstructionFactory.createNull;
27 import static org.apache.bcel.generic.InstructionFactory.createReturn;
28 import static org.apache.bcel.generic.InstructionFactory.createStore;
29 import static org.junit.jupiter.api.Assertions.assertEquals;
30 import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
31
32 import org.apache.bcel.AbstractTest;
33 import org.apache.bcel.Const;
34 import org.apache.bcel.Repository;
35 import org.junit.jupiter.api.Test;
36
37 class InstructionFactoryTest extends AbstractTest {
38
39 @Test
40 void testArrayLoad() throws Exception {
41 assertEquals(InstructionConst.BALOAD, createArrayLoad(Type.BOOLEAN));
42 assertEquals(InstructionConst.BALOAD, createArrayLoad(Type.BYTE));
43 assertEquals(InstructionConst.CALOAD, createArrayLoad(Type.CHAR));
44 assertEquals(InstructionConst.SALOAD, createArrayLoad(Type.SHORT));
45 assertEquals(InstructionConst.IALOAD, createArrayLoad(Type.INT));
46 assertEquals(InstructionConst.FALOAD, createArrayLoad(Type.FLOAT));
47 assertEquals(InstructionConst.DALOAD, createArrayLoad(Type.DOUBLE));
48 assertEquals(InstructionConst.LALOAD, createArrayLoad(Type.LONG));
49 assertEquals(InstructionConst.AALOAD, createArrayLoad(Type.OBJECT));
50 assertEquals(InstructionConst.AALOAD, createArrayLoad(Type.getType("[I")));
51 }
52
53 @Test
54 void testArrayStore() throws Exception {
55 assertEquals(InstructionConst.BASTORE, createArrayStore(Type.BOOLEAN));
56 assertEquals(InstructionConst.BASTORE, createArrayStore(Type.BYTE));
57 assertEquals(InstructionConst.CASTORE, createArrayStore(Type.CHAR));
58 assertEquals(InstructionConst.SASTORE, createArrayStore(Type.SHORT));
59 assertEquals(InstructionConst.IASTORE, createArrayStore(Type.INT));
60 assertEquals(InstructionConst.FASTORE, createArrayStore(Type.FLOAT));
61 assertEquals(InstructionConst.DASTORE, createArrayStore(Type.DOUBLE));
62 assertEquals(InstructionConst.LASTORE, createArrayStore(Type.LONG));
63 assertEquals(InstructionConst.AASTORE, createArrayStore(Type.OBJECT));
64 assertEquals(InstructionConst.AASTORE, createArrayStore(Type.getType("[I")));
65 }
66
67 @Test
68 void testCreateInvokeNullArgTypes() throws Exception {
69 final InstructionFactory factory = new InstructionFactory(new ClassGen(Repository.lookupClass(Object.class)));
70 factory.createInvoke("", "", Type.VOID, null, Const.INVOKESPECIAL, false);
71 }
72
73 @Test
74 void testExceptions() throws Exception {
75 final InstructionFactory factory = new InstructionFactory(new ClassGen(Repository.lookupClass(Object.class)));
76 assertThrowsExactly(IllegalArgumentException.class, () -> createArrayLoad(Type.UNKNOWN));
77 assertThrowsExactly(IllegalArgumentException.class, () -> createArrayStore(Type.UNKNOWN));
78 assertThrowsExactly(IllegalArgumentException.class, () -> createBinaryOperation("$", Type.DOUBLE));
79 assertThrowsExactly(IllegalArgumentException.class, () -> createBinaryOperation("$", Type.FLOAT));
80 assertThrowsExactly(IllegalArgumentException.class, () -> createBinaryOperation("$", Type.INT));
81 assertThrowsExactly(IllegalArgumentException.class, () -> createBinaryOperation("$", Type.LONG));
82 assertThrowsExactly(IllegalArgumentException.class, () -> createBinaryOperation("*", Type.OBJECT));
83 assertThrowsExactly(IllegalArgumentException.class, () -> createBranchInstruction(Short.MIN_VALUE, null));
84 assertThrowsExactly(IllegalArgumentException.class, () -> createLoad(Type.UNKNOWN, 0));
85 assertThrowsExactly(IllegalArgumentException.class, () -> createNull(Type.UNKNOWN));
86 assertThrowsExactly(IllegalArgumentException.class, () -> createReturn(Type.UNKNOWN));
87 assertThrowsExactly(IllegalArgumentException.class, () -> createStore(Type.UNKNOWN, 0));
88 assertThrowsExactly(IllegalArgumentException.class, () -> factory.createAppend(Type.UNKNOWN));
89 assertThrowsExactly(IllegalArgumentException.class, () -> factory.createCast(Type.UNKNOWN, Type.UNKNOWN));
90 assertThrowsExactly(IllegalArgumentException.class, () -> factory.createFieldAccess("java.lang.System", "out", new ObjectType("java.io.PrintStream"), Const.NOP));
91 assertThrowsExactly(IllegalArgumentException.class, () -> factory.createInvoke("java.io.PrintStream", "println", Type.VOID, new Type[] { Type.STRING }, Const.NOP));
92 }
93
94 @Test
95 void testNull() throws Exception {
96 assertEquals(InstructionConst.ICONST_0, createNull(Type.BOOLEAN));
97 assertEquals(InstructionConst.ICONST_0, createNull(Type.BYTE));
98 assertEquals(InstructionConst.ICONST_0, createNull(Type.CHAR));
99 assertEquals(InstructionConst.ICONST_0, createNull(Type.SHORT));
100 assertEquals(InstructionConst.ICONST_0, createNull(Type.INT));
101 assertEquals(InstructionConst.FCONST_0, createNull(Type.FLOAT));
102 assertEquals(InstructionConst.DCONST_0, createNull(Type.DOUBLE));
103 assertEquals(InstructionConst.LCONST_0, createNull(Type.LONG));
104 assertEquals(InstructionConst.NOP, createNull(Type.VOID));
105 assertEquals(InstructionConst.ACONST_NULL, createNull(Type.OBJECT));
106 assertEquals(InstructionConst.ACONST_NULL, createNull(Type.getType("[I")));
107 }
108 }