Markers
Add arbitrary metadata to LSTs
Markers annotate LST elements with metadata. Visitors can read or attach any type implementing the Marker interface to any LST element's Markers. Markers can be used to identify search results or to communicate between Recipes during OpenRewrite execution. When an LST is printed back to source code most markers, being metadata, have no textual representation within the source. The exception is SearchResult Markers which are printed as comments that indicate the result of a search. OpenRewrite attaches framework provided markers to LSTs.
Usage
Adding Markers to an LST Element
LST implementations providing Markers have at last two methods to add Markers. LST elements are immutable, and that includes their metadata, so these methods return a copy of the LST element with the specified Markers.
Reading Markers from an LST Element
The Markers class provides several convenience methods.
SearchResult
The most common form of Marker in a typical Recipe is a SearchResult. A Recipe which adds SearchResult
markers to an LST is described as a search recipe. SearchResult
s can optionally include a text description. When a SearchResult
is added to an LST element, it is printed as a comment next to the element it is attached to.
Adding a Search Result to an LST
In this example, the search recipe FindAnnotations adds a SearchResult
indicating that it found a matching Annotation.
SearchResult Markers are written back to source code as comments, including the description if one is present.
For example: if the FindMethods("A singleArg(String)")
recipe were applied to this Java source file:
The search result would be visualized like so:
Last updated