Class LordOfTheStrings

java.lang.Object
org.springframework.data.javapoet.LordOfTheStrings

public abstract class LordOfTheStrings extends Object
Utility class for creating Java code blocks using a fluent API. This class provides a structured and extensible programming model to simplify the creation of method calls, return statements, and complex code structures on top of JavaPoet. It is designed to reduce conditional nesting and improve readability in code generation scenarios.

This class introduces additional abstractions such as LordOfTheStrings.CodeBlockBuilder, LordOfTheStrings.InvocationBuilder, and LordOfTheStrings.TypedReturnBuilder to facilitate the construction of dynamic code blocks. These abstractions enable developers to create code with conditional logic, argument concatenation, and control flow in a declarative and intuitive manner.

This class is intended for internal use within the framework and is not meant to be used directly by application developers.

Since:
4.0
Author:
Mark Paluch
  • Method Details

    • builder

      public static LordOfTheStrings.CodeBlockBuilder builder()
      Create a new CodeBlockBuilder instance.
      Returns:
      a new CodeBlockBuilder.
    • builder

      public static LordOfTheStrings.CodeBlockBuilder builder(org.springframework.javapoet.CodeBlock.Builder builder)
      Create a new CodeBlockBuilder instance using the given CodeBlock.Builder.
      Parameters:
      builder - the CodeBlock.Builder to use.
      Returns:
      a new CodeBlockBuilder.
    • builder

      public static LordOfTheStrings.CodeBlockBuilder builder(String format, @Nullable Object... args)
      Create a new CodeBlockBuilder instance with an initial format and arguments.
      Parameters:
      format - the format string.
      args - the arguments for the format string.
      Returns:
      a new initialized CodeBlockBuilder.
    • invoke

      public static LordOfTheStrings.InvocationBuilder invoke(String methodCall, Object... arguments)
      Create a LordOfTheStrings.InvocationBuilder for building method invocation code.

      The given methodCall may contain format specifiers as defined in Java Poet. It must additionally contain a format specifier (last position) that is used to expand the method arguments when intending to provide arguments, for example:

       Sort sort = …;
       MethodCallBuilder method = PoetrySlam.method("$T.by($L)", Sort.class);
      
       method.arguments(sort, (order, builder) -> {
              builder.add("$T.asc($S)", Sort.Order.class, order.getProperty());
       });
      
       method.build();
       
      The presence of arguments is detected when calling any LordOfTheStrings.InvocationBuilder.argument(java.lang.String) or LordOfTheStrings.InvocationBuilder.arguments method. Providing an empty CodeBlock or Iterable activates argument processing for easier handling when calling this static method. Note that the argument placeholder in methodCall must be omitted if no arguments are added.
      Parameters:
      methodCall - the invocation (or method call) format string.
      arguments - the arguments for the method call.
      Returns:
      a new MethodCallBuilder.
    • returning

      public static LordOfTheStrings.TypedReturnBuilder returning(ResolvableType returnType)
      Create a builder for a return statement targeting the given return type. Any formats provided to LordOfTheStrings.ReturnBuilderSupport must not contain the return keyword as this will be included when building the resulting CodeBlock, for example:
       Method method = …;
       CodeBlock block = LordOfTheStrings.returning(ResolvableType.forMethodReturnType(method))
           .whenBoxedLong("$T.valueOf(1)", Long.class)
           .whenLong("1L")
           .otherwise("0L")
           .build();
       
      Parameters:
      returnType - the method return type.
      Returns:
      a new ReturnStatementBuilder.
    • returning

      public static LordOfTheStrings.TypedReturnBuilder returning(Class<?> returnType)
      Create a builder for a return statement targeting the given return type. Any formats provided to LordOfTheStrings.ReturnBuilderSupport must not contain the return keyword as this will be included when building the resulting CodeBlock, for example:
       Method method = …;
       CodeBlock block = LordOfTheStrings.returning(method.getReturnType())
           .whenBoxedLong("$T.valueOf(1)", Long.class)
           .whenLong("1L")
           .otherwise("0L")
           .build();
       
      Parameters:
      returnType - the method return type.
      Returns:
      a new ReturnStatementBuilder.