1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.bcel.util;
21
22 import static org.junit.jupiter.api.Assertions.assertFalse;
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24
25 import java.io.File;
26 import java.nio.charset.StandardCharsets;
27 import java.nio.file.Files;
28
29 import org.apache.bcel.Const;
30 import org.apache.bcel.classfile.JavaClass;
31 import org.apache.bcel.generic.ClassGen;
32 import org.apache.bcel.generic.ConstantPoolGen;
33 import org.apache.bcel.generic.ILOAD;
34 import org.apache.bcel.generic.InstructionFactory;
35 import org.apache.bcel.generic.InstructionList;
36 import org.apache.bcel.generic.MethodGen;
37 import org.apache.bcel.generic.Type;
38 import org.junit.jupiter.api.Test;
39
40 class CodeHTMLNarrowingTest {
41
42
43
44
45
46
47 @Test
48 void testWideLocalVariableIndexIsUnsigned() throws Exception {
49 final int index = 40000;
50 final ClassGen cg = new ClassGen("Wide", "java.lang.Object", "Wide.java", Const.ACC_PUBLIC, null);
51 final ConstantPoolGen cp = cg.getConstantPool();
52 final InstructionList il = new InstructionList();
53 il.append(new ILOAD(index));
54 il.append(InstructionFactory.createReturn(Type.INT));
55 final MethodGen mg = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.INT, Type.NO_ARGS, null, "m", "Wide", il, cp);
56 mg.setMaxStack(1);
57 mg.setMaxLocals(index + 1);
58 cg.addMethod(mg.getMethod());
59 final JavaClass jc = cg.getJavaClass();
60
61 final File outputDir = new File("target/test-output/html-narrowing");
62 if (!outputDir.mkdirs()) {
63 assertTrue(outputDir.isDirectory());
64 }
65 new Class2HTML(jc, outputDir.getAbsolutePath() + File.separator);
66
67 final String code = new String(Files.readAllBytes(new File(outputDir, "Wide_code.html").toPath()), StandardCharsets.UTF_8);
68 assertTrue(code.contains("%" + index), "expected the wide index rendered as " + index);
69 assertFalse(code.contains("%" + (short) index), "wide index was sign-extended to " + (short) index);
70 }
71 }