Class TreeBuilder


  • @NonNullApi
    public class TreeBuilder
    extends java.lang.Object
    • Method Detail

      • buildName

        public static <T extends TypeTree & Expression> T buildName​(java.lang.String fullyQualifiedName)
      • buildName

        public static <T extends TypeTree & Expression> T buildName​(java.lang.String fullyQualifiedName,
                                                                    org.openrewrite.Formatting fmt)
      • buildName

        public static <T extends TypeTree & Expression> T buildName​(java.lang.String fullyQualifiedName,
                                                                    org.openrewrite.Formatting fmt,
                                                                    java.util.UUID id)
      • buildDeclaration

        public J buildDeclaration​(J.ClassDecl insertionScope,
                                  java.lang.String snippet,
                                  JavaType... types)
        Build a class-scoped declaration. A "class-scoped declaration" is anything you can put inside a class declaration. Examples of such statements include method declarations, field declarations, inner class declarations, and static initializers.
        Parameters:
        insertionScope - The class this declaration is being inserted into.
        snippet - The declaration code to insert
        types - specify any
      • buildInnerClassDeclaration

        public J.ClassDecl buildInnerClassDeclaration​(J.ClassDecl insertionScope,
                                                      java.lang.String classDeclarationSnippet,
                                                      JavaType... types)
      • buildMethodDeclaration

        public J.MethodDecl buildMethodDeclaration​(J.ClassDecl insertionScope,
                                                   java.lang.String methodDeclarationSnippet,
                                                   JavaType... types)
      • buildSnippet

        public <T extends J> java.util.List<T> buildSnippet​(org.openrewrite.Cursor insertionScope,
                                                            java.lang.String snippet,
                                                            JavaType... imports)
        Manually constructing complex AST elements programmatically can be tedious and foreign to developers used to authoring code as text. This method provides a mechanism for authorizing a snippet of code as text, within the context of the insertion point, that will then be converted into an abstract syntax tree.

        The method uses a parser that is constructed from the compilation unit associated with the insertionScope. Currently, the parser is seeded with the runtime classpath and, while this will work for a majority of cases, if types are referenced in the snippet but are not on the runtime classpath, no type attribution will be applied to any of the elements returned.

        On a best-effort basis, this method attempts to include variables and method invocations from the original insertion scope. Those references should be valid within the code snippet if the original insertion scope has proper type attribution.

        Any types introduced into the snippet that are NOT already present in the insertion scope MUST be explicitly enumerated using the "imports" parameter.

        A syntactically correct snippet of code will result in a list of AST elements that represent the text, however, type attribution will only occur if the parser can fully resolve types used within the snippets AND available on the runtime classpath.

        If type attribution fails for any reason, the resulting snippet may still serialize back to the correct Java source code. But any other visitors running as part of the same pipeline that would operate on the snippet will likely fail to do so if they depend on correct type attribution, as most visitors do.

        Type Parameters:
        T - The expected type of element that is returned from the snippet.
        Parameters:
        insertionScope - A point within an existing AST where this snippet will be inserted.
        snippet - A valid code snippet within the context of the current insertion point.
        imports - Any types introduced into the snippet that were not originally present within the compilation unit.
        Returns:
        A list of AST elements constructed from the snippet.