1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.bcel;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertFalse;
22 import static org.junit.jupiter.api.Assertions.assertNotNull;
23
24 import java.util.stream.Stream;
25
26 import org.apache.bcel.classfile.Code;
27 import org.apache.bcel.classfile.JavaClass;
28 import org.apache.bcel.classfile.LineNumber;
29 import org.apache.bcel.classfile.LineNumberTable;
30 import org.apache.bcel.classfile.LocalVariable;
31 import org.apache.bcel.classfile.LocalVariableTable;
32 import org.apache.bcel.classfile.Method;
33 import org.apache.bcel.classfile.Utility;
34 import org.apache.bcel.generic.ClassGen;
35 import org.apache.bcel.generic.ConstantPoolGen;
36 import org.apache.bcel.generic.InstructionHandle;
37 import org.apache.bcel.generic.InstructionList;
38 import org.apache.bcel.generic.InvokeInstruction;
39 import org.apache.bcel.generic.MethodGen;
40 import org.apache.bcel.generic.Type;
41 import org.junit.jupiter.api.Test;
42 import org.junit.jupiter.params.ParameterizedTest;
43 import org.junit.jupiter.params.provider.ValueSource;
44
45 public class PLSETestCase extends AbstractTestCase {
46
47
48
49 @Test
50 public void testBCEL208() throws ClassNotFoundException {
51 final JavaClass clazz = getTestJavaClass(PACKAGE_BASE_NAME + ".data.PLSETestClass");
52 final ClassGen gen = new ClassGen(clazz);
53 final ConstantPoolGen pool = gen.getConstantPool();
54 final Method m = gen.getMethodAt(1);
55 final MethodGen mg = new MethodGen(m, gen.getClassName(), pool);
56 mg.setInstructionList(null);
57 mg.addLocalVariable("local2", Type.INT, null, null);
58
59 mg.getLocalVariableTable(pool);
60 }
61
62
63
64
65 @Test
66 public void testBCEL262() throws ClassNotFoundException {
67 final JavaClass clazz = getTestJavaClass(PACKAGE_BASE_NAME + ".data.PLSETestEnum");
68 final ClassGen gen = new ClassGen(clazz);
69 final ConstantPoolGen pool = gen.getConstantPool();
70
71 final Method m = gen.getMethodAt(0);
72 final MethodGen mg = new MethodGen(m, gen.getClassName(), pool);
73 final InstructionList il = mg.getInstructionList();
74
75 final InstructionHandle ih = il.findHandle(3);
76 final InvokeInstruction ii = (InvokeInstruction) ih.getInstruction();
77
78
79 final String cn = ii.getClassName(pool);
80 assertEquals("[Lorg.apache.bcel.data.PLSETestEnum;", cn);
81 }
82
83
84
85
86 @Test
87 public void testBCEL295() throws Exception {
88 final JavaClass clazz = getTestJavaClass(PACKAGE_BASE_NAME + ".data.PLSETestClass2");
89 final ClassGen cg = new ClassGen(clazz);
90 final ConstantPoolGen pool = cg.getConstantPool();
91 final Method m = cg.getMethodAt(1);
92 final LocalVariableTable lvt = m.getLocalVariableTable();
93 final LocalVariable lv = lvt.getLocalVariable(2, 4);
94
95 final MethodGen mg = new MethodGen(m, cg.getClassName(), pool);
96 final LocalVariableTable newLvt = mg.getLocalVariableTable(mg.getConstantPool());
97 final LocalVariable newLv = newLvt.getLocalVariable(2, 4);
98
99 assertEquals(lv.getLength(), newLv.getLength(), "live range length");
100 }
101
102
103
104
105 @Test
106 public void testBCEL361() throws Exception {
107 final JavaClass clazz = getTestJavaClass(PACKAGE_BASE_NAME + ".data.LargeMethod");
108 final Method[] methods = clazz.getMethods();
109 final Method m = methods[0];
110
111 final Code code = m.getCode();
112 final LineNumberTable lnt = code.getLineNumberTable();
113 final LineNumber[] lineNumbers = lnt.getLineNumberTable();
114 final String data = lineNumbers[lineNumbers.length - 1].toString();
115
116
117 assertFalse(data.contains("-"), "code offsets must be positive");
118 Stream.of(lineNumbers).forEach(ln -> assertFalse(ln.getLineNumber() < 0));
119 Stream.of(lineNumbers).forEach(ln -> assertFalse(ln.getStartPC() < 0));
120 }
121
122
123
124
125 @Test
126 public void testBCEL79() throws ClassNotFoundException {
127 final JavaClass clazz = getTestJavaClass(PACKAGE_BASE_NAME + ".data.PLSETestClass");
128 final ClassGen gen = new ClassGen(clazz);
129 final ConstantPoolGen pool = gen.getConstantPool();
130 final Method m = gen.getMethodAt(2);
131 final LocalVariableTable lvt = m.getLocalVariableTable();
132
133
134 final MethodGen mg = new MethodGen(m, gen.getClassName(), pool);
135 final LocalVariableTable newLvt = mg.getLocalVariableTable(mg.getConstantPool());
136
137 assertEquals(lvt.getTableLength(), newLvt.getTableLength(), "number of locals");
138 }
139
140
141
142
143 @ParameterizedTest
144 @ValueSource(booleans = { true, false })
145 public void testCoverage(final boolean compress) throws ClassNotFoundException, java.io.IOException {
146
147 final JavaClass clazz = getTestJavaClass(PACKAGE_BASE_NAME + ".data.ConstantPoolX");
148 for (final Method m : clazz.getMethods()) {
149 final String signature = m.getSignature();
150 Utility.methodTypeToSignature(Utility.methodSignatureReturnType(signature), Utility.methodSignatureArgumentTypes(signature));
151 final Code code = m.getCode();
152 if (code != null) {
153
154 final String encoded = Utility.encode(code.getCode(), compress);
155 assertNotNull(encoded);
156
157 assertNotNull(Utility.decode(encoded, compress));
158 assertNotNull(code.toString());
159 }
160 }
161 }
162 }