View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.bcel.verifier.tests;
18  
19  import static org.junit.jupiter.api.Assertions.assertNotNull;
20  
21  import java.io.IOException;
22  import java.io.OutputStream;
23  
24  import org.apache.bcel.Const;
25  import org.apache.bcel.generic.ClassGen;
26  import org.apache.bcel.generic.ConstantPoolGen;
27  import org.apache.bcel.generic.InstructionConst;
28  import org.apache.bcel.generic.InstructionFactory;
29  import org.apache.bcel.generic.InstructionHandle;
30  import org.apache.bcel.generic.InstructionList;
31  import org.apache.bcel.generic.MethodGen;
32  import org.apache.bcel.generic.Type;
33  
34  public class TestReturn01Creator extends TestCreator {
35      private final InstructionFactory factory;
36      private final ConstantPoolGen cp;
37      private final ClassGen cg;
38  
39      public TestReturn01Creator() {
40          cg = new ClassGen(TEST_PACKAGE + ".TestReturn01", "java.lang.Object", "TestReturn01.java", Const.ACC_PUBLIC | Const.ACC_SUPER, new String[] {});
41  
42          cp = cg.getConstantPool();
43          factory = new InstructionFactory(cg, cp);
44      }
45  
46      @Override
47      public void create(final OutputStream out) throws IOException {
48          createMethod_0();
49          createMethod_1();
50          cg.getJavaClass().dump(out);
51      }
52  
53      private void createMethod_0() {
54          final InstructionList il = new InstructionList();
55          final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] {}, "<init>", TEST_PACKAGE + ".TestReturn01", il, cp);
56  
57          final InstructionHandle ih_0 = il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
58          assertNotNull(ih_0); // TODO why is this not used
59          il.append(factory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL));
60          final InstructionHandle ih_4 = il.append(InstructionFactory.createReturn(Type.VOID));
61          assertNotNull(ih_4); // TODO why is this not used
62          method.setMaxStack();
63          method.setMaxLocals();
64          cg.addMethod(method.getMethod());
65          il.dispose();
66      }
67  
68      private void createMethod_1() {
69          final InstructionList il = new InstructionList();
70          final MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.VOID, Type.NO_ARGS, new String[] {}, "foo",
71              TEST_PACKAGE + ".TestReturn01", il, cp);
72  
73          final InstructionHandle ih_0 = il.append(factory.createNew("java.lang.Object"));
74          assertNotNull(ih_0); // TODO why is this not used
75          il.append(InstructionConst.DUP);
76          il.append(factory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL));
77          il.append(InstructionConst.NOP);
78          final InstructionHandle ih_8 = il.append(InstructionFactory.createReturn(Type.OBJECT));
79          assertNotNull(ih_8); // TODO why is this not used
80          method.setMaxStack();
81          method.setMaxLocals();
82          cg.addMethod(method.getMethod());
83          il.dispose();
84      }
85  }