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 TestArrayAccess03Creator extends TestCreator {
39      private final InstructionFactory factory;
40      private final ConstantPoolGen cp;
41      private final ClassGen cg;
42  
43      public TestArrayAccess03Creator() {
44          cg = new ClassGen(TEST_PACKAGE + ".TestArrayAccess03", "java.lang.Object", "TestArrayAccess03.java", Const.ACC_PUBLIC | Const.ACC_SUPER,
45              new String[] {});
46  
47          cp = cg.getConstantPool();
48          factory = new InstructionFactory(cg, cp);
49      }
50  
51      @Override
52      public void create(final OutputStream out) throws IOException {
53          createMethod_0();
54          createMethod_1();
55          cg.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 + ".TestArrayAccess03", il,
61              cp);
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(factory.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          cg.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, new Type[] {Type.OBJECT}, new String[] {"arg0"}, "test",
77              TEST_PACKAGE + ".TestArrayAccess03", il, cp);
78  
79          final InstructionHandle ih_0 = il.append(new PUSH(cp, 1));
80          assertNotNull(ih_0); // TODO why is this not used
81          il.append(factory.createNewArray(new ObjectType(TEST_PACKAGE + ".TestArrayAccess03"), (short) 1));
82          il.append(InstructionFactory.createStore(Type.OBJECT, 1));
83          final InstructionHandle ih_5 = il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
84          assertNotNull(ih_5); // TODO why is this not used
85          il.append(new PUSH(cp, 0));
86          il.append(factory.createNew(TEST_PACKAGE + ".TestArrayAccess03"));
87          il.append(InstructionConst.DUP);
88          il.append(factory.createInvoke(TEST_PACKAGE + ".TestArrayAccess03", "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL));
89          il.append(InstructionConst.AASTORE);
90          final InstructionHandle ih_15 = il.append(InstructionFactory.createReturn(Type.VOID));
91          assertNotNull(ih_15); // TODO why is this not used
92          method.setMaxStack();
93          method.setMaxLocals();
94          cg.addMethod(method.getMethod());
95          il.dispose();
96      }
97  }