Skip to content

Commit

Permalink
8345773: Class-File API debug printing capability
Browse files Browse the repository at this point in the history
Reviewed-by: liach, mcimadamore
  • Loading branch information
asotona committed Dec 11, 2024
1 parent e88e793 commit f88c1c6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import jdk.internal.classfile.components.ClassPrinter;

/**
* A {@link ClassFileElement} that has complex structure defined in terms of
* other classfile elements, such as a method, field, method body, or entire
Expand Down Expand Up @@ -92,4 +94,14 @@ public void accept(E e) {
return Collections.unmodifiableList(list);
}

/**
* {@return a text representation of the compound element and its contents for debugging purposes}
*
* The format, structure and exact contents of the returned string are not specified and may change at any time in the future.
*/
default String toDebugString() {
StringBuilder text = new StringBuilder();
ClassPrinter.toYaml(this, ClassPrinter.Verbosity.TRACE_ALL, text::append);
return text.toString();
}
}
7 changes: 3 additions & 4 deletions test/jdk/jdk/classfile/ClassPrinterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

/*
* @test
* @bug 8335927
* @bug 8335927 8345773
* @summary Testing ClassFile ClassPrinter.
* @run junit ClassPrinterTest
*/
Expand Down Expand Up @@ -122,8 +122,7 @@ ClassModel getClassModel() {

@Test
void testPrintYamlTraceAll() throws IOException {
var out = new StringBuilder();
ClassPrinter.toYaml(getClassModel(), ClassPrinter.Verbosity.TRACE_ALL, out::append);
var out = getClassModel().toDebugString();
assertOut(out,
"""
- class name: Foo
Expand Down Expand Up @@ -904,7 +903,7 @@ void testWalkMembersOnly() throws IOException {
assertEquals(node.walk().count(), 42);
}

private static void assertOut(StringBuilder out, String expected) {
private static void assertOut(CharSequence out, String expected) {
// System.out.println("-----------------");
// System.out.println(out.toString());
// System.out.println("-----------------");
Expand Down

0 comments on commit f88c1c6

Please sign in to comment.