View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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); // TODO why is this not used
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); // TODO why is this not used
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); // TODO why is this not used
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); // TODO why is this not used
87          il.append(InstructionFactory.createStore(primitiveType, 2));
88          final InstructionHandle ih_7 = il.append(InstructionFactory.createLoad(Type.OBJECT, 1));
89          assertNotNull(ih_7); // TODO why is this not used
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); // TODO why is this not used
95          method.setMaxStack();
96          method.setMaxLocals();
97          cg.addMethod(method.getMethod());
98          il.dispose();
99      }
100 }