Skip to content

Commit

Permalink
8320396: Class-File API ClassModel::verify should include checks from…
Browse files Browse the repository at this point in the history
… hotspot/share/classfile/classFileParser.cpp

Reviewed-by: liach, mcimadamore
  • Loading branch information
asotona committed May 31, 2024
1 parent 2ab8ab5 commit 22ef827
Show file tree
Hide file tree
Showing 6 changed files with 862 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
import java.lang.constant.DirectMethodHandleDesc;
import java.lang.reflect.AccessFlag;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.Set;
import java.util.function.Consumer;
Expand Down Expand Up @@ -75,16 +75,22 @@ public Stream<Node> walk() {
}
}

public static final class ListNodeImpl extends AbstractList<Node> implements ListNode {
public static sealed class ListNodeImpl extends AbstractList<Node> implements ListNode {

private final Style style;
private final ConstantDesc name;
private final Node[] nodes;
protected final List<Node> nodes;

public ListNodeImpl(Style style, ConstantDesc name, Stream<Node> nodes) {
this.style = style;
this.name = name;
this.nodes = nodes.toArray(Node[]::new);
this.nodes = nodes.toList();
}

protected ListNodeImpl(Style style, ConstantDesc name, List<Node> nodes) {
this.style = style;
this.name = name;
this.nodes = nodes;
}

@Override
Expand All @@ -103,18 +109,23 @@ public Style style() {

@Override
public Node get(int index) {
Objects.checkIndex(index, nodes.length);
return nodes[index];
return nodes.get(index);
}

@Override
public int size() {
return nodes.length;
return nodes.size();
}
}

public static final class MapNodeImpl implements MapNode {

private static final class PrivateListNodeImpl extends ListNodeImpl {
PrivateListNodeImpl(Style style, ConstantDesc name, Node... n) {
super(style, name, new ArrayList<>(List.of(n)));
}
}

private final Style style;
private final ConstantDesc name;
private final Map<ConstantDesc, Node> map;
Expand Down Expand Up @@ -198,9 +209,19 @@ public Set<Entry<ConstantDesc, Node>> entrySet() {


MapNodeImpl with(Node... nodes) {
for (var n : nodes)
if (n != null && map.put(n.name(), n) != null)
throw new AssertionError("Double entry of " + n.name() + " into " + name);
for (var n : nodes) {
if (n != null) {
var prev = map.putIfAbsent(n.name(), n);
if (prev != null) {
//nodes with duplicite keys are joined into a list
if (prev instanceof PrivateListNodeImpl list) {
list.nodes.add(n);
} else {
map.put(n.name(), new PrivateListNodeImpl(style, n.name(), prev, n));
}
}
}
}
return this;
}
}
Expand Down Expand Up @@ -975,7 +996,7 @@ private static Node[] attributesToTree(List<Attribute<?>> attributes, Verbosity
nodes.add(leaf("module main class", mmca.mainClass().name().stringValue()));
case RecordAttribute ra ->
nodes.add(new ListNodeImpl(BLOCK, "record components", ra.components().stream()
.map(rc -> new MapNodeImpl(BLOCK, "record")
.map(rc -> new MapNodeImpl(BLOCK, "component")
.with(leafs(
"name", rc.name().stringValue(),
"type", rc.descriptor().stringValue()))
Expand Down
Loading

0 comments on commit 22ef827

Please sign in to comment.