• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
20 
21 import org.apache.bcel.classfile.JavaClass;
22 import org.apache.bcel.classfile.LocalVariable;
23 import org.apache.bcel.classfile.LocalVariableTable;
24 import org.apache.bcel.classfile.Method;
25 import org.apache.bcel.generic.ClassGen;
26 import org.apache.bcel.generic.ConstantPoolGen;
27 import org.apache.bcel.generic.InstructionHandle;
28 import org.apache.bcel.generic.InstructionList;
29 import org.apache.bcel.generic.InvokeInstruction;
30 import org.apache.bcel.generic.MethodGen;
31 import org.apache.bcel.generic.Type;
32 
33 public class PLSETestCase extends AbstractTestCase
34 {
35     /**
36      * BCEL-208: A couple of methods in MethodGen.java need to test for
37      * an empty instruction list.
38      */
testB208()39     public void testB208() throws ClassNotFoundException
40     {
41         final JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.PLSETestClass");
42         final ClassGen gen = new ClassGen(clazz);
43         final ConstantPoolGen pool = gen.getConstantPool();
44         final Method m = gen.getMethodAt(1);
45         final MethodGen mg = new MethodGen(m, gen.getClassName(), pool);
46         mg.setInstructionList(null);
47         mg.addLocalVariable("local2", Type.INT, null, null);
48         // currently, this will cause null pointer exception
49         mg.getLocalVariableTable(pool);
50     }
51 
52     /**
53      * BCEL-79:
54      */
testB79()55     public void testB79() throws ClassNotFoundException
56     {
57         final JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.PLSETestClass");
58         final ClassGen gen = new ClassGen(clazz);
59         final ConstantPoolGen pool = gen.getConstantPool();
60         final Method m = gen.getMethodAt(2);
61         final LocalVariableTable lvt = m.getLocalVariableTable();
62         //System.out.println(lvt);
63         //System.out.println(lvt.getTableLength());
64         final MethodGen mg = new MethodGen(m, gen.getClassName(), pool);
65         final LocalVariableTable new_lvt = mg.getLocalVariableTable(mg.getConstantPool());
66         //System.out.println(new_lvt);
67         assertEquals("number of locals", lvt.getTableLength(), new_lvt.getTableLength());
68     }
69 
70     /**
71      * BCEL-262:
72      */
testB262()73     public void testB262() throws ClassNotFoundException
74     {
75         final JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.PLSETestEnum");
76         final ClassGen gen = new ClassGen(clazz);
77         final ConstantPoolGen pool = gen.getConstantPool();
78         // get the values() method
79         final Method m = gen.getMethodAt(0);
80         final MethodGen mg = new MethodGen(m, gen.getClassName(), pool);
81         final InstructionList il = mg.getInstructionList();
82         // get the invokevirtual instruction
83         final InstructionHandle ih = il.findHandle(3);
84         final InvokeInstruction ii = (InvokeInstruction)(ih.getInstruction());
85         // without fix, the getClassName() will throw:
86         //   java.lang.IllegalArgumentException: Cannot be used on an array type
87         final String cn = ii.getClassName(pool);
88         assertEquals("[Lorg.apache.bcel.data.PLSETestEnum;", cn);
89     }
90 
91     /**
92      * BCEL-295:
93      */
testB295()94     public void testB295() throws Exception
95     {
96         final JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.PLSETestClass2");
97         final ClassGen cg = new ClassGen(clazz);
98         final ConstantPoolGen pool = cg.getConstantPool();
99         final Method m = cg.getMethodAt(1);  // 'main'
100         final LocalVariableTable lvt = m.getLocalVariableTable();
101         final LocalVariable lv = lvt.getLocalVariable(2, 4);  // 'i'
102         //System.out.println(lv);
103         final MethodGen mg = new MethodGen(m, cg.getClassName(), pool);
104         final LocalVariableTable new_lvt = mg.getLocalVariableTable(mg.getConstantPool());
105         final LocalVariable new_lv = new_lvt.getLocalVariable(2, 4);  // 'i'
106         //System.out.println(new_lv);
107         assertEquals("live range length", lv.getLength(), new_lv.getLength());
108     }
109 }
110