Allow a MethodReference to be produced from a GeneratedMethod
This commit updates GeneratedMethod and its underlying infrastructure to be able to produce a MethodReference. This simplifies the need when such a reference needs to be created manually and reuses more of what MethodReference has to offer. See gh-29005
This commit is contained in:
@@ -18,8 +18,12 @@ package org.springframework.aot.generate;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import javax.lang.model.element.Modifier;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.javapoet.ClassName;
|
||||
import org.springframework.javapoet.CodeBlock;
|
||||
import org.springframework.javapoet.MethodSpec;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -29,30 +33,55 @@ import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
* Tests for {@link GeneratedMethod}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
class GeneratedMethodTests {
|
||||
|
||||
private static final Consumer<MethodSpec.Builder> methodSpecCustomizer = method -> {};
|
||||
private static final ClassName TEST_CLASS_NAME = ClassName.get("com.example", "Test");
|
||||
|
||||
private static final Consumer<MethodSpec.Builder> emptyMethod = method -> {};
|
||||
|
||||
private static final String NAME = "spring";
|
||||
|
||||
@Test
|
||||
void getNameReturnsName() {
|
||||
GeneratedMethod generatedMethod = new GeneratedMethod(NAME, methodSpecCustomizer);
|
||||
GeneratedMethod generatedMethod = new GeneratedMethod(TEST_CLASS_NAME, NAME, emptyMethod);
|
||||
assertThat(generatedMethod.getName()).isSameAs(NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateMethodSpecReturnsMethodSpec() {
|
||||
GeneratedMethod generatedMethod = new GeneratedMethod(NAME, method -> method.addJavadoc("Test"));
|
||||
GeneratedMethod generatedMethod = create(method -> method.addJavadoc("Test"));
|
||||
assertThat(generatedMethod.getMethodSpec().javadoc).asString().contains("Test");
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateMethodSpecWhenMethodNameIsChangedThrowsException() {
|
||||
assertThatIllegalStateException().isThrownBy(() ->
|
||||
new GeneratedMethod(NAME, method -> method.setName("badname")).getMethodSpec())
|
||||
.withMessage("'method' consumer must not change the generated method name");
|
||||
create(method -> method.setName("badname")).getMethodSpec())
|
||||
.withMessage("'method' consumer must not change the generated method name");
|
||||
}
|
||||
|
||||
@Test
|
||||
void toMethodReferenceWithInstanceMethod() {
|
||||
GeneratedMethod generatedMethod = create(emptyMethod);
|
||||
MethodReference methodReference = generatedMethod.toMethodReference();
|
||||
assertThat(methodReference).isNotNull();
|
||||
assertThat(methodReference.toInvokeCodeBlock("test"))
|
||||
.isEqualTo(CodeBlock.of("test.spring()"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toMethodReferenceWithStaticMethod() {
|
||||
GeneratedMethod generatedMethod = create(method -> method.addModifiers(Modifier.STATIC));
|
||||
MethodReference methodReference = generatedMethod.toMethodReference();
|
||||
assertThat(methodReference).isNotNull();
|
||||
assertThat(methodReference.toInvokeCodeBlock())
|
||||
.isEqualTo(CodeBlock.of("com.example.Test.spring()"));
|
||||
}
|
||||
|
||||
private GeneratedMethod create(Consumer<MethodSpec.Builder> method) {
|
||||
return new GeneratedMethod(TEST_CLASS_NAME, NAME, method);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.util.function.Function;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.javapoet.ClassName;
|
||||
import org.springframework.javapoet.MethodSpec;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -32,38 +33,49 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
|
||||
* Tests for {@link GeneratedMethods}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
class GeneratedMethodsTests {
|
||||
|
||||
private static final ClassName TEST_CLASS_NAME = ClassName.get("com.example", "Test");
|
||||
|
||||
private static final Consumer<MethodSpec.Builder> methodSpecCustomizer = method -> {};
|
||||
|
||||
private final GeneratedMethods methods = new GeneratedMethods(MethodName::toString);
|
||||
private final GeneratedMethods methods = new GeneratedMethods(TEST_CLASS_NAME, MethodName::toString);
|
||||
|
||||
@Test
|
||||
void createWhenClassNameIsNullThrowsException() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
||||
new GeneratedMethods(null, MethodName::toString))
|
||||
.withMessage("'className' must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createWhenMethodNameGeneratorIsNullThrowsException() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new GeneratedMethods(null))
|
||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
||||
new GeneratedMethods(TEST_CLASS_NAME, null))
|
||||
.withMessage("'methodNameGenerator' must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createWithExistingGeneratorUsesGenerator() {
|
||||
Function<MethodName, String> generator = name -> "__" + name.toString();
|
||||
GeneratedMethods methods = new GeneratedMethods(generator);
|
||||
GeneratedMethods methods = new GeneratedMethods(TEST_CLASS_NAME, generator);
|
||||
assertThat(methods.add("test", methodSpecCustomizer).getName()).hasToString("__test");
|
||||
}
|
||||
|
||||
@Test
|
||||
void addWithStringNameWhenSuggestedMethodIsNullThrowsException() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
||||
this.methods.add((String) null, methodSpecCustomizer))
|
||||
.withMessage("'suggestedName' must not be null");
|
||||
this.methods.add((String) null, methodSpecCustomizer))
|
||||
.withMessage("'suggestedName' must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void addWithStringNameWhenMethodIsNullThrowsException() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
||||
this.methods.add("test", null))
|
||||
.withMessage("'method' must not be null");
|
||||
this.methods.add("test", null))
|
||||
.withMessage("'method' must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -71,7 +83,7 @@ class GeneratedMethodsTests {
|
||||
this.methods.add("springBeans", methodSpecCustomizer);
|
||||
this.methods.add("springContext", methodSpecCustomizer);
|
||||
assertThat(this.methods.stream().map(GeneratedMethod::getName).map(Object::toString))
|
||||
.containsExactly("springBeans", "springContext");
|
||||
.containsExactly("springBeans", "springContext");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user