Links

Framework Provided Markers

Not everything a Recipe may wish to know about an AST is contained in the AST itself. OpenRewrite attaches Markers to an AST provide Recipes access to relevant metadata. This document provides Recipe authors a reference for what metadata is available and how to access it.

Accessing Provided Markers

All markers described in this document are found on the root element of the AST. The root AST element varies by language, but they all implement the SourceFile interface. From anywhere in a Visitor the cursor can be used to access the markers on the root element like so:
class SomeVisitor implements JavaVisitor<ExecutionContext> {
@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
// This snippet will retrieve the root element's markers in any language, from anywhere in a visitor
Markers m = getCursor().firstEnclosing(SourceFile.class).getMarkers();
// There is only one of each of these markers, so Markers.findFirst() is a convenient way to access them
Optional<BuildTool> buildTool = m.findFirst(BuildTool.class);
// Some markers are language-specific
Optional<JavaProject> javaProject = m.findFirst(JavaProject.class);
return classDecl;
}
}

Build Markers

These markers are language-independent, appearing on source files of all formats.

Build Tool

The BuildTool marker records the type and version of build tool which produced the AST. Available on all ASTs produced by one of our build tool plugins.

Build Environment

The subtypes of BuildEnvironment record information on the continuous integration environment from within which the AST was produced. Available on all ASTs produced from a supported CI environment.
Supported CI environments:

Git Provenance

The GitProvenance marker records Git branch, origin, and change hash. Available on all ASTs produced from a Git repository.

Java Markers

NamedStyles

NamedStyles is a named collection of styles representing code style/formatting and related configuration options. If not explicitly configured the code style a project uses will be auto-detected. It is available on Java source files.

Checkstyle

The CheckStyle marker is a NamedStyles which records Checkstyle settings. It is available on Java source files in projects that configure checkstyle.

JavaProject

JavaProject records the publication coordinates (groupId, artifactId, version) of the module which contains the sources. Available on all sources, not only Java sources, in projects where these publication coordinates are configured.

JavaSourceSet

JavaSourceSet records the name (usually "main" or "test") of the source set containing the source file, and list of fully qualified type names present on the classpath. Available on all Java sources, as well as non-Java sources placed in a Java source set's resources directory.

JavaVersion

JavaVersion records the version of Java used to compile the source, including source and target compatibility. Available on all Java sources.

Maven Markers

These markers are available on Maven pom.xml sources.

MavenResolutionResult

MavenResolutionResult contains a rich data model of a pom.xml, including full dependency resolution information.