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:
Stephane Nicoll
2022-08-24 07:21:02 +02:00
parent 649c2f56fd
commit 8a4a89b9d9
14 changed files with 101 additions and 43 deletions

View File

@@ -55,7 +55,7 @@ public final class GeneratedClass {
GeneratedClass(ClassName name, Consumer<TypeSpec.Builder> type) {
this.name = name;
this.type = type;
this.methods = new GeneratedMethods(this::generateSequencedMethodName);
this.methods = new GeneratedMethods(name, this::generateSequencedMethodName);
}

View File

@@ -18,6 +18,9 @@ package org.springframework.aot.generate;
import java.util.function.Consumer;
import javax.lang.model.element.Modifier;
import org.springframework.javapoet.ClassName;
import org.springframework.javapoet.MethodSpec;
import org.springframework.util.Assert;
@@ -25,11 +28,14 @@ import org.springframework.util.Assert;
* A generated method.
*
* @author Phillip Webb
* @author Stephane Nicoll
* @since 6.0
* @see GeneratedMethods
*/
public final class GeneratedMethod {
private final ClassName className;
private final String name;
private final MethodSpec methodSpec;
@@ -39,12 +45,14 @@ public final class GeneratedMethod {
* Create a new {@link GeneratedMethod} instance with the given name. This
* constructor is package-private since names should only be generated via
* {@link GeneratedMethods}.
* @param className the declaring class of the method
* @param name the generated method name
* @param method consumer to generate the method
*/
GeneratedMethod(String name, Consumer<MethodSpec.Builder> method) {
GeneratedMethod(ClassName className, String name, Consumer<MethodSpec.Builder> method) {
this.className = className;
this.name = name;
MethodSpec.Builder builder = MethodSpec.methodBuilder(getName());
MethodSpec.Builder builder = MethodSpec.methodBuilder(this.name);
method.accept(builder);
this.methodSpec = builder.build();
Assert.state(this.name.equals(this.methodSpec.name),
@@ -60,6 +68,16 @@ public final class GeneratedMethod {
return this.name;
}
/**
* Return a {@link MethodReference} to this generated method.
* @return a method reference
*/
public MethodReference toMethodReference() {
return (this.methodSpec.modifiers.contains(Modifier.STATIC)
? MethodReference.ofStatic(this.className, this.name)
: MethodReference.of(this.className, this.name));
}
/**
* Return the {@link MethodSpec} for this generated method.
* @return the method spec

View File

@@ -22,6 +22,7 @@ import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;
import org.springframework.javapoet.ClassName;
import org.springframework.javapoet.MethodSpec;
import org.springframework.javapoet.MethodSpec.Builder;
import org.springframework.util.Assert;
@@ -30,11 +31,14 @@ import org.springframework.util.Assert;
* A managed collection of generated methods.
*
* @author Phillip Webb
* @author Stephane Nicoll
* @since 6.0
* @see GeneratedMethod
*/
public class GeneratedMethods {
private final ClassName className;
private final Function<MethodName, String> methodNameGenerator;
private final MethodName prefix;
@@ -44,18 +48,22 @@ public class GeneratedMethods {
/**
* Create a new {@link GeneratedMethods} using the specified method name
* generator.
* @param className the declaring class name
* @param methodNameGenerator the method name generator
*/
GeneratedMethods(Function<MethodName, String> methodNameGenerator) {
GeneratedMethods(ClassName className, Function<MethodName, String> methodNameGenerator) {
Assert.notNull(className, "'className' must not be null");
Assert.notNull(methodNameGenerator, "'methodNameGenerator' must not be null");
this.className = className;
this.methodNameGenerator = methodNameGenerator;
this.prefix = MethodName.NONE;
this.generatedMethods = new ArrayList<>();
}
private GeneratedMethods(Function<MethodName, String> methodNameGenerator,
private GeneratedMethods(ClassName className, Function<MethodName, String> methodNameGenerator,
MethodName prefix, List<GeneratedMethod> generatedMethods) {
this.className = className;
this.methodNameGenerator = methodNameGenerator;
this.prefix = prefix;
this.generatedMethods = generatedMethods;
@@ -82,7 +90,7 @@ public class GeneratedMethods {
Assert.notNull(suggestedNameParts, "'suggestedNameParts' must not be null");
Assert.notNull(method, "'method' must not be null");
String generatedName = this.methodNameGenerator.apply(this.prefix.and(suggestedNameParts));
GeneratedMethod generatedMethod = new GeneratedMethod(generatedName, method);
GeneratedMethod generatedMethod = new GeneratedMethod(this.className, generatedName, method);
this.generatedMethods.add(generatedMethod);
return generatedMethod;
}
@@ -90,7 +98,8 @@ public class GeneratedMethods {
public GeneratedMethods withPrefix(String prefix) {
Assert.notNull(prefix, "'prefix' must not be null");
return new GeneratedMethods(this.methodNameGenerator, this.prefix.and(prefix), this.generatedMethods);
return new GeneratedMethods(this.className, this.methodNameGenerator,
this.prefix.and(prefix), this.generatedMethods);
}
/**

View File

@@ -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);
}
}

View File

@@ -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