java.lang.Object
org.glavo.classfile.components.ClassPrinter
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 ClassPrinter.MapNode, ClassPrinter.ListNode, and ClassPrinter.LeafNode instances.
Level of details to print or to export is driven by ClassPrinter.Verbosity option.
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));
}
}
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA leaf node holding single printable value.static interfaceA tree node holdingListof nested nodes.static interfaceA tree node holdingMapof nested nodes.static interfaceNamed, traversable, and printable node parent.static enumLevel of detail to print or export. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidtoJson(CompoundElement<?> model, ClassPrinter.Verbosity verbosity, Consumer<String> out) Prints provided model as structured text in JSON format.static ClassPrinter.MapNodetoTree(CompoundElement<?> model, ClassPrinter.Verbosity verbosity) Exports provided model into a tree of printable nodes.static voidtoXml(CompoundElement<?> model, ClassPrinter.Verbosity verbosity, Consumer<String> out) Prints provided model as structured text in XML format.static voidtoYaml(CompoundElement<?> model, ClassPrinter.Verbosity verbosity, Consumer<String> out) Prints provided model as structured text in YAML format.
-
Constructor Details
-
ClassPrinter
public ClassPrinter()
-
-
Method Details
-
toTree
public static ClassPrinter.MapNode toTree(CompoundElement<?> model, ClassPrinter.Verbosity verbosity) Exports provided model into a tree of printable nodes.- Parameters:
model- aClassModel,FieldModel,MethodModel, orCodeModelto exportverbosity- level of details to export- Returns:
- root node of the exported tree
-
toJson
public static void toJson(CompoundElement<?> model, ClassPrinter.Verbosity verbosity, Consumer<String> out) Prints provided model as structured text in JSON format.- Parameters:
model- aClassModel,FieldModel,MethodModel, orCodeModelto printverbosity- level of details to printout- consumer of the print fragments
-
toXml
public static void toXml(CompoundElement<?> model, ClassPrinter.Verbosity verbosity, Consumer<String> out) Prints provided model as structured text in XML format.- Parameters:
model- aClassModel,FieldModel,MethodModel, orCodeModelto printverbosity- level of details to printout- consumer of the print fragments
-
toYaml
public static void toYaml(CompoundElement<?> model, ClassPrinter.Verbosity verbosity, Consumer<String> out) Prints provided model as structured text in YAML format.- Parameters:
model- aClassModel,FieldModel,MethodModel, orCodeModelto printverbosity- level of details to printout- consumer of the print fragments
-