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