提交 264f5860 编写于 作者: S Sam Brannen

Use Supplier<String> support in Assert in spring-core

This commit makes use of the new Supplier<String> variants of utility
methods in org.springframework.util.Assert within the spring-core
module.

Issue: SPR-14450
上级 17dd5dd2
......@@ -224,10 +224,9 @@ public abstract class GenericTypeResolver {
}
private static Class<?> getSingleGeneric(ResolvableType resolvableType) {
if (resolvableType.getGenerics().length > 1) {
throw new IllegalArgumentException("Expected 1 type argument on generic interface [" +
resolvableType + "] but found " + resolvableType.getGenerics().length);
}
Assert.isTrue(resolvableType.getGenerics().length == 1,
() -> "Expected 1 type argument on generic interface [" + resolvableType +
"] but found " + resolvableType.getGenerics().length);
return resolvableType.getGeneric().resolve();
}
......
......@@ -668,9 +668,7 @@ public class MethodParameter {
private static int validateIndex(Executable executable, int parameterIndex) {
int count = executable.getParameterCount();
if (parameterIndex >= count) {
throw new IllegalArgumentException("Parameter index needs to be between 0 and " + (count - 1));
}
Assert.isTrue(parameterIndex < count, () -> "Parameter index needs to be between 0 and " + (count - 1));
return parameterIndex;
}
......
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
......@@ -480,18 +480,14 @@ public class AnnotationAttributes extends LinkedHashMap<String, Object> {
}
private void assertAttributePresence(String attributeName, Object attributeValue) {
if (attributeValue == null) {
throw new IllegalArgumentException(String.format(
"Attribute '%s' not found in attributes for annotation [%s]", attributeName, this.displayName));
}
Assert.notNull(attributeValue, () -> String.format("Attribute '%s' not found in attributes for annotation [%s]",
attributeName, this.displayName));
}
private void assertAttributePresence(String attributeName, List<String> aliases, Object attributeValue) {
if (attributeValue == null) {
throw new IllegalArgumentException(String.format(
"Neither attribute '%s' nor one of its aliases %s was found in attributes for annotation [%s]",
attributeName, aliases, this.displayName));
}
Assert.notNull(attributeValue, () -> String.format(
"Neither attribute '%s' nor one of its aliases %s was found in attributes for annotation [%s]",
attributeName, aliases, this.displayName));
}
private void assertNotException(String attributeName, Object attributeValue) {
......
......@@ -24,6 +24,7 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import static org.springframework.core.annotation.AnnotationUtils.*;
......@@ -120,11 +121,9 @@ class MapAnnotationAttributeExtractor extends AbstractAliasAwareAnnotationAttrib
}
// if still null
if (attributeValue == null) {
throw new IllegalArgumentException(String.format(
"Attributes map %s returned null for required attribute '%s' defined by annotation type [%s].",
attributes, attributeName, annotationType.getName()));
}
Assert.notNull(attributeValue, () -> String.format(
"Attributes map %s returned null for required attribute '%s' defined by annotation type [%s].",
attributes, attributeName, annotationType.getName()));
// finally, ensure correct type
Class<?> requiredReturnType = attributeMethod.getReturnType();
......@@ -162,13 +161,11 @@ class MapAnnotationAttributeExtractor extends AbstractAliasAwareAnnotationAttrib
converted = true;
}
if (!converted) {
throw new IllegalArgumentException(String.format(
"Attributes map %s returned a value of type [%s] for attribute '%s', " +
"but a value of type [%s] is required as defined by annotation type [%s].",
attributes, actualReturnType.getName(), attributeName, requiredReturnType.getName(),
annotationType.getName()));
}
Assert.isTrue(converted, () -> String.format(
"Attributes map %s returned a value of type [%s] for attribute '%s', " +
"but a value of type [%s] is required as defined by annotation type [%s].",
attributes, actualReturnType.getName(), attributeName, requiredReturnType.getName(),
annotationType.getName()));
}
}
......
......@@ -20,6 +20,7 @@ import org.springframework.core.convert.ConversionFailedException;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.util.Assert;
/**
* Internal utilities for the conversion package.
......@@ -71,10 +72,7 @@ abstract class ConversionUtils {
while (enumType != null && !enumType.isEnum()) {
enumType = enumType.getSuperclass();
}
if (enumType == null) {
throw new IllegalArgumentException(
"The target type " + targetType.getName() + " does not refer to an enum");
}
Assert.notNull(enumType, () -> "The target type " + targetType.getName() + " does not refer to an enum");
return enumType;
}
......
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
......@@ -19,6 +19,8 @@ package org.springframework.core.io;
import java.io.IOException;
import java.io.InputStream;
import org.springframework.util.Assert;
/**
* {@link Resource} implementation for a given {@link InputStream}.
* <p>Should only be used if no other specific {@code Resource} implementation
......@@ -62,9 +64,7 @@ public class InputStreamResource extends AbstractResource {
* @param description where the InputStream comes from
*/
public InputStreamResource(InputStream inputStream, String description) {
if (inputStream == null) {
throw new IllegalArgumentException("InputStream must not be null");
}
Assert.notNull(inputStream, "InputStream must not be null");
this.inputStream = inputStream;
this.description = (description != null ? description : "");
}
......
......@@ -16,6 +16,8 @@
package org.springframework.util.backoff;
import org.springframework.util.Assert;
/**
* Implementation of {@link BackOff} that increases the back off period for each
* retry attempt. When the interval has reached the {@link #setMaxInterval(long)
......@@ -169,10 +171,8 @@ public class ExponentialBackOff implements BackOff {
}
private void checkMultiplier(double multiplier) {
if (multiplier < 1) {
throw new IllegalArgumentException("Invalid multiplier '" + multiplier + "'. Should be equal" +
"or higher than 1. A multiplier of 1 is equivalent to a fixed interval");
}
Assert.isTrue(multiplier >= 1, () -> "Invalid multiplier '" + multiplier + "'. Should be greater than " +
"or equal to 1. A multiplier of 1 is equivalent to a fixed interval.");
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册