Class CodeSnippet

java.lang.Object
org.springframework.javapoet.support.CodeSnippet

public final class CodeSnippet extends Object
A code snippet using tabs indentation that is fully processed by JavaPoet so that imports are resolved.
Since:
6.0
Author:
Stephane Nicoll
  • Method Summary

    Modifier and Type
    Method
    Description
    Return the rendered code snippet.
    boolean
    hasImport(Class<?> type)
    Specify if an import statement for the specified type is present.
    boolean
    hasImport(String className)
    Specify if an import statement for the specified class name is present.
    of(org.springframework.javapoet.CodeBlock code)
    Create a CodeSnippet using the specified code.
    static String
    process(Consumer<org.springframework.javapoet.CodeBlock.Builder> code)
    Process the specified code and return a fully-processed code snippet as a String.
    static String
    process(org.springframework.javapoet.CodeBlock code)
    Process the specified code and return a fully-processed code snippet as a String.
    removeIndent(int indent)
    Return a new CodeSnippet where the specified number of indentations have been removed.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getSnippet

      public String getSnippet()
      Return the rendered code snippet.
      Returns:
      a code snippet where imports have been resolved
    • hasImport

      public boolean hasImport(Class<?> type)
      Specify if an import statement for the specified type is present.
      Parameters:
      type - the type to check
      Returns:
      true if this type has an import statement, false otherwise
    • hasImport

      public boolean hasImport(String className)
      Specify if an import statement for the specified class name is present.
      Parameters:
      className - the name of the class to check
      Returns:
      true if this type has an import statement, false otherwise
    • removeIndent

      public CodeSnippet removeIndent(int indent)
      Return a new CodeSnippet where the specified number of indentations have been removed.
      Parameters:
      indent - the number of indent to remove
      Returns:
      a CodeSnippet instance with the number of indentations removed
    • of

      public static CodeSnippet of(org.springframework.javapoet.CodeBlock code)
      Create a CodeSnippet using the specified code.
      Parameters:
      code - the code snippet
      Returns:
      a CodeSnippet instance
    • process

      public static String process(Consumer<org.springframework.javapoet.CodeBlock.Builder> code)
      Process the specified code and return a fully-processed code snippet as a String.
      Parameters:
      code - a consumer to use to generate the code snippet
      Returns:
      a resolved code snippet
    • process

      public static String process(org.springframework.javapoet.CodeBlock code)
      Process the specified code and return a fully-processed code snippet as a String.
      Parameters:
      code - the code snippet
      Returns:
      a resolved code snippet