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  
20  package org.apache.bcel.verifier.tests;
21  
22  import static org.junit.jupiter.api.Assertions.assertNotNull;
23  
24  import java.io.IOException;
25  import java.io.OutputStream;
26  
27  import org.apache.bcel.Const;
28  import org.apache.bcel.generic.ClassGen;
29  import org.apache.bcel.generic.ConstantPoolGen;
30  import org.apache.bcel.generic.InstructionConst;
31  import org.apache.bcel.generic.InstructionFactory;
32  import org.apache.bcel.generic.InstructionHandle;
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  public abstract class TestArrayAccess04Creator extends TestCreator {
39      private final InstructionFactory factory;
40      private final ConstantPoolGen cp;
41      private final ClassGen cg;
42      private final Type primitiveType;
43      private final String genClassSuffix;
44  
45      protected TestArrayAccess04Creator(final Type primitiveType, final String genClassSuffix) {
46          cg = new ClassGen(TEST_PACKAGE + ".TestArrayAccess04" + genClassSuffix, "java.lang.Object", "TestArrayAccess04.java", Const.ACC_PUBLIC | Const.ACC_SUPER,
47              new String[] {});
48  
49          cp = cg.getConstantPool();
50          factory = new InstructionFactory(cg, cp);
51          this.primitiveType = primitiveType;
52          this.genClassSuffix = genClassSuffix;
53      }
54  
55      @Override
56      public void create(final OutputStream out) throws IOException {
57          createMethod_0();
58          createMethod_1();
59          cg.getJavaClass().dump(out);
60      }
61  
62      private void createMethod_0() {
63          final InstructionList il = new InstructionList();
64          final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] {}, "<init>", TEST_PACKAGE + ".TestArrayAccess04", il,
65              cp);
66  
67          final InstructionHandle ih_0 = il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
68          assertNotNull(ih_0); // TODO why is this not used
69          il.append(factory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL));
70          final InstructionHandle ih_4 = il.append(InstructionFactory.createReturn(Type.VOID));
71          assertNotNull(ih_4); // TODO why is this not used
72          method.setMaxStack();
73          method.setMaxLocals();
74          cg.addMethod(method.getMethod());
75          il.dispose();
76      }
77  
78      private void createMethod_1() {
79          final InstructionList il = new InstructionList();
80          final MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.VOID, new Type[] {Type.OBJECT}, new String[] {"arg0"}, "test",
81              TEST_PACKAGE + ".TestArrayAccess04" + genClassSuffix, il, cp);
82  
83          final InstructionHandle ih_0 = il.append(new PUSH(cp, 1));
84          assertNotNull(ih_0); // TODO why is this not used
85          il.append(factory.createNewArray(Type.OBJECT, (short) 1));
86          il.append(InstructionFactory.createStore(Type.OBJECT, 1));
87          final InstructionHandle ih_5 = il.append(new PUSH(cp, 1));
88          assertNotNull(ih_5); // TODO why is this not used
89          il.append(InstructionFactory.createStore(primitiveType, 2));
90          final InstructionHandle ih_7 = il.append(InstructionFactory.createLoad(Type.OBJECT, 1));
91          assertNotNull(ih_7); // TODO why is this not used
92          il.append(new PUSH(cp, 0));
93          il.append(InstructionFactory.createLoad(primitiveType, 2));
94          il.append(InstructionConst.AASTORE);
95          final InstructionHandle ih_11 = il.append(InstructionFactory.createReturn(Type.VOID));
96          assertNotNull(ih_11); // TODO why is this not used
97          method.setMaxStack();
98          method.setMaxLocals();
99          cg.addMethod(method.getMethod());
100         il.dispose();
101     }
102 }