Use Assert.state() where appropriate

This commit is contained in:
Sam Brannen
2022-11-14 17:36:11 +01:00
parent 2aa78889d2
commit abf3400c07
30 changed files with 80 additions and 82 deletions

View File

@@ -42,16 +42,18 @@ public class DefaultMethodReference implements MethodReference {
@Nullable
private final ClassName declaringClass;
public DefaultMethodReference(MethodSpec method, @Nullable ClassName declaringClass) {
this.method = method;
this.declaringClass = declaringClass;
}
@Override
public CodeBlock toCodeBlock() {
String methodName = this.method.name;
if (isStatic()) {
Assert.notNull(this.declaringClass, "static method reference must define a declaring class");
Assert.state(this.declaringClass != null, "static method reference must define a declaring class");
return CodeBlock.of("$T::$L", this.declaringClass, methodName);
}
else {
@@ -59,12 +61,13 @@ public class DefaultMethodReference implements MethodReference {
}
}
@Override
public CodeBlock toInvokeCodeBlock(ArgumentCodeGenerator argumentCodeGenerator,
@Nullable ClassName targetClassName) {
String methodName = this.method.name;
CodeBlock.Builder code = CodeBlock.builder();
if (isStatic()) {
Assert.notNull(this.declaringClass, "static method reference must define a declaring class");
Assert.state(this.declaringClass != null, "static method reference must define a declaring class");
if (isSameDeclaringClass(targetClassName)) {
code.add("$L", methodName);
}

View File

@@ -30,6 +30,7 @@ import org.springframework.lang.Nullable;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* Tests for {@link DefaultMethodReference}.
@@ -86,7 +87,7 @@ class DefaultMethodReferenceTests {
void toCodeBlockWithStaticMethodRequiresDeclaringClass() {
MethodSpec method = createTestMethod("methodName", new TypeName[0], Modifier.STATIC);
MethodReference methodReference = new DefaultMethodReference(method, null);
assertThatIllegalArgumentException().isThrownBy(methodReference::toCodeBlock)
assertThatIllegalStateException().isThrownBy(methodReference::toCodeBlock)
.withMessage("static method reference must define a declaring class");
}