Clarify lambda code block methods

This commit is contained in:
Stephane Nicoll 2022-04-26 15:13:43 +02:00
parent f64fc4baff
commit 5378572b00
5 changed files with 15 additions and 15 deletions

View File

@ -92,7 +92,7 @@ class ScopedProxyBeanRegistrationContributionProvider implements BeanRegistratio
statements.addStatement("factory.setTargetBeanName($S)", targetBeanName);
statements.addStatement("factory.setBeanFactory(beanFactory)");
statements.addStatement("return factory.getObject()");
codeContribution.statements().add(statements.toLambdaBody("() ->"));
codeContribution.statements().add(statements.toLambda("() ->"));
return codeContribution;
}
};

View File

@ -421,7 +421,7 @@ public class BeanRegistrationBeanFactoryContribution implements BeanFactoryContr
if (statements.isEmpty()) {
return;
}
code.add(statements.toLambdaBody(".customize((" + bdVariable + ") ->"));
code.add(statements.toLambda(".customize((" + bdVariable + ") ->"));
code.add(")");
}

View File

@ -234,7 +234,7 @@ public class InjectionGenerator {
code.add(".resolve(beanFactory, false).ifResolved(");
}
code.add(this.fieldGenerator.generateSetValue("bean", injectionPoint,
CodeBlock.of("attributes.get(0)")).toLambdaBody("(attributes) ->"));
CodeBlock.of("attributes.get(0)")).toLambda("(attributes) ->"));
code.add(")").unindent().unindent();
return code.build();
}

View File

@ -141,7 +141,7 @@ public final class MultiStatement {
* Return a {@link CodeBlock} that applies all the {@code statements} of this
* instance. If only one statement is available, it is not completed using the
* {@code ;} termination so that it can be used in the context of a lambda.
* @return the statement(s)
* @return the body of the lambda
*/
public CodeBlock toLambdaBody() {
Builder code = CodeBlock.builder();
@ -155,12 +155,12 @@ public final class MultiStatement {
/**
* Return a {@link CodeBlock} that applies all the {@code statements} of this
* instance in the context of a lambda.
* @param lambda the context of the lambda, must end with {@code ->}
* @return the lambda body
* @param lambdaParameter the parameter(s) of the lambda, must end with {@code ->}
* @return a lambda whose body is generated from the statements of this instance
*/
public CodeBlock toLambdaBody(CodeBlock lambda) {
public CodeBlock toLambda(CodeBlock lambdaParameter) {
Builder code = CodeBlock.builder();
code.add(lambda);
code.add(lambdaParameter);
if (isMulti()) {
code.beginControlFlow("");
}
@ -177,11 +177,11 @@ public final class MultiStatement {
/**
* Return a {@link CodeBlock} that applies all the {@code statements} of this
* instance in the context of a lambda.
* @param lambda the context of the lambda, must end with {@code ->}
* @return the lambda body
* @param lambdaParameter the parameter(s) of the lambda, must end with {@code ->}
* @return a lambda whose body is generated from the statements of this instance
*/
public CodeBlock toLambdaBody(String lambda) {
return toLambdaBody(CodeBlock.of(lambda));
public CodeBlock toLambda(String lambdaParameter) {
return toLambda(CodeBlock.of(lambdaParameter));
}
private boolean isMulti() {

View File

@ -119,7 +119,7 @@ class MultiStatementTests {
void singleStatementWithLambda() {
MultiStatement statements = new MultiStatement();
statements.addStatement("field.method($S)", "hello");
CodeBlock codeBlock = statements.toLambdaBody(CodeBlock.of("() ->"));
CodeBlock codeBlock = statements.toLambda(CodeBlock.of("() ->"));
assertThat(codeBlock.toString()).isEqualTo("() -> field.method(\"hello\")");
}
@ -128,7 +128,7 @@ class MultiStatementTests {
MultiStatement statements = new MultiStatement();
statements.addStatement("field.method($S)", "hello");
statements.addStatement("field.anotherMethod($S)", "hello");
CodeBlock codeBlock = statements.toLambdaBody(CodeBlock.of("() ->"));
CodeBlock codeBlock = statements.toLambda(CodeBlock.of("() ->"));
assertThat(codeBlock.toString().lines()).containsExactly(
"() -> {",
" field.method(\"hello\");",
@ -141,7 +141,7 @@ class MultiStatementTests {
MultiStatement statements = new MultiStatement();
statements.addAll(List.of(0, 1, 2),
index -> CodeBlock.of("field[$L] = $S", index, "hello"));
CodeBlock codeBlock = statements.toLambdaBody("() ->");
CodeBlock codeBlock = statements.toLambda("() ->");
assertThat(codeBlock.toString().lines()).containsExactly(
"() -> {",
" field[0] = \"hello\";",