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  
20  package org.apache.bcel;
21  
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertFalse;
24  import static org.junit.jupiter.api.Assertions.assertNotNull;
25  
26  import java.util.stream.Stream;
27  
28  import org.apache.bcel.classfile.Code;
29  import org.apache.bcel.classfile.JavaClass;
30  import org.apache.bcel.classfile.LineNumber;
31  import org.apache.bcel.classfile.LineNumberTable;
32  import org.apache.bcel.classfile.LocalVariable;
33  import org.apache.bcel.classfile.LocalVariableTable;
34  import org.apache.bcel.classfile.Method;
35  import org.apache.bcel.classfile.Utility;
36  import org.apache.bcel.generic.ClassGen;
37  import org.apache.bcel.generic.ConstantPoolGen;
38  import org.apache.bcel.generic.InstructionHandle;
39  import org.apache.bcel.generic.InstructionList;
40  import org.apache.bcel.generic.InvokeInstruction;
41  import org.apache.bcel.generic.MethodGen;
42  import org.apache.bcel.generic.Type;
43  import org.junit.jupiter.api.Test;
44  import org.junit.jupiter.params.ParameterizedTest;
45  import org.junit.jupiter.params.provider.ValueSource;
46  
47  class PLSETest extends AbstractTest {
48      /**
49       * BCEL-208: A couple of methods in MethodGen.java need to test for an empty instruction list.
50       */
51      @Test
52      void testBCEL208() throws ClassNotFoundException {
53          final JavaClass clazz = getTestJavaClass(PACKAGE_BASE_NAME + ".data.PLSETestClass");
54          final ClassGen gen = new ClassGen(clazz);
55          final ConstantPoolGen pool = gen.getConstantPool();
56          final Method m = gen.getMethodAt(1);
57          final MethodGen mg = new MethodGen(m, gen.getClassName(), pool);
58          mg.setInstructionList(null);
59          mg.addLocalVariable("local2", Type.INT, null, null);
60          // currently, this will cause null pointer exception
61          mg.getLocalVariableTable(pool);
62      }
63  
64      /**
65       * BCEL-262:
66       */
67      @Test
68      void testBCEL262() throws ClassNotFoundException {
69          final JavaClass clazz = getTestJavaClass(PACKAGE_BASE_NAME + ".data.PLSETestEnum");
70          final ClassGen gen = new ClassGen(clazz);
71          final ConstantPoolGen pool = gen.getConstantPool();
72          // get the values() method
73          final Method m = gen.getMethodAt(0);
74          final MethodGen mg = new MethodGen(m, gen.getClassName(), pool);
75          final InstructionList il = mg.getInstructionList();
76          // get the invokevirtual instruction
77          final InstructionHandle ih = il.findHandle(3);
78          final InvokeInstruction ii = (InvokeInstruction) ih.getInstruction();
79          // without fix, the getClassName() will throw:
80          // java.lang.IllegalArgumentException: Cannot be used on an array type
81          final String cn = ii.getClassName(pool);
82          assertEquals("[Lorg.apache.bcel.data.PLSETestEnum;", cn);
83      }
84  
85      /**
86       * BCEL-295:
87       */
88      @Test
89      void testBCEL295() throws Exception {
90          final JavaClass clazz = getTestJavaClass(PACKAGE_BASE_NAME + ".data.PLSETestClass2");
91          final ClassGen cg = new ClassGen(clazz);
92          final ConstantPoolGen pool = cg.getConstantPool();
93          final Method m = cg.getMethodAt(1); // 'main'
94          final LocalVariableTable lvt = m.getLocalVariableTable();
95          final LocalVariable lv = lvt.getLocalVariable(2, 4); // 'i'
96          // System.out.println(lv);
97          final MethodGen mg = new MethodGen(m, cg.getClassName(), pool);
98          final LocalVariableTable newLvt = mg.getLocalVariableTable(mg.getConstantPool());
99          final LocalVariable newLv = newLvt.getLocalVariable(2, 4); // 'i'
100         // System.out.println(new_lv);
101         assertEquals(lv.getLength(), newLv.getLength(), "live range length");
102     }
103 
104     /**
105      * BCEL-361: LineNumber.toString() treats code offset as signed
106      */
107     @Test
108     void testBCEL361() throws Exception {
109         final JavaClass clazz = getTestJavaClass(PACKAGE_BASE_NAME + ".data.LargeMethod");
110         final Method[] methods = clazz.getMethods();
111         final Method m = methods[0];
112         // System.out.println(m.getName());
113         final Code code = m.getCode();
114         final LineNumberTable lnt = code.getLineNumberTable();
115         final LineNumber[] lineNumbers = lnt.getLineNumberTable();
116         final String data = lineNumbers[lineNumbers.length - 1].toString();
117         // System.out.println(data);
118         // System.out.println(data.contains("-"));
119         assertFalse(data.contains("-"), "code offsets must be positive");
120         Stream.of(lineNumbers).forEach(ln -> assertFalse(ln.getLineNumber() < 0));
121         Stream.of(lineNumbers).forEach(ln -> assertFalse(ln.getStartPC() < 0));
122     }
123 
124     /**
125      * BCEL-79:
126      */
127     @Test
128     void testBCEL79() throws ClassNotFoundException {
129         final JavaClass clazz = getTestJavaClass(PACKAGE_BASE_NAME + ".data.PLSETestClass");
130         final ClassGen gen = new ClassGen(clazz);
131         final ConstantPoolGen pool = gen.getConstantPool();
132         final Method m = gen.getMethodAt(2);
133         final LocalVariableTable lvt = m.getLocalVariableTable();
134         // System.out.println(lvt);
135         // System.out.println(lvt.getTableLength());
136         final MethodGen mg = new MethodGen(m, gen.getClassName(), pool);
137         final LocalVariableTable newLvt = mg.getLocalVariableTable(mg.getConstantPool());
138         // System.out.println(new_lvt);
139         assertEquals(lvt.getTableLength(), newLvt.getTableLength(), "number of locals");
140     }
141 
142     /**
143      * Test to improve BCEL tests code coverage for classfile/Utility.java.
144      */
145     @ParameterizedTest
146     @ValueSource(booleans = { true, false })
147     void testCoverage(final boolean compress) throws ClassNotFoundException, java.io.IOException {
148         // load a class with a wide variety of byte codes - including tableswitch and lookupswitch
149         final JavaClass clazz = getTestJavaClass(PACKAGE_BASE_NAME + ".data.ConstantPoolX");
150         for (final Method m : clazz.getMethods()) {
151             final String signature = m.getSignature();
152             Utility.methodTypeToSignature(Utility.methodSignatureReturnType(signature), Utility.methodSignatureArgumentTypes(signature)); // discard result
153             final Code code = m.getCode();
154             if (code != null) {
155                 // TODO: need for real assertions here
156                 final String encoded = Utility.encode(code.getCode(), compress);
157                 assertNotNull(encoded);
158                 // following statement will throw exeception without classfile/Utility.encode fix
159                 assertNotNull(Utility.decode(encoded, compress));
160                 assertNotNull(code.toString());
161             }
162         }
163     }
164 }