ClassPrinter (Java SE 22 & JDK 22)
Description: declaration: module: java.base, package: java.lang.classfile.components, class: ClassPrinter
Keywords: java.lang.classfile.components.ClassPrinter class

Module java.base
Package java.lang.classfile.componentsPREVIEW

# Class ClassPrinter

java.lang.Object
java.lang.classfile.components.ClassPrinter

public final class ClassPrinter extends Object
`ClassPrinter` is a preview API of the Java platform.
Programs can only use `ClassPrinter` when preview features are enabled.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.
A printer of classfiles and its elements.

Any `ClassModel`PREVIEW, `FieldModel`PREVIEW, `MethodModel`PREVIEW, or `CodeModel`PREVIEW can be printed to a human-readable structured text in JSON, XML, or YAML format. Or it can be exported into a tree of traversable and printable nodes, more exactly into a tree of `ClassPrinter.MapNode`PREVIEW, `ClassPrinter.ListNode`PREVIEW, and `ClassPrinter.LeafNode`PREVIEW instances.

Level of details to print or to export is driven by `ClassPrinter.Verbosity`PREVIEW option.

Printing is for debugging purposes only. Printed text schema, tree content and structure not guaranteed. It may change anytime in a future.

The most frequent use case is to simply print a class:

    ClassPrinter.toJson(classModel, ClassPrinter.Verbosity.TRACE_ALL, System.out::print);

`ClassPrinter`PREVIEW allows to traverse tree of simple printable nodes to hook custom printer:

    void customPrint(ClassModel classModel) {
        print(ClassPrinter.toTree(classModel, ClassPrinter.Verbosity.TRACE_ALL));
    }

    void print(ClassPrinter.Node node) {
        switch (node) {
            case ClassPrinter.MapNode mn -> {
                // print map header
                mn.values().forEach(this::print);
            }
            case ClassPrinter.ListNode ln -> {
                // print list header
                ln.forEach(this::print);
            }
            case ClassPrinter.LeafNode n -> {
                // print leaf node
            }
        }
    }

Another use case for `ClassPrinter`PREVIEW is to simplify writing of automated tests:

    @Test
    void printNodesInTest(ClassModel classModel) {
        var classNode = ClassPrinter.toTree(classModel, ClassPrinter.Verbosity.TRACE_ALL);
        assertContains(classNode, "method name", "myFooMethod");
        assertContains(classNode, "field name", "myBarField");
        assertContains(classNode, "inner class", "MyInnerFooClass");
    }

    void assertContains(ClassPrinter.Node node, ConstantDesc key, ConstantDesc value) {
        if (!node.walk().anyMatch(n -> n instanceof ClassPrinter.LeafNode ln
                               && ln.name().equals(key)
                               && ln.value().equals(value))) {
            node.toYaml(System.out::print);
            throw new AssertionError("expected %s: %s".formatted(key, value));
        }
    }

Since:
  22

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static interface`
  `ClassPrinter.LeafNodePREVIEW`
  Preview.
  A leaf node holding single printable value.
  `static interface`
  `ClassPrinter.ListNodePREVIEW`
  Preview.
  A tree node holding `List` of nested nodes.
  `static interface`
  `ClassPrinter.MapNodePREVIEW`
  Preview.
  A tree node holding `Map` of nested nodes.
  `static interface`
  `ClassPrinter.NodePREVIEW`
  Preview.
  Named, traversable, and printable node parent.
  `static enum`
  `ClassPrinter.VerbosityPREVIEW`
  Preview.
  Level of detail to print or export.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static void`
  `toJson(CompoundElementPREVIEW<?> model, ClassPrinter.VerbosityPREVIEW verbosity, Consumer<String> out)`
  Prints provided model as structured text in JSON format.
  `static ClassPrinter.MapNodePREVIEW`
  `toTree(CompoundElementPREVIEW<?> model, ClassPrinter.VerbosityPREVIEW verbosity)`
  Exports provided model into a tree of printable nodes.
  `static void`
  `toXml(CompoundElementPREVIEW<?> model, ClassPrinter.VerbosityPREVIEW verbosity, Consumer<String> out)`
  Prints provided model as structured text in XML format.
  `static void`
  `toYaml(CompoundElementPREVIEW<?> model, ClassPrinter.VerbosityPREVIEW verbosity, Consumer<String> out)`
  Prints provided model as structured text in YAML format.

  ### Methods declared in class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### toTree

    public static ClassPrinter.MapNodePREVIEW toTree(CompoundElementPREVIEW<?> model, ClassPrinter.VerbosityPREVIEW verbosity)
    Exports provided model into a tree of printable nodes.
    Parameters:
      `model` - a `ClassModel`PREVIEW, `FieldModel`PREVIEW, `MethodModel`PREVIEW, or `CodeModel`PREVIEW to export
      `verbosity` - level of details to export
    Returns:
      root node of the exported tree

  - ### toJson

    public static void toJson(CompoundElementPREVIEW<?> model, ClassPrinter.VerbosityPREVIEW verbosity, Consumer<String> out)
    Prints provided model as structured text in JSON format.
    Parameters:
      `model` - a `ClassModel`PREVIEW, `FieldModel`PREVIEW, `MethodModel`PREVIEW, or `CodeModel`PREVIEW to print
      `verbosity` - level of details to print
      `out` - consumer of the print fragments

  - ### toXml

    public static void toXml(CompoundElementPREVIEW<?> model, ClassPrinter.VerbosityPREVIEW verbosity, Consumer<String> out)
    Prints provided model as structured text in XML format.
    Parameters:
      `model` - a `ClassModel`PREVIEW, `FieldModel`PREVIEW, `MethodModel`PREVIEW, or `CodeModel`PREVIEW to print
      `verbosity` - level of details to print
      `out` - consumer of the print fragments

  - ### toYaml

    public static void toYaml(CompoundElementPREVIEW<?> model, ClassPrinter.VerbosityPREVIEW verbosity, Consumer<String> out)
    Prints provided model as structured text in YAML format.
    Parameters:
      `model` - a `ClassModel`PREVIEW, `FieldModel`PREVIEW, `MethodModel`PREVIEW, or `CodeModel`PREVIEW to print
      `verbosity` - level of details to print
      `out` - consumer of the print fragments