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.Type;
35  
36  public abstract class TestReturn03Creator extends TestCreator {
37      private final InstructionFactory instructionFactory;
38      private final ConstantPoolGen constantPoolGen;
39      private final ClassGen classGen;
40      private final Type returnType;
41      private final String genClassSuffix;
42  
43      protected TestReturn03Creator(final Type returnType, final String genClassSuffix) {
44          classGen = new ClassGen(TEST_PACKAGE + ".TestReturn03" + genClassSuffix, "java.lang.Object", "TestReturn03.java", Const.ACC_PUBLIC | Const.ACC_SUPER,
45                  new String[] {});
46  
47          constantPoolGen = classGen.getConstantPool();
48          instructionFactory = new InstructionFactory(classGen, constantPoolGen);
49          this.returnType = returnType;
50          this.genClassSuffix = genClassSuffix;
51      }
52  
53      @Override
54      public void create(final OutputStream out) throws IOException {
55          createMethod_0();
56          createMethod_1();
57          classGen.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 + ".TestReturn03", il,
63                  constantPoolGen);
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(instructionFactory.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          classGen.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.INT, Type.NO_ARGS, new String[] {}, "test3",
79                  TEST_PACKAGE + ".TestReturn03" + genClassSuffix, il, constantPoolGen);
80  
81          final InstructionHandle ih_0 = il.append(InstructionConst.ACONST_NULL);
82          assertNotNull(ih_0); // TODO why is this not used
83          il.append(InstructionFactory.createReturn(returnType));
84          method.setMaxStack();
85          method.setMaxLocals();
86          classGen.addMethod(method.getMethod());
87          il.dispose();
88      }
89  }