View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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       * Check that we can save and load the attribute correctly.
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          // Write it out
48          final File tfile = createTestdataFile("AttributeTestClassEM02$1.class");
49          clazz.dump(tfile);
50          // Read in the new version and check it is OK
51          final SyntheticRepository repos2 = createRepos(".");
52          final JavaClass clazz2 = repos2.loadClass(name);
53          assertNotNull(clazz2); // Use the variable to avoid a warning
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       * Verify for an inner class declared at the type level that the EnclosingMethod attribute is set correctly (for example to a
63       * null value)
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       * Verify for an inner class declared inside the 'main' method that the enclosing method attribute is set correctly.
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  }