8345773: Class-File API debug printing capability

Reviewed-by: liach, mcimadamore
This commit is contained in:
Adam Sotona 2024-12-11 07:58:26 +00:00
parent e88e793cfd
commit f88c1c6ff8
2 changed files with 15 additions and 4 deletions

View file

@ -34,6 +34,8 @@ import java.util.function.Consumer;
import java.util.stream.Stream; import java.util.stream.Stream;
import java.util.stream.StreamSupport; import java.util.stream.StreamSupport;
import jdk.internal.classfile.components.ClassPrinter;
/** /**
* A {@link ClassFileElement} that has complex structure defined in terms of * A {@link ClassFileElement} that has complex structure defined in terms of
* other classfile elements, such as a method, field, method body, or entire * other classfile elements, such as a method, field, method body, or entire
@ -92,4 +94,14 @@ public sealed interface CompoundElement<E extends ClassFileElement>
return Collections.unmodifiableList(list); 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();
}
} }

View file

@ -23,7 +23,7 @@
/* /*
* @test * @test
* @bug 8335927 * @bug 8335927 8345773
* @summary Testing ClassFile ClassPrinter. * @summary Testing ClassFile ClassPrinter.
* @run junit ClassPrinterTest * @run junit ClassPrinterTest
*/ */
@ -122,8 +122,7 @@ class ClassPrinterTest {
@Test @Test
void testPrintYamlTraceAll() throws IOException { void testPrintYamlTraceAll() throws IOException {
var out = new StringBuilder(); var out = getClassModel().toDebugString();
ClassPrinter.toYaml(getClassModel(), ClassPrinter.Verbosity.TRACE_ALL, out::append);
assertOut(out, assertOut(out,
""" """
- class name: Foo - class name: Foo
@ -904,7 +903,7 @@ class ClassPrinterTest {
assertEquals(node.walk().count(), 42); 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("-----------------");
// System.out.println(out.toString()); // System.out.println(out.toString());
// System.out.println("-----------------"); // System.out.println("-----------------");