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.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertNull;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import org.apache.bcel.classfile.InnerClass;
26  import org.apache.bcel.classfile.InnerClasses;
27  import org.apache.bcel.classfile.JavaClass;
28  import org.apache.bcel.data.EmptyClass;
29  import org.junit.jupiter.api.Test;
30  
31  public class AnonymousClassTestCase extends AbstractTestCase {
32      @Test
33      public void testAnonymousInnerClassIsAnonymous() throws ClassNotFoundException {
34          final JavaClass clazz = getTestJavaClass(PACKAGE_BASE_NAME + ".data.AnonymousClassTest$1");
35          assertTrue(clazz.isAnonymous(), "anonymous inner classes are anonymous");
36          assertTrue(clazz.isNested(), "anonymous inner classes are anonymous");
37      }
38  
39      @Test
40      public void testNamedInnerClassIsNotAnonymous() throws ClassNotFoundException {
41          final JavaClass clazz = getTestJavaClass(PACKAGE_BASE_NAME + ".data.AnonymousClassTest$X");
42          assertFalse(clazz.isAnonymous(), "regular inner classes are not anonymous");
43          assertTrue(clazz.isNested(), "regular inner classes are nested");
44      }
45  
46      @Test
47      public void testRegularClassInnerClasses() throws ClassNotFoundException {
48          final JavaClass clazz = getTestJavaClass(PACKAGE_BASE_NAME + ".data.AnonymousClassTest");
49          final InnerClasses innerClasses = clazz.getAttribute(Const.ATTR_INNER_CLASSES);
50          final InnerClass[] innerClassArray = innerClasses.getInnerClasses();
51          assertEquals(3, innerClassArray.length);
52          assertNull(Repository.lookupClass(EmptyClass.class).getAttribute(Const.ATTR_INNER_CLASSES));
53      }
54  
55      @Test
56      public void testRegularClassIsNotAnonymous() throws ClassNotFoundException {
57          final JavaClass clazz = getTestJavaClass(PACKAGE_BASE_NAME + ".data.AnonymousClassTest");
58          assertFalse(clazz.isAnonymous(), "regular outer classes are not anonymous");
59          assertFalse(clazz.isNested(), "regular outer classes are not nested");
60      }
61  
62      @Test
63      public void testStaticInnerClassIsNotAnonymous() throws ClassNotFoundException {
64          final JavaClass clazz = getTestJavaClass(PACKAGE_BASE_NAME + ".data.AnonymousClassTest$Y");
65          assertFalse(clazz.isAnonymous(), "regular static inner classes are not anonymous");
66          assertTrue(clazz.isNested(), "regular static inner classes are nested");
67      }
68  }