-
Notifications
You must be signed in to change notification settings - Fork 132
Add support for permitted subclasses #493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
src/main/java/org/apache/bcel/classfile/PermittedSubclasses.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.bcel.classfile; | ||
|
|
||
| import java.io.DataInput; | ||
| import java.io.DataOutputStream; | ||
| import java.io.IOException; | ||
| import java.util.Arrays; | ||
|
|
||
| import org.apache.bcel.Const; | ||
| import org.apache.bcel.util.Args; | ||
| import org.apache.commons.lang3.ArrayUtils; | ||
|
|
||
| /** | ||
| * This class is derived from <em>Attribute</em> and records the classes and interfaces that are permitted to extend or | ||
| * implement the current class or interface. There may be at most one PermittedSubclasses attribute in a ClassFile | ||
| * structure. | ||
| * | ||
| * @see Attribute | ||
| * @since 6.13.0 | ||
| */ | ||
| public final class PermittedSubclasses extends Attribute { | ||
|
|
||
| private int[] classes; | ||
|
|
||
| /** | ||
| * Constructs object from input stream. | ||
| * | ||
| * @param nameIndex Index in constant pool. | ||
| * @param length Content length in bytes. | ||
| * @param dataInput Input stream. | ||
| * @param constantPool Array of constants. | ||
| * @throws IOException if an I/O error occurs. | ||
| */ | ||
| PermittedSubclasses(final int nameIndex, final int length, final DataInput dataInput, final ConstantPool constantPool) throws IOException { | ||
| this(nameIndex, length, (int[]) null, constantPool); | ||
| classes = ClassParser.readU2U2Table(dataInput); | ||
| } | ||
|
|
||
| /** | ||
| * Constructs object from table of class indices in constant pool. | ||
| * | ||
| * @param nameIndex Index in constant pool. | ||
| * @param length Content length in bytes. | ||
| * @param classes Table of indices in constant pool. | ||
| * @param constantPool Array of constants. | ||
| */ | ||
| public PermittedSubclasses(final int nameIndex, final int length, final int[] classes, final ConstantPool constantPool) { | ||
| super(Const.ATTR_PERMITTED_SUBCLASSES, nameIndex, length, constantPool); | ||
| this.classes = ArrayUtils.nullToEmpty(classes); | ||
| Args.requireU2(this.classes.length, "classes.length"); | ||
| } | ||
|
|
||
| /** | ||
| * Initialize from another object. Note that both objects use the same references (shallow copy). Use copy() for a | ||
| * physical copy. | ||
| * | ||
| * @param c Source to copy. | ||
| */ | ||
| public PermittedSubclasses(final PermittedSubclasses c) { | ||
| this(c.getNameIndex(), c.getLength(), c.getClasses(), c.getConstantPool()); | ||
| } | ||
|
|
||
| /** | ||
| * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. | ||
| * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects. | ||
| * | ||
| * @param v Visitor object. | ||
| */ | ||
| @Override | ||
| public void accept(final Visitor v) { | ||
| v.visitPermittedSubclasses(this); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a deep clone of this object given constant pool. | ||
| * | ||
| * @return deep copy of this attribute. | ||
nbauma109 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| */ | ||
| @Override | ||
| public Attribute copy(final ConstantPool constantPool) { | ||
| final PermittedSubclasses c = (PermittedSubclasses) clone(); | ||
| if (classes.length > 0) { | ||
| c.classes = classes.clone(); | ||
| } | ||
| c.setConstantPool(constantPool); | ||
| return c; | ||
| } | ||
|
|
||
| /** | ||
| * Dumps PermittedSubclasses attribute to file stream in binary format. | ||
| * | ||
| * @param file Output file stream. | ||
| * @throws IOException if an I/O error occurs. | ||
| */ | ||
| @Override | ||
| public void dump(final DataOutputStream file) throws IOException { | ||
| super.dump(file); | ||
| file.writeShort(classes.length); | ||
| for (final int index : classes) { | ||
| file.writeShort(index); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Gets the class indices in constant pool. | ||
| * | ||
| * @return array of indices into constant pool of class names. | ||
nbauma109 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| */ | ||
| public int[] getClasses() { | ||
| return classes; | ||
| } | ||
|
|
||
| /** | ||
| * Gets permitted class names. | ||
| * | ||
| * @return string array of class names. | ||
nbauma109 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| */ | ||
| public String[] getClassNames() { | ||
| final String[] names = new String[classes.length]; | ||
| Arrays.setAll(names, i -> Utility.pathToPackage(super.getConstantPool().getConstantString(classes[i], Const.CONSTANT_Class))); | ||
| return names; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the number of classes. | ||
| * | ||
| * @return Length of classes table. | ||
nbauma109 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| */ | ||
| public int getNumberClasses() { | ||
| return classes.length; | ||
| } | ||
|
|
||
| /** | ||
| * Sets class indices. | ||
| * | ||
| * @param classes the list of class indexes Also redefines number_of_classes according to table length. | ||
nbauma109 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| */ | ||
| public void setClasses(final int[] classes) { | ||
| this.classes = ArrayUtils.nullToEmpty(classes); | ||
| } | ||
|
|
||
| /** | ||
| * String representation of PermittedSubclasses (for debugging purposes). | ||
| * | ||
| * @return String representation, that is, a list of permitted subclasses. | ||
nbauma109 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| */ | ||
| @Override | ||
| public String toString() { | ||
| final StringBuilder buf = new StringBuilder(); | ||
| buf.append("PermittedSubclasses("); | ||
| buf.append(classes.length); | ||
| buf.append("):\n"); | ||
| for (final int index : classes) { | ||
| final String className = super.getConstantPool().getConstantString(index, Const.CONSTANT_Class); | ||
| buf.append(" ").append(Utility.compactClassName(className, false)).append("\n"); | ||
| } | ||
| return buf.substring(0, buf.length() - 1); // remove the last newline | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/test/java/org/apache/bcel/classfile/PermittedSubclassesTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.bcel.classfile; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.jar.JarEntry; | ||
| import java.util.jar.JarFile; | ||
|
|
||
| import org.apache.bcel.AbstractTest; | ||
| import org.apache.bcel.verifier.statics.StringRepresentation; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class PermittedSubclassesTest extends AbstractTest { | ||
|
|
||
| private static final String SEALED_JAR_PATH = "src/test/resources/sealed/sealed-demo-jdk21.0.8.jar"; | ||
|
|
||
| private static final String SHAPE_CLASS_ENTRY = "org/jd/core/v1/SealedDemo$Shape.class"; | ||
|
|
||
| private JavaClass parseJarClass(final String entryName) throws IOException { | ||
| try (JarFile jar = new JarFile(SEALED_JAR_PATH)) { | ||
| final JarEntry entry = jar.getJarEntry(entryName); | ||
| assertNotNull(entry, "Missing jar entry: " + entryName); | ||
| try (InputStream inputStream = jar.getInputStream(entry)) { | ||
| return new ClassParser(inputStream, entryName).parse(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void readsPermittedSubclassesAttribute() throws IOException { | ||
| final JavaClass clazz = parseJarClass(SHAPE_CLASS_ENTRY); | ||
| final Attribute[] attributes = findAttribute("PermittedSubclasses", clazz); | ||
| assertEquals(1, attributes.length, "Expected one PermittedSubclasses attribute"); | ||
| final PermittedSubclasses permittedSubclasses = (PermittedSubclasses) attributes[0]; | ||
| final List<String> classNames = Arrays.asList(permittedSubclasses.getClassNames()); | ||
| assertEquals(2, classNames.size(), "Expected two permitted subclasses"); | ||
| assertTrue(classNames.contains("org.jd.core.v1.SealedDemo$Circle"), "Missing permitted subclass Circle"); | ||
| assertTrue(classNames.contains("org.jd.core.v1.SealedDemo$Rectangle"), "Missing permitted subclass Rectangle"); | ||
| } | ||
|
|
||
| @Test | ||
| void stringRepresentationHandlesPermittedSubclasses() throws IOException { | ||
| final JavaClass clazz = parseJarClass(SHAPE_CLASS_ENTRY); | ||
| final Attribute[] attributes = findAttribute("PermittedSubclasses", clazz); | ||
| final PermittedSubclasses permittedSubclasses = (PermittedSubclasses) attributes[0]; | ||
| assertEquals(permittedSubclasses.toString(), new StringRepresentation(permittedSubclasses).toString()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package org.jd.core.v1; | ||
|
|
||
| public class SealedDemo { | ||
|
|
||
| sealed interface Shape permits Circle, Rectangle { | ||
| double area(); | ||
| } | ||
|
|
||
| static final class Circle implements Shape { | ||
| private final double radius; | ||
|
|
||
| Circle(double radius) { | ||
| this.radius = radius; | ||
| } | ||
|
|
||
| @Override | ||
| public double area() { | ||
| return Math.PI * radius * radius; | ||
| } | ||
| } | ||
|
|
||
| static non-sealed class Rectangle implements Shape { | ||
| private final double width; | ||
| private final double height; | ||
|
|
||
| Rectangle(double width, double height) { | ||
| this.width = width; | ||
| this.height = height; | ||
| } | ||
|
|
||
| @Override | ||
| public double area() { | ||
| return width * height; | ||
| } | ||
| } | ||
|
|
||
| double sealedSwitch(Shape shape) { | ||
| return switch (shape) { | ||
| case Circle circle -> circle.area(); | ||
| case Rectangle rectangle -> rectangle.area(); | ||
| }; | ||
| } | ||
| } |
Binary file not shown.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.