Skip to main content

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 we'll see later) visualizations. Every recipe automatically emits some core metadata (like source path, project info, etc.), but you can also define custom data table 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.
  • Learn how to run and inspect data table output using the Moderne platform or CLI.

Steps

  1. With the rewrite-recipe-starter still open in IntelliJ, open the ClassHierarchy recipe.
  2. Review the ClassHierarchy recipe to see how data tables are defined and populated.
    • Notice there is a ClassHierarchyReport member and that the .insertRow(...) method is called against it in the visitor.
    • This member is marked as transient to make sure it does not get serialized since it is only needed at runtime.
  3. Now open src/main/java/com/yourorg/table/ClassHierarchyReport.java.
    • This is where the structure of the data table is defined. You can see that there is a displayName and description for each field, as well as a type.
    • In this case, there is also an enum defined. This can be a useful way to add more semantic meaning to the data instead of using arbitrary strings.
  4. Now open the unit tests for ClassHierarchy.

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 DataTable and insertRow(...).

Steps

  1. Open the unit test src/test/java/com/yourorg/TrackJavaTodosTest.java again.
    • Notice the code at the top of the test. These lines test for the presence of the expected data tables.
    • Remove the @Disabled annotations, and run the tests to see that it fails with an error that the expected data table is missing. You will need to add a data table that allows these tests to pass.
  2. Now open src/main/java/com/yourorg/table/TodoCommentsReport.java.
    • This code has been provided for you. It defines the fields for the data table that you will need to populate in the next step.
    • All of the members are type String, and the sourcePath one is identical to the example above.
  3. Now open src/main/java/com/yourorg/TrackJavaTodos.java again.
    • Using the knowledge gained in Exercise 2a, add the necessary .insertRow(...) statements to populate the data table.
    • Use the example from Exercise 2a to help you populate sourcePath.
    • If you are having trouble with where to find the data for elementType, use System.out.println() or the debugger to find the right method that matches what the tests show.
      • Hint: Look at the .getTree().getClass() method.
  4. Build your project and run the tests.
    • All tests should pass, and you should see a message that the project was successfully built.
    • If one or more of the tests fail, use the description of the failure to try to find where the problem is.
  5. In case you get completely stuck or just need a reference, here's an example of a completed TrackJavaTodos.java file.

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.