Top Description Inners Constructors Methods
java.lang.classfile.components

public final Class ClassPrinter

extends Object
Class Inheritance
Annotations
@PreviewFeature
feature:CLASSFILE_API
Imports
java.lang.constant.ConstantDesc, java.util.List, .Map, java.util.function.Consumer, java.util.stream.Stream, java.lang.classfile.ClassModel, .FieldModel, .MethodModel, .CodeModel, .CompoundElement, jdk.internal.classfile.impl.ClassPrinterImpl, jdk.internal.javac.PreviewFeature

Preview

Preview of ClassFile API (JEP 457).

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, FieldModel, MethodModel, or CodeModel 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 MapNode, ListNode, and LeafNode instances.

Level of details to print or to export is driven by Verbosity 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.toJson(classModel, ClassPrinter.Verbosity.TRACE_ALL, System.out::print);

ClassPrinter 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 } } }
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 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)); } }
@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 and Inner Type Summary

Modifier and TypeClass and Description
public static interface
ClassPrinter.LeafNode

Preview Preview of ClassFile API (JEP 457).

A leaf node holding single printable value.
public static interface
ClassPrinter.ListNode

Preview Preview of ClassFile API (JEP 457).

A tree node holding List of nested nodes.
public static interface
ClassPrinter.MapNode

Preview Preview of ClassFile API (JEP 457).

A tree node holding Map of nested nodes.
public static interface
ClassPrinter.Node

Preview Preview of ClassFile API (JEP 457).

Named, traversable, and printable node parent.
public static enum
ClassPrinter.Verbosity

Preview Preview of ClassFile API (JEP 457).

Level of detail to print or export.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static void
toJson(CompoundElement<?> model, ClassPrinter.Verbosity
level of details to print
verbosity
,
Consumer<String>
consumer of the print fragments
out
)

Prints provided model as structured text in JSON format.

public static ClassPrinter.MapNode

Returns:

root node of the exported tree
toTree
(CompoundElement<?> model, ClassPrinter.Verbosity
level of details to export
verbosity
)

Exports provided model into a tree of printable nodes.

public static void
toXml(CompoundElement<?> model, ClassPrinter.Verbosity
level of details to print
verbosity
,
Consumer<String>
consumer of the print fragments
out
)

Prints provided model as structured text in XML format.

public static void
toYaml(CompoundElement<?> model, ClassPrinter.Verbosity
level of details to print
verbosity
,
Consumer<String>
consumer of the print fragments
out
)

Prints provided model as structured text in YAML format.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

ClassPrinterback to summary
private ClassPrinter()

Method Detail

toJsonback to summary
public static void toJson(CompoundElement<?> model, ClassPrinter.Verbosity verbosity, Consumer<String> out)

Prints provided model as structured text in JSON format.

Parameters
model:CompoundElement<?>

a ClassModel, FieldModel, MethodModel, or CodeModel to print

verbosity:ClassPrinter.Verbosity

level of details to print

out:Consumer<String>

consumer of the print fragments

toTreeback to summary
public static ClassPrinter.MapNode toTree(CompoundElement<?> model, ClassPrinter.Verbosity verbosity)

Exports provided model into a tree of printable nodes.

Parameters
model:CompoundElement<?>

a ClassModel, FieldModel, MethodModel, or CodeModel to export

verbosity:ClassPrinter.Verbosity

level of details to export

Returns:ClassPrinter.MapNode

root node of the exported tree

toXmlback to summary
public static void toXml(CompoundElement<?> model, ClassPrinter.Verbosity verbosity, Consumer<String> out)

Prints provided model as structured text in XML format.

Parameters
model:CompoundElement<?>

a ClassModel, FieldModel, MethodModel, or CodeModel to print

verbosity:ClassPrinter.Verbosity

level of details to print

out:Consumer<String>

consumer of the print fragments

toYamlback to summary
public static void toYaml(CompoundElement<?> model, ClassPrinter.Verbosity verbosity, Consumer<String> out)

Prints provided model as structured text in YAML format.

Parameters
model:CompoundElement<?>

a ClassModel, FieldModel, MethodModel, or CodeModel to print

verbosity:ClassPrinter.Verbosity

level of details to print

out:Consumer<String>

consumer of the print fragments

java.lang.classfile.components back to summary

public sealed Interface ClassPrinter.LeafNode

extends Node
Known Direct Implementers
jdk.internal.classfile.impl.ClassPrinterImpl.LeafNodeImpl
Annotations
@PreviewFeature
feature:CLASSFILE_API

Preview

Preview of ClassFile API (JEP 457).

Programs can only use LeafNode when preview features are enabled.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.

A leaf node holding single printable value.
Since
22

Method Summary

Modifier and TypeMethod and Description
public ConstantDesc

Returns:

node value
value
()

Printable node value

Inherited from java.lang.classfile.components.ClassPrinter.Node:
nametoJsontoXmltoYamlwalk

Method Detail

valueback to summary
public ConstantDesc value()

Printable node value

Returns:ConstantDesc

node value

java.lang.classfile.components back to summary

public sealed Interface ClassPrinter.ListNode

extends Node, List<ClassPrinter.Node>
Known Direct Implementers
jdk.internal.classfile.impl.ClassPrinterImpl.ListNodeImpl
Annotations
@PreviewFeature
feature:CLASSFILE_API

Preview

Preview of ClassFile API (JEP 457).

Programs can only use ListNode when preview features are enabled.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.

