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 classAn extended variant ofCodeBlock.Builderthat supports building statements in a fluent way and extended for functionalstatement creation.static classBuilder to create method invocation code supporting argument concatenation.static classBuilder for constructing return statements based on the target return type.static classBuilder for creating statements including conditional and concatenated variants.static classBuilder for constructing return statements based conditionally on the target return type. -
Method Summary
Modifier and TypeMethodDescriptionbuilder()Create a newCodeBlockBuilderinstance.Create a newCodeBlockBuilderinstance with an initial format and arguments.builder(org.springframework.javapoet.CodeBlock.Builder builder) Create a newCodeBlockBuilderinstance using the givenCodeBlock.Builder.Create aLordOfTheStrings.InvocationBuilderfor 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 newCodeBlockBuilderinstance.- Returns:
- a new
CodeBlockBuilder.
-
builder
public static LordOfTheStrings.CodeBlockBuilder builder(org.springframework.javapoet.CodeBlock.Builder builder) Create a newCodeBlockBuilderinstance using the givenCodeBlock.Builder.- Parameters:
builder- theCodeBlock.Builderto use.- Returns:
- a new
CodeBlockBuilder.
-
builder
Create a newCodeBlockBuilderinstance 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.InvocationBuilderfor building method invocation code.The given
methodCallmay 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.argumentsmethod. Providing an emptyCodeBlockorIterableactivates argument processing for easier handling when calling this static method. Note that the argument placeholder inmethodCallmust 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.ReturnBuilderSupportmust not contain thereturnkeyword 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.ReturnBuilderSupportmust not contain thereturnkeyword 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.
-