Deprecate mutable methods of MethodParameter

Deprecate all mutation methods in `MethodParameter` in favor of factory
methods that return a new instance. Existing code that previously relied
on mutation has been updated to use the replacement methods.

Closes gh-23385
This commit is contained in:
Phillip Webb
2019-07-30 15:22:05 +01:00
parent 89d150d398
commit 62a3e41473
7 changed files with 189 additions and 70 deletions

View File

@@ -32,9 +32,12 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* Tests for {@link MethodParameter}.
*
* @author Arjen Poutsma
* @author Juergen Hoeller
* @author Sam Brannen
* @author Phillip Webb
*/
public class MethodParameterTests {
@@ -151,6 +154,7 @@ public class MethodParameterTests {
}
@Test
@Deprecated
public void multipleResolveParameterTypeCalls() throws Exception {
Method method = ArrayList.class.getMethod("get", int.class);
MethodParameter methodParameter = MethodParameter.forExecutable(method, -1);
@@ -174,17 +178,63 @@ public class MethodParameterTests {
@Test
public void equalsAndHashCodeConsidersNesting() throws Exception {
Method method = ArrayList.class.getMethod("get", int.class);
MethodParameter m1 = MethodParameter.forExecutable(method, -1);
GenericTypeResolver.resolveParameterType(m1, StringList.class);
MethodParameter m2 = MethodParameter.forExecutable(method, -1);
GenericTypeResolver.resolveParameterType(m2, StringList.class);
MethodParameter m3 = MethodParameter.forExecutable(method, -1);
GenericTypeResolver.resolveParameterType(m3, IntegerList.class);
MethodParameter m1 = MethodParameter.forExecutable(method, -1)
.withContainingClass(StringList.class);
MethodParameter m2 = MethodParameter.forExecutable(method, -1)
.withContainingClass(StringList.class);
MethodParameter m3 = MethodParameter.forExecutable(method, -1)
.withContainingClass(IntegerList.class);
MethodParameter m4 = MethodParameter.forExecutable(method, -1);
assertThat(m1).isEqualTo(m2).isNotEqualTo(m3).isNotEqualTo(m4);
assertThat(m1.hashCode()).isEqualTo(m2.hashCode());
}
public void withContainingClassReturnsNewInstance() throws Exception {
Method method = ArrayList.class.getMethod("get", int.class);
MethodParameter m1 = MethodParameter.forExecutable(method, -1);
MethodParameter m2 = m1.withContainingClass(StringList.class);
MethodParameter m3 = m1.withContainingClass(IntegerList.class);
assertThat(m1).isNotSameAs(m2).isNotSameAs(m3);
assertThat(m1.getParameterType()).isEqualTo(Object.class);
assertThat(m2.getParameterType()).isEqualTo(String.class);
assertThat(m3.getParameterType()).isEqualTo(Integer.class);
}
@Test
public void withTypeIndexReturnsNewInstance() throws Exception {
Method method = ArrayList.class.getMethod("get", int.class);
MethodParameter m1 = MethodParameter.forExecutable(method, -1);
MethodParameter m2 = m1.withTypeIndex(2);
MethodParameter m3 = m1.withTypeIndex(3);
assertThat(m1).isNotSameAs(m2).isNotSameAs(m3);
assertThat(m1.getTypeIndexForCurrentLevel()).isNull();
assertThat(m2.getTypeIndexForCurrentLevel()).isEqualTo(2);
assertThat(m3.getTypeIndexForCurrentLevel()).isEqualTo(3);
}
@Test
@SuppressWarnings("deprecation")
public void mutatingNestingLevelShouldNotChangeNewInstance() throws Exception {
Method method = ArrayList.class.getMethod("get", int.class);
MethodParameter m1 = MethodParameter.forExecutable(method, -1);
MethodParameter m2 = m1.withTypeIndex(2);
assertThat(m2.getTypeIndexForCurrentLevel()).isEqualTo(2);
m1.setTypeIndexForCurrentLevel(1);
m2.decreaseNestingLevel();
assertThat(m2.getTypeIndexForCurrentLevel()).isNull();
}
@Test
public void nestedWithTypeIndexReturnsNewInstance() throws Exception {
Method method = ArrayList.class.getMethod("get", int.class);
MethodParameter m1 = MethodParameter.forExecutable(method, -1);
MethodParameter m2 = m1.nested(2);
MethodParameter m3 = m1.nested(3);
assertThat(m1).isNotSameAs(m2).isNotSameAs(m3);
assertThat(m1.getTypeIndexForCurrentLevel()).isNull();
assertThat(m2.getTypeIndexForCurrentLevel()).isEqualTo(2);
assertThat(m3.getTypeIndexForCurrentLevel()).isEqualTo(3);
}
public int method(String p1, long p2) {
return 42;