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