Introduce InvocationArguments type to capture the arguments passed to a method invocation's parameters.

This commit is contained in:
John Blum
2020-04-26 15:30:42 -07:00
parent b1e9a30e10
commit 6f0b2ecf84
2 changed files with 181 additions and 0 deletions

View File

@@ -0,0 +1,84 @@
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package org.springframework.geode.util.function;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Iterator;
/**
* {@link Iterable} of {@link Method} invocation {@link Object arguments}.
*
* @author John Blum
* @see java.lang.Iterable
* @since 1.3.0
*/
public class InvocationArguments implements Iterable<Object> {
public static InvocationArguments from(Object... arguments) {
return new InvocationArguments(arguments);
}
private final Object[] arguments;
/**
* Constructs a new instance of {@link InvocationArguments} initialized with the given array of
* {@link Object arguments}.
*
* @param arguments array of {@link Object arguments} indicating the values passed to the {@link Method} invocation
* parameters; may be {@literal null}.
*/
public InvocationArguments(Object[] arguments) {
this.arguments = arguments != null ? arguments : new Object[0];
}
protected Object[] getArguments() {
return this.arguments;
}
@SuppressWarnings("unchecked")
protected <T> T getArgumentAt(int index) {
return (T) getArguments()[index];
}
@Override
public Iterator<Object> iterator() {
return new Iterator<Object>() {
int index = 0;
@Override
public boolean hasNext() {
return this.index < InvocationArguments.this.getArguments().length;
}
@Override
public Object next() {
return InvocationArguments.this.getArguments()[this.index++];
}
};
}
public int size() {
return this.arguments.length;
}
@Override
public String toString() {
return Arrays.toString(getArguments());
}
}

View File

@@ -0,0 +1,97 @@
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package org.springframework.geode.util.function;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import org.junit.Test;
/**
* Unit Tests for {@link InvocationArguments).
*
* @author John Blum
* @see org.junit.Test
* @see org.springframework.geode.util.function.InvocationArguments
* @since 1.3.0
*/
public class InvocationArgumentsUnitTests {
@Test
public void constructsInvocationArgumentsWithArguments() {
Object[] arguments = { true, 'c', 1, Math.PI, "test" };
InvocationArguments invocationArguments = new InvocationArguments(arguments);
assertThat(invocationArguments).isNotNull();
assertThat(invocationArguments.size()).isEqualTo(arguments.length);
assertThat(Arrays.equals(invocationArguments.getArguments(), arguments)).isTrue();
assertThat(invocationArguments.<Boolean>getArgumentAt(0)).isEqualTo(true);
assertThat(invocationArguments.<Character>getArgumentAt(1)).isEqualTo('c');
assertThat(invocationArguments.<Integer>getArgumentAt(2)).isEqualTo(1);
assertThat(invocationArguments.<Double>getArgumentAt(3)).isEqualTo(Math.PI);
assertThat(invocationArguments.<String>getArgumentAt(4)).isEqualTo("test");
}
@Test
public void fromConstructsNewInvocationArguments() {
InvocationArguments arguments = InvocationArguments.from("test", 1, false);
assertThat(arguments).isNotNull();
assertThat(arguments).hasSize(3);
assertThat(arguments.size()).isEqualTo(3);
assertThat(arguments).containsExactly("test", 1, false);
assertThat(arguments.<String>getArgumentAt(0)).isEqualTo("test");
assertThat(arguments.<Integer>getArgumentAt(1)).isEqualTo(1);
assertThat(arguments.<Boolean>getArgumentAt(2)).isEqualTo(false);
}
@Test
public void constructInvocationArgumentsWithNull() {
InvocationArguments arguments = new InvocationArguments(null);
assertThat(arguments).isNotNull();
assertThat(arguments).hasSize(0);
assertThat(arguments.getArguments()).isNotNull();
}
@Test
public void iteratesArguments() {
Object[] arguments = { true, 1, "test" };
InvocationArguments invocationArguments = new InvocationArguments(arguments);
int index = 0;
for (Object argument : invocationArguments) {
assertThat(argument).isEqualTo(arguments[index++]);
}
}
@Test
public void toStringIsCorrect() {
InvocationArguments arguments = InvocationArguments.from("one", "two", "three");
assertThat(arguments).isNotNull();
assertThat(arguments.toString()).isEqualTo("[one, two, three]");
}
}