This commit is contained in:
Phillip Webb
2018-02-01 10:24:53 -08:00
parent 00d9dbe4ce
commit 7d5e41f7dc
12 changed files with 117 additions and 85 deletions

View File

@@ -37,9 +37,9 @@ public interface OperationParameter {
Class<?> getType();
/**
* Return if the parameter accepts null values.
* @return if the parameter is nullable
* Return if the parameter is mandatory (does not accept null values).
* @return if the parameter is mandatory
*/
boolean isNullable();
boolean isMandatory();
}

View File

@@ -40,6 +40,15 @@ public interface OperationParameters extends Iterable<OperationParameter> {
*/
int getParameterCount();
/**
* Return if any of the contained parameters are
* {@link OperationParameter#isMandatory() mandatory}.
* @return if any parameters are mandatory
*/
default boolean hasMandatoryParameter() {
return stream().anyMatch(OperationParameter::isMandatory);
}
/**
* Return the parameter at the specified index.
* @param index the parameter index

View File

@@ -54,8 +54,8 @@ class OperationMethodParameter implements OperationParameter {
}
@Override
public boolean isNullable() {
return !ObjectUtils.isEmpty(this.parameter.getAnnotationsByType(Nullable.class));
public boolean isMandatory() {
return ObjectUtils.isEmpty(this.parameter.getAnnotationsByType(Nullable.class));
}
@Override

View File

@@ -85,8 +85,8 @@ public class ReflectiveOperationInvoker implements OperationInvoker {
private boolean isMissing(Map<String, Object> arguments,
OperationParameter parameter) {
if (parameter.isNullable()) {
return false;
if (!parameter.isMandatory()) {
return true;
}
return arguments.get(parameter.getName()) == null;
}

View File

@@ -17,6 +17,7 @@
package org.springframework.boot.actuate.endpoint.invoker.cache;
import java.util.Map;
import java.util.Objects;
import org.springframework.boot.actuate.endpoint.invoke.OperationInvoker;
import org.springframework.util.Assert;
@@ -74,11 +75,7 @@ public class CachingOperationInvoker implements OperationInvoker {
private boolean hasArgument(Map<String, Object> arguments) {
if (!ObjectUtils.isEmpty(arguments)) {
for (Object value : arguments.values()) {
if (value != null) {
return true;
}
}
return arguments.values().stream().anyMatch(Objects::nonNull);
}
return false;
}

View File

@@ -21,7 +21,6 @@ import java.util.function.Function;
import org.springframework.boot.actuate.endpoint.OperationType;
import org.springframework.boot.actuate.endpoint.invoke.OperationInvoker;
import org.springframework.boot.actuate.endpoint.invoke.OperationInvokerAdvisor;
import org.springframework.boot.actuate.endpoint.invoke.OperationParameter;
import org.springframework.boot.actuate.endpoint.invoke.OperationParameters;
/**
@@ -41,7 +40,7 @@ public class CachingOperationInvokerAdvisor implements OperationInvokerAdvisor {
@Override
public OperationInvoker apply(String endpointId, OperationType operationType,
OperationParameters parameters, OperationInvoker invoker) {
if (operationType == OperationType.READ && !hasMandatoryParameter(parameters)) {
if (operationType == OperationType.READ && !parameters.hasMandatoryParameter()) {
Long timeToLive = this.endpointIdTimeToLive.apply(endpointId);
if (timeToLive != null && timeToLive > 0) {
return new CachingOperationInvoker(invoker, timeToLive);
@@ -50,13 +49,4 @@ public class CachingOperationInvokerAdvisor implements OperationInvokerAdvisor {
return invoker;
}
private boolean hasMandatoryParameter(OperationParameters parameters) {
for (OperationParameter parameter : parameters) {
if (!parameter.isNullable()) {
return true;
}
}
return false;
}
}

View File

@@ -50,17 +50,17 @@ public class OperationMethodParameterTests {
}
@Test
public void isNullableWhenNoAnnotationShouldReturnFalse() {
public void isMandatoryWhenNoAnnotationShouldReturnTrue() {
OperationMethodParameter parameter = new OperationMethodParameter("name",
this.method.getParameters()[0]);
assertThat(parameter.isNullable()).isFalse();
assertThat(parameter.isMandatory()).isTrue();
}
@Test
public void isNullableWhenNullableAnnotationShouldReturnTrue() {
public void isMandatoryWhenNullableAnnotationShouldReturnFalse() {
OperationMethodParameter parameter = new OperationMethodParameter("name",
this.method.getParameters()[1]);
assertThat(parameter.isNullable()).isTrue();
assertThat(parameter.isMandatory()).isFalse();
}
void example(String one, @Nullable String two) {