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 JavaClass clazz = getTestJavaClass(PACKAGE_BASE_NAME + ".data.AttributeTestClassEM02$1");
43 final ConstantPool pool = clazz.getConstantPool();
44 final Attribute[] encMethodAttrs = findAttribute("EnclosingMethod", clazz);
45 assertEquals(1, encMethodAttrs.length, "Wrong number of EnclosingMethod attributes");
46
47 final File tfile = createTestdataFile("AttributeTestClassEM02$1.class");
48 clazz.dump(tfile);
49
50 final SyntheticRepository repos2 = createRepos(".");
51 final JavaClass clazz2 = repos2.loadClass("AttributeTestClassEM02$1");
52 assertNotNull(clazz2);
53 final EnclosingMethod em = (EnclosingMethod) encMethodAttrs[0];
54 final String enclosingClassName = em.getEnclosingClass().getBytes(pool);
55 assertEquals(em.getEnclosingMethodIndex(), 0, "The class is not within a method, so method_index should be null");
56 assertEquals(PACKAGE_BASE_SIG + "/data/AttributeTestClassEM02", enclosingClassName, "Wrong class name");
57 tfile.deleteOnExit();
58 }
59
60
61
62
63
64 @Test
65 void testCheckClassLevelNamedInnerClass() throws ClassNotFoundException {
66 final JavaClass clazz = getTestJavaClass(PACKAGE_BASE_NAME + ".data.AttributeTestClassEM02$1");
67 final ConstantPool pool = clazz.getConstantPool();
68 final Attribute[] encMethodAttrs = findAttribute("EnclosingMethod", clazz);
69 assertEquals(1, encMethodAttrs.length, "Expected 1 EnclosingMethod attribute but found " + encMethodAttrs.length);
70 final EnclosingMethod em = (EnclosingMethod) encMethodAttrs[0];
71 final String enclosingClassName = em.getEnclosingClass().getBytes(pool);
72 assertEquals(em.getEnclosingMethodIndex(), 0, "The class is not within a method, so method_index should be null");
73 assertEquals(PACKAGE_BASE_SIG + "/data/AttributeTestClassEM02", enclosingClassName, "Wrong class name");
74 }
75
76
77
78
79 @Test
80 void testCheckMethodLevelNamedInnerClass() throws ClassNotFoundException {
81 final JavaClass clazz = getTestJavaClass(PACKAGE_BASE_NAME + ".data.AttributeTestClassEM01$1S");
82 final ConstantPool pool = clazz.getConstantPool();
83 final Attribute[] encMethodAttrs = findAttribute("EnclosingMethod", clazz);
84 assertEquals(1, encMethodAttrs.length, "Wrong number of EnclosingMethod attributes");
85 final EnclosingMethod em = (EnclosingMethod) encMethodAttrs[0];
86 final String enclosingClassName = em.getEnclosingClass().getBytes(pool);
87 final String enclosingMethodName = em.getEnclosingMethod().getName(pool);
88 assertEquals(PACKAGE_BASE_SIG + "/data/AttributeTestClassEM01", enclosingClassName, "Wrong class name");
89 assertEquals(enclosingMethodName, "main", "Wrong method name");
90 }
91 }