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
This commit is contained in:
Sam Brannen 2016-07-12 16:40:19 +02:00
parent 17dd5dd22d
commit 264f5860a8
7 changed files with 29 additions and 41 deletions

View File

@ -224,10 +224,9 @@ public abstract class GenericTypeResolver {
} }
private static Class<?> getSingleGeneric(ResolvableType resolvableType) { private static Class<?> getSingleGeneric(ResolvableType resolvableType) {
if (resolvableType.getGenerics().length > 1) { Assert.isTrue(resolvableType.getGenerics().length == 1,
throw new IllegalArgumentException("Expected 1 type argument on generic interface [" + () -> "Expected 1 type argument on generic interface [" + resolvableType +
resolvableType + "] but found " + resolvableType.getGenerics().length); "] but found " + resolvableType.getGenerics().length);
}
return resolvableType.getGeneric().resolve(); return resolvableType.getGeneric().resolve();
} }

View File

@ -668,9 +668,7 @@ public class MethodParameter {
private static int validateIndex(Executable executable, int parameterIndex) { private static int validateIndex(Executable executable, int parameterIndex) {
int count = executable.getParameterCount(); int count = executable.getParameterCount();
if (parameterIndex >= count) { Assert.isTrue(parameterIndex < count, () -> "Parameter index needs to be between 0 and " + (count - 1));
throw new IllegalArgumentException("Parameter index needs to be between 0 and " + (count - 1));
}
return parameterIndex; return parameterIndex;
} }

View File

@ -1,5 +1,5 @@
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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) { private void assertAttributePresence(String attributeName, Object attributeValue) {
if (attributeValue == null) { Assert.notNull(attributeValue, () -> String.format("Attribute '%s' not found in attributes for annotation [%s]",
throw new IllegalArgumentException(String.format( attributeName, this.displayName));
"Attribute '%s' not found in attributes for annotation [%s]", attributeName, this.displayName));
}
} }
private void assertAttributePresence(String attributeName, List<String> aliases, Object attributeValue) { private void assertAttributePresence(String attributeName, List<String> aliases, Object attributeValue) {
if (attributeValue == null) { Assert.notNull(attributeValue, () -> String.format(
throw new IllegalArgumentException(String.format( "Neither attribute '%s' nor one of its aliases %s was found in attributes for annotation [%s]",
"Neither attribute '%s' nor one of its aliases %s was found in attributes for annotation [%s]", attributeName, aliases, this.displayName));
attributeName, aliases, this.displayName));
}
} }
private void assertNotException(String attributeName, Object attributeValue) { private void assertNotException(String attributeName, Object attributeValue) {

View File

@ -24,6 +24,7 @@ import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import static org.springframework.core.annotation.AnnotationUtils.*; import static org.springframework.core.annotation.AnnotationUtils.*;
@ -120,11 +121,9 @@ class MapAnnotationAttributeExtractor extends AbstractAliasAwareAnnotationAttrib
} }
// if still null // if still null
if (attributeValue == null) { Assert.notNull(attributeValue, () -> String.format(
throw new IllegalArgumentException(String.format( "Attributes map %s returned null for required attribute '%s' defined by annotation type [%s].",
"Attributes map %s returned null for required attribute '%s' defined by annotation type [%s].", attributes, attributeName, annotationType.getName()));
attributes, attributeName, annotationType.getName()));
}
// finally, ensure correct type // finally, ensure correct type
Class<?> requiredReturnType = attributeMethod.getReturnType(); Class<?> requiredReturnType = attributeMethod.getReturnType();
@ -162,13 +161,11 @@ class MapAnnotationAttributeExtractor extends AbstractAliasAwareAnnotationAttrib
converted = true; converted = true;
} }
if (!converted) { Assert.isTrue(converted, () -> String.format(
throw new IllegalArgumentException(String.format( "Attributes map %s returned a value of type [%s] for attribute '%s', " +
"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].",
"but a value of type [%s] is required as defined by annotation type [%s].", attributes, actualReturnType.getName(), attributeName, requiredReturnType.getName(),
attributes, actualReturnType.getName(), attributeName, requiredReturnType.getName(), annotationType.getName()));
annotationType.getName()));
}
} }
} }

View File

@ -20,6 +20,7 @@ import org.springframework.core.convert.ConversionFailedException;
import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter; import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.util.Assert;
/** /**
* Internal utilities for the conversion package. * Internal utilities for the conversion package.
@ -71,10 +72,7 @@ abstract class ConversionUtils {
while (enumType != null && !enumType.isEnum()) { while (enumType != null && !enumType.isEnum()) {
enumType = enumType.getSuperclass(); enumType = enumType.getSuperclass();
} }
if (enumType == null) { Assert.notNull(enumType, () -> "The target type " + targetType.getName() + " does not refer to an enum");
throw new IllegalArgumentException(
"The target type " + targetType.getName() + " does not refer to an enum");
}
return enumType; return enumType;
} }

View File

@ -1,5 +1,5 @@
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.IOException;
import java.io.InputStream; import java.io.InputStream;
import org.springframework.util.Assert;
/** /**
* {@link Resource} implementation for a given {@link InputStream}. * {@link Resource} implementation for a given {@link InputStream}.
* <p>Should only be used if no other specific {@code Resource} implementation * <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 * @param description where the InputStream comes from
*/ */
public InputStreamResource(InputStream inputStream, String description) { public InputStreamResource(InputStream inputStream, String description) {
if (inputStream == null) { Assert.notNull(inputStream, "InputStream must not be null");
throw new IllegalArgumentException("InputStream must not be null");
}
this.inputStream = inputStream; this.inputStream = inputStream;
this.description = (description != null ? description : ""); this.description = (description != null ? description : "");
} }

View File

@ -16,6 +16,8 @@
package org.springframework.util.backoff; package org.springframework.util.backoff;
import org.springframework.util.Assert;
/** /**
* Implementation of {@link BackOff} that increases the back off period for each * Implementation of {@link BackOff} that increases the back off period for each
* retry attempt. When the interval has reached the {@link #setMaxInterval(long) * 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) { private void checkMultiplier(double multiplier) {
if (multiplier < 1) { Assert.isTrue(multiplier >= 1, () -> "Invalid multiplier '" + multiplier + "'. Should be greater than " +
throw new IllegalArgumentException("Invalid multiplier '" + multiplier + "'. Should be equal" + "or equal to 1. A multiplier of 1 is equivalent to a fixed interval.");
"or higher than 1. A multiplier of 1 is equivalent to a fixed interval");
}
} }