1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.bcel.verifier.tests;
21
22 import java.io.IOException;
23 import java.io.OutputStream;
24
25 import org.apache.bcel.Const;
26 import org.apache.bcel.classfile.JavaClass;
27 import org.apache.bcel.generic.ArrayInstruction;
28 import org.apache.bcel.generic.ClassGen;
29 import org.apache.bcel.generic.ConstantPoolGen;
30 import org.apache.bcel.generic.Instruction;
31 import org.apache.bcel.generic.InstructionConst;
32 import org.apache.bcel.generic.InstructionFactory;
33 import org.apache.bcel.generic.InstructionList;
34 import org.apache.bcel.generic.MethodGen;
35 import org.apache.bcel.generic.PUSH;
36 import org.apache.bcel.generic.Type;
37
38
39
40
41
42
43 public class TestArrayAccess07Creator extends TestCreator {
44 private final InstructionFactory factory;
45 private final ConstantPoolGen cp;
46 private final ClassGen cg;
47 private final ArrayInstruction arrayInstruction;
48 private final Type arrayElementType;
49 private final String simpleClassName;
50
51 public TestArrayAccess07Creator(final ArrayInstruction arrayInstruction, final Type arrayElementType) {
52 this.arrayInstruction = arrayInstruction;
53 this.arrayElementType = arrayElementType;
54 final String name = arrayInstruction.getName();
55 simpleClassName = "TestArrayAccess07" + Character.toUpperCase(name.charAt(0)) + name.substring(1);
56 cg = new ClassGen(TEST_PACKAGE + "." + simpleClassName, "java.lang.Object", simpleClassName + ".java", Const.ACC_PUBLIC | Const.ACC_SUPER,
57 new String[] {});
58 cp = cg.getConstantPool();
59 factory = new InstructionFactory(cg, cp);
60 }
61
62 @Override
63 public void create(final OutputStream out) throws IOException {
64 createMethod_0();
65 createMethod_1();
66 cg.getJavaClass().dump(out);
67 }
68
69 private void createMethod_0() {
70 final InstructionList il = new InstructionList();
71 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] {}, "<init>", TEST_PACKAGE + "." + simpleClassName, il,
72 cp);
73 il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
74 il.append(factory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL));
75 il.append(InstructionFactory.createReturn(Type.VOID));
76 method.setMaxStack();
77 method.setMaxLocals();
78 cg.addMethod(method.getMethod());
79 il.dispose();
80 }
81
82 private void createMethod_1() {
83 final InstructionList il = new InstructionList();
84 final MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.VOID, Type.NO_ARGS, new String[] {}, "test",
85 TEST_PACKAGE + "." + simpleClassName, il, cp);
86
87 il.append(new PUSH(cp, 1));
88 il.append(factory.createNewArray(arrayElementType, (short) 1));
89 il.append(new PUSH(cp, 0));
90 if (arrayInstruction.produceStack(cp) == 0) {
91
92 il.append(storedValue());
93 il.append(arrayInstruction);
94 } else {
95
96 il.append(arrayInstruction);
97 il.append(arrayInstruction.produceStack(cp) == 2 ? InstructionConst.POP2 : InstructionConst.POP);
98 }
99 il.append(InstructionFactory.createReturn(Type.VOID));
100 method.setMaxStack();
101 method.setMaxLocals();
102 cg.addMethod(method.getMethod());
103 il.dispose();
104 }
105
106 @Override
107 protected String getClassName() {
108 return simpleClassName + JavaClass.EXTENSION;
109 }
110
111 public String getSimpleClassName() {
112 return simpleClassName;
113 }
114
115 private Instruction storedValue() {
116 switch (arrayInstruction.getOpcode()) {
117 case Const.AASTORE:
118 return InstructionConst.ACONST_NULL;
119 case Const.DASTORE:
120 return InstructionConst.DCONST_1;
121 case Const.FASTORE:
122 return InstructionConst.FCONST_1;
123 case Const.LASTORE:
124 return InstructionConst.LCONST_1;
125 default:
126 return InstructionConst.ICONST_1;
127 }
128 }
129 }