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 class TestReturn01Creator extends TestCreator {
37      private final InstructionFactory factory;
38      private final ConstantPoolGen cp;
39      private final ClassGen cg;
40  
41      public TestReturn01Creator() {
42          cg = new ClassGen(TEST_PACKAGE + ".TestReturn01", "java.lang.Object", "TestReturn01.java", Const.ACC_PUBLIC | Const.ACC_SUPER, new String[] {});
43  
44          cp = cg.getConstantPool();
45          factory = new InstructionFactory(cg, cp);
46      }
47  
48      @Override
49      public void create(final OutputStream out) throws IOException {
50          createMethod_0();
51          createMethod_1();
52          cg.getJavaClass().dump(out);
53      }
54  
55      private void createMethod_0() {
56          final InstructionList il = new InstructionList();
57          final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] {}, "<init>", TEST_PACKAGE + ".TestReturn01", il, cp);
58  
59          final InstructionHandle ih_0 = il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
60          assertNotNull(ih_0); // TODO why is this not used
61          il.append(factory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL));
62          final InstructionHandle ih_4 = il.append(InstructionFactory.createReturn(Type.VOID));
63          assertNotNull(ih_4); // TODO why is this not used
64          method.setMaxStack();
65          method.setMaxLocals();
66          cg.addMethod(method.getMethod());
67          il.dispose();
68      }
69  
70      private void createMethod_1() {
71          final InstructionList il = new InstructionList();
72          final MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.VOID, Type.NO_ARGS, new String[] {}, "foo",
73              TEST_PACKAGE + ".TestReturn01", il, cp);
74  
75          final InstructionHandle ih_0 = il.append(factory.createNew("java.lang.Object"));
76          assertNotNull(ih_0); // TODO why is this not used
77          il.append(InstructionConst.DUP);
78          il.append(factory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL));
79          il.append(InstructionConst.NOP);
80          final InstructionHandle ih_8 = il.append(InstructionFactory.createReturn(Type.OBJECT));
81          assertNotNull(ih_8); // TODO why is this not used
82          method.setMaxStack();
83          method.setMaxLocals();
84          cg.addMethod(method.getMethod());
85          il.dispose();
86      }
87  }