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.classfile;
21  
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertNotNull;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import java.io.IOException;
27  import java.io.InputStream;
28  import java.util.Arrays;
29  import java.util.List;
30  import java.util.jar.JarEntry;
31  import java.util.jar.JarFile;
32  
33  import org.apache.bcel.AbstractTest;
34  import org.apache.bcel.verifier.statics.StringRepresentation;
35  import org.junit.jupiter.api.Test;
36  
37  class PermittedSubclassesTest extends AbstractTest {
38  
39      private static final String SEALED_JAR_PATH = "src/test/resources/sealed/sealed-demo-jdk21.0.8.jar";
40  
41      private static final String SHAPE_CLASS_ENTRY = "org/jd/core/v1/SealedDemo$Shape.class";
42  
43      private JavaClass parseJarClass(final String entryName) throws IOException {
44          try (JarFile jar = new JarFile(SEALED_JAR_PATH)) {
45              final JarEntry entry = jar.getJarEntry(entryName);
46              assertNotNull(entry, "Missing jar entry: " + entryName);
47              try (InputStream inputStream = jar.getInputStream(entry)) {
48                  return new ClassParser(inputStream, entryName).parse();
49              }
50          }
51      }
52  
53      @Test
54      void readsPermittedSubclassesAttribute() throws IOException {
55          final JavaClass clazz = parseJarClass(SHAPE_CLASS_ENTRY);
56          final Attribute[] attributes = findAttribute("PermittedSubclasses", clazz);
57          assertEquals(1, attributes.length, "Expected one PermittedSubclasses attribute");
58          final PermittedSubclasses permittedSubclasses = (PermittedSubclasses) attributes[0];
59          final List<String> classNames = Arrays.asList(permittedSubclasses.getClassNames());
60          assertEquals(2, classNames.size(), "Expected two permitted subclasses");
61          assertTrue(classNames.contains("org.jd.core.v1.SealedDemo$Circle"), "Missing permitted subclass Circle");
62          assertTrue(classNames.contains("org.jd.core.v1.SealedDemo$Rectangle"), "Missing permitted subclass Rectangle");
63      }
64  
65      @Test
66      void stringRepresentationHandlesPermittedSubclasses() throws IOException {
67          final JavaClass clazz = parseJarClass(SHAPE_CLASS_ENTRY);
68          final Attribute[] attributes = findAttribute("PermittedSubclasses", clazz);
69          final PermittedSubclasses permittedSubclasses = (PermittedSubclasses) attributes[0];
70          assertEquals(permittedSubclasses.toString(), new StringRepresentation(permittedSubclasses).toString());
71      }
72  }