View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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.ObjectType;
35  import org.apache.bcel.generic.PUSH;
36  import org.apache.bcel.generic.Type;
37  
38  public class TestArrayAccess02Creator extends TestCreator {
39      private final InstructionFactory instructionFactory;
40      private final ConstantPoolGen constantPoolGen;
41      private final ClassGen classGen;
42  
43      public TestArrayAccess02Creator() {
44          classGen = new ClassGen(TEST_PACKAGE + ".TestArrayAccess02", "java.lang.Object", "TestArrayAccess02.java", Const.ACC_PUBLIC | Const.ACC_SUPER,
45              new String[] {});
46  
47          constantPoolGen = classGen.getConstantPool();
48          instructionFactory = new InstructionFactory(classGen, constantPoolGen);
49      }
50  
51      @Override
52      public void create(final OutputStream out) throws IOException {
53          createMethod_0();
54          createMethod_1();
55          classGen.getJavaClass().dump(out);
56      }
57  
58      private void createMethod_0() {
59          final InstructionList il = new InstructionList();
60          final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] {}, "<init>", TEST_PACKAGE + ".TestArrayAccess02", il,
61              constantPoolGen);
62  
63          final InstructionHandle ih_0 = il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
64          assertNotNull(ih_0); // TODO why is this not used
65          il.append(instructionFactory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL));
66          final InstructionHandle ih_4 = il.append(InstructionFactory.createReturn(Type.VOID));
67          assertNotNull(ih_4); // TODO why is this not used
68          method.setMaxStack();
69          method.setMaxLocals();
70          classGen.addMethod(method.getMethod());
71          il.dispose();
72      }
73  
74      private void createMethod_1() {
75          final InstructionList il = new InstructionList();
76          final MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.VOID, Type.NO_ARGS, new String[] {}, "test",
77              TEST_PACKAGE + ".TestArrayAccess02", il, constantPoolGen);
78  
79          final InstructionHandle ih_0 = il.append(new PUSH(constantPoolGen, 1));
80          assertNotNull(ih_0); // TODO why is this not used
81          il.append(instructionFactory.createNewArray(new ObjectType(TEST_PACKAGE + ".TestArrayAccess02"), (short) 1));
82          il.append(InstructionFactory.createStore(Type.OBJECT, 0));
83          final InstructionHandle ih_5 = il.append(new PUSH(constantPoolGen, 1));
84          assertNotNull(ih_5); // TODO why is this not used
85          il.append(instructionFactory.createNewArray(Type.STRING, (short) 1));
86          il.append(InstructionFactory.createStore(Type.OBJECT, 1));
87          final InstructionHandle ih_10 = il.append(InstructionFactory.createLoad(Type.OBJECT, 1));
88          assertNotNull(ih_10); // TODO why is this not used
89          il.append(new PUSH(constantPoolGen, 0));
90          il.append(instructionFactory.createNew(TEST_PACKAGE + ".TestArrayAccess02"));
91          il.append(InstructionConst.DUP);
92          il.append(instructionFactory.createInvoke(TEST_PACKAGE + ".TestArrayAccess02", "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL));
93          il.append(InstructionConst.AASTORE);
94          final InstructionHandle ih_20 = il.append(InstructionFactory.createReturn(Type.VOID));
95          assertNotNull(ih_20); // TODO why is this not used
96          method.setMaxStack();
97          method.setMaxLocals();
98          classGen.addMethod(method.getMethod());
99          il.dispose();
100     }
101 }