1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.assertNotNull;
24
25 import java.io.File;
26 import java.io.IOException;
27
28 import org.apache.bcel.classfile.Attribute;
29 import org.apache.bcel.classfile.ConstantPool;
30 import org.apache.bcel.classfile.EnclosingMethod;
31 import org.apache.bcel.classfile.JavaClass;
32 import org.apache.bcel.util.SyntheticRepository;
33 import org.junit.jupiter.api.Test;
34
35 class EnclosingMethodAttributeTest extends AbstractTest {
36
37
38
39
40 @Test
41 void testAttributeSerializtion() throws ClassNotFoundException, IOException {
42 final String name = PACKAGE_BASE_NAME + ".data.AttributeTestClassEM02$1";
43 final JavaClass clazz = getTestJavaClass(name);
44 final ConstantPool pool = clazz.getConstantPool();
45 final Attribute[] encMethodAttrs = findAttribute("EnclosingMethod", clazz);
46 assertEquals(1, encMethodAttrs.length, "Wrong number of EnclosingMethod attributes");
47
48 final File tfile = createTestdataFile("AttributeTestClassEM02$1.class");
49 clazz.dump(tfile);
50
51 final SyntheticRepository repos2 = createRepos(".");
52 final JavaClass clazz2 = repos2.loadClass(name);
53 assertNotNull(clazz2);
54 final EnclosingMethod em = (EnclosingMethod) encMethodAttrs[0];
55 final String enclosingClassName = em.getEnclosingClass().getBytes(pool);
56 assertEquals(em.getEnclosingMethodIndex(), 0, "The class is not within a method, so method_index should be null");
57 assertEquals(PACKAGE_BASE_SIG + "/data/AttributeTestClassEM02", enclosingClassName, "Wrong class name");
58 tfile.deleteOnExit();
59 }
60
61
62
63
64
65 @Test
66 void testCheckClassLevelNamedInnerClass() throws ClassNotFoundException {
67 final JavaClass clazz = getTestJavaClass(PACKAGE_BASE_NAME + ".data.AttributeTestClassEM02$1");
68 final ConstantPool pool = clazz.getConstantPool();
69 final Attribute[] encMethodAttrs = findAttribute("EnclosingMethod", clazz);
70 assertEquals(1, encMethodAttrs.length, "Expected 1 EnclosingMethod attribute but found " + encMethodAttrs.length);
71 final EnclosingMethod em = (EnclosingMethod) encMethodAttrs[0];
72 final String enclosingClassName = em.getEnclosingClass().getBytes(pool);
73 assertEquals(em.getEnclosingMethodIndex(), 0, "The class is not within a method, so method_index should be null");
74 assertEquals(PACKAGE_BASE_SIG + "/data/AttributeTestClassEM02", enclosingClassName, "Wrong class name");
75 }
76
77
78
79
80 @Test
81 void testCheckMethodLevelNamedInnerClass() throws ClassNotFoundException {
82 final JavaClass clazz = getTestJavaClass(PACKAGE_BASE_NAME + ".data.AttributeTestClassEM01$1S");
83 final ConstantPool pool = clazz.getConstantPool();
84 final Attribute[] encMethodAttrs = findAttribute("EnclosingMethod", clazz);
85 assertEquals(1, encMethodAttrs.length, "Wrong number of EnclosingMethod attributes");
86 final EnclosingMethod em = (EnclosingMethod) encMethodAttrs[0];
87 final String enclosingClassName = em.getEnclosingClass().getBytes(pool);
88 final String enclosingMethodName = em.getEnclosingMethod().getName(pool);
89 assertEquals(PACKAGE_BASE_SIG + "/data/AttributeTestClassEM01", enclosingClassName, "Wrong class name");
90 assertEquals(enclosingMethodName, "main", "Wrong method name");
91 }
92 }