Class LordOfTheStrings
java.lang.Object
org.springframework.data.javapoet.LordOfTheStrings
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
An extended variant ofCodeBlock.Builder
that supports building statements in a fluent way and extended for functionalstatement creation
.static class
Builder to create method invocation code supporting argument concatenation.static class
Builder for constructing return statements based on the target return type.static class
Builder for creating statements including conditional and concatenated variants.static class
Builder for constructing return statements based conditionally on the target return type. -
Method Summary
Modifier and TypeMethodDescriptionbuilder()
Create a newCodeBlockBuilder
instance.Create a newCodeBlockBuilder
instance with an initial format and arguments.builder
(org.springframework.javapoet.CodeBlock.Builder builder) Create a newCodeBlockBuilder
instance using the givenCodeBlock.Builder
.Create aLordOfTheStrings.InvocationBuilder
for building method invocation code.Create a builder for a return statement targeting the given return type.returning
(ResolvableType returnType) Create a builder for a return statement targeting the given return type.
-
Method Details
-
builder
Create a newCodeBlockBuilder
instance.- Returns:
- a new
CodeBlockBuilder
.
-
builder
public static LordOfTheStrings.CodeBlockBuilder builder(org.springframework.javapoet.CodeBlock.Builder builder) Create a newCodeBlockBuilder
instance using the givenCodeBlock.Builder
.- Parameters:
builder
- theCodeBlock.Builder
to use.- Returns:
- a new
CodeBlockBuilder
.
-
builder
Create a newCodeBlockBuilder
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
Create aLordOfTheStrings.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 anyLordOfTheStrings.InvocationBuilder.argument(java.lang.String)
orLordOfTheStrings.InvocationBuilder.arguments
method. Providing an emptyCodeBlock
orIterable
activates argument processing for easier handling when calling this static method. Note that the argument placeholder inmethodCall
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
Create a builder for a return statement targeting the given return type. Any formats provided toLordOfTheStrings.ReturnBuilderSupport
must not contain thereturn
keyword as this will be included when building the resultingCodeBlock
, 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
Create a builder for a return statement targeting the given return type. Any formats provided toLordOfTheStrings.ReturnBuilderSupport
must not contain thereturn
keyword as this will be included when building the resultingCodeBlock
, 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
.
-