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