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   */
18  package org.apache.bcel.verifier.tests;
19  
20  import java.io.IOException;
21  import java.io.OutputStream;
22  
23  import org.apache.bcel.Const;
24  import org.apache.bcel.generic.ClassGen;
25  import org.apache.bcel.generic.ConstantPoolGen;
26  import org.apache.bcel.generic.InstructionConst;
27  import org.apache.bcel.generic.InstructionFactory;
28  import org.apache.bcel.generic.InstructionHandle;
29  import org.apache.bcel.generic.InstructionList;
30  import org.apache.bcel.generic.MethodGen;
31  import org.apache.bcel.generic.Type;
32  import org.junit.Assert;
33  
34  public class TestReturn03Creator extends TestCreator {
35    private final InstructionFactory _factory;
36    private final ConstantPoolGen    _cp;
37    private final ClassGen           _cg;
38  
39    public TestReturn03Creator() {
40      _cg = new ClassGen(TEST_PACKAGE+".TestReturn03", "java.lang.Object", "TestReturn03.java",
41              Const.ACC_PUBLIC | Const.ACC_SUPER, new String[] {  });
42  
43      _cp = _cg.getConstantPool();
44      _factory = new InstructionFactory(_cg, _cp);
45    }
46  
47    @Override
48  public void create(final OutputStream out) throws IOException {
49      createMethod_0();
50      createMethod_1();
51      _cg.getJavaClass().dump(out);
52    }
53  
54    private void createMethod_0() {
55      final InstructionList il = new InstructionList();
56      final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] {  },
57              "<init>", TEST_PACKAGE+".TestReturn03", il, _cp);
58  
59      final InstructionHandle ih_0 = il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
60      Assert.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      Assert.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.INT, Type.NO_ARGS,
73              new String[] {  }, "test3", TEST_PACKAGE+".TestReturn03", il, _cp);
74  
75      final InstructionHandle ih_0 = il.append(InstructionConst.ACONST_NULL);
76      Assert.assertNotNull(ih_0); // TODO why is this not used
77      il.append(InstructionFactory.createReturn(Type.OBJECT));
78      method.setMaxStack();
79      method.setMaxLocals();
80      _cg.addMethod(method.getMethod());
81      il.dispose();
82    }
83  }