Module 2: Data tables
Data tables allow you to extract structured insights from a codebase without modifying the code itself. Instead of applying transformations, you collect data and present it in tabular form that can easily be used for reporting, analytics, or (as you'll see later) visualizations. Every recipe run produces some default data tables (such as which source files had results), and every data table includes some common columns by default (like source path). Beyond these defaults, you can also define custom data tables with their own columns to capture additional information.
Exercise 2a: Exploring data tables
In this exercise, you’ll review the use of a data table to extract superclass relationships and present them in a structured format.
Goals for this exercise
- See how to define and populate data tables in recipes.
- See how a data table's structure is defined and how its contents are verified in tests.
Steps
- In the
rewrite-recipe-starterproject, openClassHierarchy.javaand note thetransient ClassHierarchyReportmember and the.insertRow(...)call in the visitor. (It istransientso it is not serialized, since it is only needed at runtime.) - Open
table/ClassHierarchyReport.javato see how the data table is defined: each column has adisplayName,description, and type, and anenumis used to give the data more semantic meaning than arbitrary strings. - Open
ClassHierarchyTest.javaand note how aRecipeSpecwithassertThat(rows).containsExactly(...)verifies the data table contents.
Takeaways
- Data tables extract insights without changing source code.
- They are ideal for visualizing patterns across a codebase.
- Recipes can define custom columns for targeted reporting.
Exercise 2b: Writing a recipe that uses data tables
In this exercise, you'll write a recipe to find any comments in Java source files that contain TODO and capture them in a data table.
Goals for this exercise
- Learn how to define and populate a data table from a recipe.
- Understand how to structure output using
DataTableandinsertRow(...).
Steps
- Open the unit test
TrackJavaTodosTest.java. The lines at the top test for the presence of the expected data table. Remove the@Disabledannotations and run the tests — they fail because the data table is missing. - Open
table/TodoCommentsReport.java. This is provided for you and defines the data table fields you will populate. All members areString, andsourcePathis identical to theClassHierarchyexample. - Open
TrackJavaTodos.javaand, using what you saw in Exercise 2a, add the.insertRow(...)statements to populate the data table.- Populate
sourcePaththe same way as theClassHierarchyexample. - For
elementType, useSystem.out.println()or the debugger to find the right method. Hint: look at.getTree().getClass().
- Populate
- Build your project and run the tests. They should all pass. If any fail, use the failure description to locate the problem.
- If you get stuck, see the completed
TrackJavaTodos.javafor reference.
Takeaways
- Data tables let recipes report metadata without modifying source files.
- Debugging tools (like printing or stepping through with a debugger) can help find the right fields to populate a table.