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.
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
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
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)); } }
Modifier and Type | Class and Description |
---|---|
public static interface | ClassPrinter.
Preview
Preview of ClassFile API (JEP 457).
A leaf node holding single printable value.
|
public static interface | ClassPrinter.
Preview
Preview of ClassFile API (JEP 457).
A tree node holding List of nested nodes.
|
public static interface | ClassPrinter.
Preview
Preview of ClassFile API (JEP 457).
A tree node holding Map of nested nodes.
|
public static interface | ClassPrinter.
Preview
Preview of ClassFile API (JEP 457).
Named, traversable, and printable node parent.
|
public static enum | ClassPrinter.
Preview
Preview of ClassFile API (JEP 457).
Level of detail to print or export.
|
Access | Constructor and Description |
---|---|
private |
Modifier and Type | Method and Description |
---|---|
public static void | toJson(CompoundElement<?> model, ClassPrinter.
level of details to print verbosity,consumer of the print fragments out)Prints provided model as structured text in JSON format. |
public static ClassPrinter. | Returns: root node of the exported treelevel of details to export verbosityExports provided model into a tree of printable nodes. |
public static void | toXml(CompoundElement<?> model, ClassPrinter.
level of details to print verbosity,consumer of the print fragments out)Prints provided model as structured text in XML format. |
public static void | toYaml(CompoundElement<?> model, ClassPrinter.
level of details to print verbosity,consumer of the print fragments out)Prints provided model as structured text in YAML format. |
ClassPrinter | back to summary |
---|---|
private ClassPrinter() |
toJson | back to summary |
---|---|
public static void toJson(CompoundElement<?> model, ClassPrinter. Prints provided model as structured text in JSON format.
|
toTree | back to summary |
---|---|
public static ClassPrinter. Exports provided model into a tree of printable nodes.
|
toXml | back to summary |
---|---|
public static void toXml(CompoundElement<?> model, ClassPrinter. Prints provided model as structured text in XML format.
|
toYaml | back to summary |
---|---|
public static void toYaml(CompoundElement<?> model, ClassPrinter. Prints provided model as structured text in YAML format.
|
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.
Modifier and Type | Method and Description |
---|---|
public ConstantDesc |
value | back to summary |
---|---|
public ConstantDesc value() Printable node value
|
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.
List
of nested nodes.
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.
Map
of nested nodes.
Each Map.
== Map.
.name()
.
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.
Modifier and Type | Method and Description |
---|---|
public ConstantDesc | |
public default void | |
public default void | |
public default void | |
public Stream |
name | back to summary |
---|---|
public ConstantDesc name() Printable name of the node.
|
toJson | back to summary |
---|---|
public default void toJson(Consumer<String> out) Prints the node and its sub-tree into JSON format. |
toXml | back to summary |
---|---|
public default void toXml(Consumer<String> out) Prints the node and its sub-tree into XML format. |
toYaml | back to summary |
---|---|
public default void toYaml(Consumer<String> out) Prints the node and its sub-tree into YAML format. |
walk | back to summary |
---|---|
public Stream Walks through the underlying tree.
|
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.
Modifier and Type | Field and Description |
---|---|
public static final ClassPrinter. | CRITICAL_ATTRIBUTES
Top level class info, class members, and critical attributes are printed. |
public static final ClassPrinter. | MEMBERS_ONLY
Only top level class info, class members and attribute names are printed. |
public static final ClassPrinter. | TRACE_ALL
All class content is printed, including constant pool. |
Access | Constructor and Description |
---|---|
private |
Modifier and Type | Method and Description |
---|---|
public static ClassPrinter. | |
public static ClassPrinter. |
CRITICAL_ATTRIBUTES | back to summary |
---|---|
public static final ClassPrinter. Top level class info, class members, and critical attributes are printed. Critical attributes are:
|
MEMBERS_ONLY | back to summary |
---|---|
public static final ClassPrinter. Only top level class info, class members and attribute names are printed. |
TRACE_ALL | back to summary |
---|---|
public static final ClassPrinter. All class content is printed, including constant pool. |
Verbosity | back to summary |
---|---|
private Verbosity() |
valueOf | back to summary |
---|---|
public static ClassPrinter. |
values | back to summary |
---|---|
public static ClassPrinter. |