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 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          // Write it out
47          final File tfile = createTestdataFile("AttributeTestClassEM02$1.class");
48          clazz.dump(tfile);
49          // Read in the new version and check it is OK
50          final SyntheticRepository repos2 = createRepos(".");
51          final JavaClass clazz2 = repos2.loadClass("AttributeTestClassEM02$1");
52          assertNotNull(clazz2); // Use the variable to avoid a warning
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       * Verify for an inner class declared at the type level that the EnclosingMethod attribute is set correctly (for example to a
62       * null value)
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       * Verify for an inner class declared inside the 'main' method that the enclosing method attribute is set correctly.
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  }