Home | History | Annotate | Download | only in tests
      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 
     19 package org.apache.bcel.verifier.tests;
     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.PUSH;
     33 import org.apache.bcel.generic.Type;
     34 import org.junit.Assert;
     35 
     36 public class TestArrayAccess04Creator extends TestCreator {
     37   private final InstructionFactory _factory;
     38   private final ConstantPoolGen    _cp;
     39   private final ClassGen           _cg;
     40 
     41   public TestArrayAccess04Creator() {
     42     _cg = new ClassGen(TEST_PACKAGE+".TestArrayAccess04", "java.lang.Object", "TestArrayAccess04.java",
     43             Const.ACC_PUBLIC | Const.ACC_SUPER, new String[] {  });
     44 
     45     _cp = _cg.getConstantPool();
     46     _factory = new InstructionFactory(_cg, _cp);
     47   }
     48 
     49   @Override
     50 public void create(final OutputStream out) throws IOException {
     51     createMethod_0();
     52     createMethod_1();
     53     _cg.getJavaClass().dump(out);
     54   }
     55 
     56   private void createMethod_0() {
     57     final InstructionList il = new InstructionList();
     58     final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] {  }, "<init>",
     59             TEST_PACKAGE+".TestArrayAccess04", il, _cp);
     60 
     61     final InstructionHandle ih_0 = il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
     62     Assert.assertNotNull(ih_0); // TODO why is this not used
     63     il.append(_factory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL));
     64     final InstructionHandle ih_4 = il.append(InstructionFactory.createReturn(Type.VOID));
     65     Assert.assertNotNull(ih_4); // TODO why is this not used
     66     method.setMaxStack();
     67     method.setMaxLocals();
     68     _cg.addMethod(method.getMethod());
     69     il.dispose();
     70   }
     71 
     72   private void createMethod_1() {
     73     final InstructionList il = new InstructionList();
     74     final MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.VOID, new Type[] { Type.OBJECT },
     75             new String[] { "arg0" }, "test", TEST_PACKAGE+".TestArrayAccess04", il, _cp);
     76 
     77     final InstructionHandle ih_0 = il.append(new PUSH(_cp, 1));
     78     Assert.assertNotNull(ih_0); // TODO why is this not used
     79     il.append(_factory.createNewArray(Type.OBJECT, (short) 1));
     80     il.append(InstructionFactory.createStore(Type.OBJECT, 1));
     81     final InstructionHandle ih_5 = il.append(new PUSH(_cp, 1));
     82     Assert.assertNotNull(ih_5); // TODO why is this not used
     83     il.append(InstructionFactory.createStore(Type.INT, 2));
     84     final InstructionHandle ih_7 = il.append(InstructionFactory.createLoad(Type.OBJECT, 1));
     85     Assert.assertNotNull(ih_7); // TODO why is this not used
     86     il.append(new PUSH(_cp, 0));
     87     il.append(InstructionFactory.createLoad(Type.INT, 2));
     88     il.append(InstructionConst.AASTORE);
     89     final InstructionHandle ih_11 = il.append(InstructionFactory.createReturn(Type.VOID));
     90     Assert.assertNotNull(ih_11); // TODO why is this not used
     91     method.setMaxStack();
     92     method.setMaxLocals();
     93     _cg.addMethod(method.getMethod());
     94     il.dispose();
     95   }
     96 }
     97