A tree node holding List of nested nodes.
Since
22

Method Summary

Inherited from java.lang.classfile.components.ClassPrinter.Node:
nametoJsontoXmltoYamlwalk
Inherited from java.util.List:
addaddaddAlladdAlladdFirstaddLastclearcontainscontainsAllcopyOfequalsgetgetFirstgetLasthashCodeindexOfisEmptyiteratorlastIndexOflistIteratorlistIteratorofofofofofofofofofofofofremoveremoveremoveAllremoveFirstremoveLastreplaceAllretainAllreversedsetsizesortspliteratorsubListtoArraytoArray
java.lang.classfile.components back to summary

public sealed Interface ClassPrinter.MapNode

extends Node, Map<ConstantDesc, ClassPrinter.Node>
Known Direct Implementers
jdk.internal.classfile.impl.ClassPrinterImpl.MapNodeImpl
Annotations
@PreviewFeature
feature:CLASSFILE_API

Preview

Preview of ClassFile API (JEP 457).

Programs can only use MapNode when preview features are enabled.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.

A tree node holding Map of nested nodes.

Each Map.Entry#getKey() == Map.Entry#getValue().name().

Since
22

Method Summary

Inherited from java.lang.classfile.components.ClassPrinter.Node:
nametoJsontoXmltoYamlwalk
Inherited from java.util.Map:
clearcomputecomputeIfAbsentcomputeIfPresentcontainsKeycontainsValuecopyOfentryentrySetequalsforEachgetgetOrDefaulthashCodeisEmptykeySetmergeofofofofofofofofofofofofEntriesputputAllputIfAbsentremoveremovereplacereplacereplaceAllsizevalues
java.lang.classfile.components back to summary

public sealed Interface ClassPrinter.Node

Known Direct Subinterfaces
java.lang.classfile.components.ClassPrinter.LeafNode, java.lang.classfile.components.ClassPrinter.ListNode, java.lang.classfile.components.ClassPrinter.MapNode
Annotations
@PreviewFeature
feature:CLASSFILE_API

Preview

Preview of ClassFile API (JEP 457).

Programs can only use Node when preview features are enabled.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.

Named, traversable, and printable node parent.
Since
22

Method Summary

Modifier and TypeMethod and Description
public ConstantDesc

Returns:

name of the node
name
()

Printable name of the node.

public default void
toJson(Consumer<String>
consumer of the printed fragments
out
)

Prints the node and its sub-tree into JSON format.

public default void
toXml(Consumer<String>
consumer of the printed fragments
out
)

Prints the node and its sub-tree into XML format.

public default void
toYaml(Consumer<String>
consumer of the printed fragments
out
)

Prints the node and its sub-tree into YAML format.

public Stream<ClassPrinter.Node>

Returns:

ordered stream of nodes
walk
()

Walks through the underlying tree.

Method Detail

nameback to summary
public ConstantDesc name()

Printable name of the node.

Returns:ConstantDesc

name of the node

toJsonback to summary
public default void toJson(Consumer<String> out)

Prints the node and its sub-tree into JSON format.

Parameters
out:Consumer<String>

consumer of the printed fragments

toXmlback to summary
public default void toXml(Consumer<String> out)

Prints the node and its sub-tree into XML format.

Parameters
out:Consumer<String>

consumer of the printed fragments

toYamlback to summary
public default void toYaml(Consumer<String> out)

Prints the node and its sub-tree into YAML format.

Parameters
out:Consumer<String>

consumer of the printed fragments

walkback to summary
public Stream<ClassPrinter.Node> walk()

Walks through the underlying tree.

Returns:Stream<ClassPrinter.Node>

ordered stream of nodes

java.lang.classfile.components back to summary

public final Enum ClassPrinter.Verbosity

extends Enum<ClassPrinter.Verbosity>
Class Inheritance
Annotations
@PreviewFeature
feature:CLASSFILE_API

Preview

Preview of ClassFile API (JEP 457).

Programs can only use Verbosity when preview features are enabled.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.

Level of detail to print or export.
Since
22

Field Summary

Modifier and TypeField and Description
public static final ClassPrinter.Verbosity
CRITICAL_ATTRIBUTES

Top level class info, class members, and critical attributes are printed.

public static final ClassPrinter.Verbosity
MEMBERS_ONLY

Only top level class info, class members and attribute names are printed.

public static final ClassPrinter.Verbosity
TRACE_ALL

All class content is printed, including constant pool.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static ClassPrinter.Verbosity
public static ClassPrinter.Verbosity[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

CRITICAL_ATTRIBUTESback to summary
public static final ClassPrinter.Verbosity CRITICAL_ATTRIBUTES

Top level class info, class members, and critical attributes are printed.

Critical attributes are:

  • ConstantValue
  • Code
  • StackMapTable
  • BootstrapMethods
  • NestHost
  • NestMembers
  • PermittedSubclasses
Java Virtual Machine Specification
4.7 Attributes
MEMBERS_ONLYback to summary
public static final ClassPrinter.Verbosity MEMBERS_ONLY

Only top level class info, class members and attribute names are printed.

TRACE_ALLback to summary
public static final ClassPrinter.Verbosity TRACE_ALL

All class content is printed, including constant pool.

Constructor Detail

Verbosityback to summary
private Verbosity()

Method Detail

valueOfback to summary
public static ClassPrinter.Verbosity valueOf(String name)
valuesback to summary
public static ClassPrinter.Verbosity[] values()