Skip to content

Commit 6fdfa72

Browse files
committed
8345773: Class-File API debug printing capability
Reviewed-by: liach Backport-of: f88c1c6
1 parent 950c8ad commit 6fdfa72

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/java.base/share/classes/java/lang/classfile/CompoundElement.java

+12
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import java.util.stream.Stream;
3535
import java.util.stream.StreamSupport;
3636

37+
import jdk.internal.classfile.components.ClassPrinter;
38+
3739
/**
3840
* A {@link ClassFileElement} that has complex structure defined in terms of
3941
* other classfile elements, such as a method, field, method body, or entire
@@ -92,4 +94,14 @@ public void accept(E e) {
9294
return Collections.unmodifiableList(list);
9395
}
9496

97+
/**
98+
* {@return a text representation of the compound element and its contents for debugging purposes}
99+
*
100+
* The format, structure and exact contents of the returned string are not specified and may change at any time in the future.
101+
*/
102+
default String toDebugString() {
103+
StringBuilder text = new StringBuilder();
104+
ClassPrinter.toYaml(this, ClassPrinter.Verbosity.TRACE_ALL, text::append);
105+
return text.toString();
106+
}
95107
}

test/jdk/jdk/classfile/ClassPrinterTest.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 8335927
26+
* @bug 8335927 8345773
2727
* @summary Testing ClassFile ClassPrinter.
2828
* @run junit ClassPrinterTest
2929
*/
@@ -122,8 +122,7 @@ ClassModel getClassModel() {
122122

123123
@Test
124124
void testPrintYamlTraceAll() throws IOException {
125-
var out = new StringBuilder();
126-
ClassPrinter.toYaml(getClassModel(), ClassPrinter.Verbosity.TRACE_ALL, out::append);
125+
var out = getClassModel().toDebugString();
127126
assertOut(out,
128127
"""
129128
- class name: Foo
@@ -904,7 +903,7 @@ void testWalkMembersOnly() throws IOException {
904903
assertEquals(node.walk().count(), 42);
905904
}
906905

907-
private static void assertOut(StringBuilder out, String expected) {
906+
private static void assertOut(CharSequence out, String expected) {
908907
// System.out.println("-----------------");
909908
// System.out.println(out.toString());
910909
// System.out.println("-----------------");

0 commit comments

Comments
 (0)