1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.bcel.verifier.tests;
20
21 import static org.junit.jupiter.api.Assertions.assertNotNull;
22
23 import java.io.IOException;
24 import java.io.OutputStream;
25
26 import org.apache.bcel.Const;
27 import org.apache.bcel.generic.ClassGen;
28 import org.apache.bcel.generic.ConstantPoolGen;
29 import org.apache.bcel.generic.InstructionConst;
30 import org.apache.bcel.generic.InstructionFactory;
31 import org.apache.bcel.generic.InstructionHandle;
32 import org.apache.bcel.generic.InstructionList;
33 import org.apache.bcel.generic.MethodGen;
34 import org.apache.bcel.generic.Type;
35
36 public abstract class TestReturn03Creator extends TestCreator {
37 private final InstructionFactory instructionFactory;
38 private final ConstantPoolGen constantPoolGen;
39 private final ClassGen classGen;
40 private final Type returnType;
41 private final String genClassSuffix;
42
43 protected TestReturn03Creator(final Type returnType, final String genClassSuffix) {
44 classGen = new ClassGen(TEST_PACKAGE + ".TestReturn03" + genClassSuffix, "java.lang.Object", "TestReturn03.java", Const.ACC_PUBLIC | Const.ACC_SUPER,
45 new String[] {});
46
47 constantPoolGen = classGen.getConstantPool();
48 instructionFactory = new InstructionFactory(classGen, constantPoolGen);
49 this.returnType = returnType;
50 this.genClassSuffix = genClassSuffix;
51 }
52
53 @Override
54 public void create(final OutputStream out) throws IOException {
55 createMethod_0();
56 createMethod_1();
57 classGen.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 + ".TestReturn03", il,
63 constantPoolGen);
64
65 final InstructionHandle ih_0 = il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
66 assertNotNull(ih_0);
67 il.append(instructionFactory.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 classGen.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.INT, Type.NO_ARGS, new String[] {}, "test3",
79 TEST_PACKAGE + ".TestReturn03" + genClassSuffix, il, constantPoolGen);
80
81 final InstructionHandle ih_0 = il.append(InstructionConst.ACONST_NULL);
82 assertNotNull(ih_0);
83 il.append(InstructionFactory.createReturn(returnType));
84 method.setMaxStack();
85 method.setMaxLocals();
86 classGen.addMethod(method.getMethod());
87 il.dispose();
88 }
89 }