1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.bcel.verifier.tests;
18
19 import static org.junit.jupiter.api.Assertions.assertNotNull;
20
21 import java.io.IOException;
22 import java.io.OutputStream;
23
24 import org.apache.bcel.Const;
25 import org.apache.bcel.generic.ClassGen;
26 import org.apache.bcel.generic.ConstantPoolGen;
27 import org.apache.bcel.generic.InstructionConst;
28 import org.apache.bcel.generic.InstructionFactory;
29 import org.apache.bcel.generic.InstructionHandle;
30 import org.apache.bcel.generic.InstructionList;
31 import org.apache.bcel.generic.MethodGen;
32 import org.apache.bcel.generic.ObjectType;
33 import org.apache.bcel.generic.PUSH;
34 import org.apache.bcel.generic.Type;
35
36 public class TestArrayAccess03Creator extends TestCreator {
37 private final InstructionFactory factory;
38 private final ConstantPoolGen cp;
39 private final ClassGen cg;
40
41 public TestArrayAccess03Creator() {
42 cg = new ClassGen(TEST_PACKAGE + ".TestArrayAccess03", "java.lang.Object", "TestArrayAccess03.java", Const.ACC_PUBLIC | Const.ACC_SUPER,
43 new String[] {});
44
45 cp = cg.getConstantPool();
46 factory = new InstructionFactory(cg, cp);
47 }
48
49 @Override
50 public void create(final OutputStream out) throws IOException {
51 createMethod_0();
52 createMethod_1();
53 cg.getJavaClass().dump(out);
54 }
55
56 private void createMethod_0() {
57 final InstructionList il = new InstructionList();
58 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] {}, "<init>", TEST_PACKAGE + ".TestArrayAccess03", il,
59 cp);
60
61 final InstructionHandle ih_0 = il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
62 assertNotNull(ih_0);
63 il.append(factory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL));
64 final InstructionHandle ih_4 = il.append(InstructionFactory.createReturn(Type.VOID));
65 assertNotNull(ih_4);
66 method.setMaxStack();
67 method.setMaxLocals();
68 cg.addMethod(method.getMethod());
69 il.dispose();
70 }
71
72 private void createMethod_1() {
73 final InstructionList il = new InstructionList();
74 final MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.VOID, new Type[] {Type.OBJECT}, new String[] {"arg0"}, "test",
75 TEST_PACKAGE + ".TestArrayAccess03", il, cp);
76
77 final InstructionHandle ih_0 = il.append(new PUSH(cp, 1));
78 assertNotNull(ih_0);
79 il.append(factory.createNewArray(new ObjectType(TEST_PACKAGE + ".TestArrayAccess03"), (short) 1));
80 il.append(InstructionFactory.createStore(Type.OBJECT, 1));
81 final InstructionHandle ih_5 = il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
82 assertNotNull(ih_5);
83 il.append(new PUSH(cp, 0));
84 il.append(factory.createNew(TEST_PACKAGE + ".TestArrayAccess03"));
85 il.append(InstructionConst.DUP);
86 il.append(factory.createInvoke(TEST_PACKAGE + ".TestArrayAccess03", "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL));
87 il.append(InstructionConst.AASTORE);
88 final InstructionHandle ih_15 = il.append(InstructionFactory.createReturn(Type.VOID));
89 assertNotNull(ih_15);
90 method.setMaxStack();
91 method.setMaxLocals();
92 cg.addMethod(method.getMethod());
93 il.dispose();
94 }
95 }