Skip to content

Commit

Permalink
ClassPrinterImpl fix to support duplicite attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
asotona committed Nov 24, 2023
1 parent 7f84db8 commit a7a45f8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 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 @@ -970,7 +991,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
4 changes: 2 additions & 2 deletions test/jdk/jdk/classfile/ClassPrinterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -670,13 +670,13 @@ void testPrintXmlTraceAll() throws IOException {
<nest_host>Phee</nest_host>
<nest_members><member>Phoo</member><member>Boo</member><member>Bee</member></nest_members>
<record_components>
<record>
<component>
<name>fee</name>
<type>LPhoo;</type>
<attributes><attribute>Signature</attribute><attribute>RuntimeInvisibleTypeAnnotations</attribute></attributes>
<signature>LPhoo;</signature>
<invisible_type_annotations>
<anno><annotation_class>LBoo;</annotation_class><target_info>FIELD</target_info><values></values></anno></invisible_type_annotations></record></record_components>
<anno><annotation_class>LBoo;</annotation_class><target_info>FIELD</target_info><values></values></anno></invisible_type_annotations></component></record_components>
<invisible_annotations>
<anno><annotation_class>LPhoo;</annotation_class><values><pair><name>flfl</name><value><float>2.0</float></value></pair><pair><name>frfl</name><value><float>3.0</float></value></pair></values></anno></invisible_annotations>
<permitted_subclasses><subclass>Boo</subclass><subclass>Phoo</subclass></permitted_subclasses>
Expand Down

0 comments on commit a7a45f8

Please sign in to comment.