From 264f5860a86b14d2e343efa81bcbfb95474b68dd Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Tue, 12 Jul 2016 16:40:19 +0200 Subject: [PATCH] Use Supplier support in Assert in spring-core This commit makes use of the new Supplier variants of utility methods in org.springframework.util.Assert within the spring-core module. Issue: SPR-14450 --- .../core/GenericTypeResolver.java | 7 +++---- .../springframework/core/MethodParameter.java | 4 +--- .../core/annotation/AnnotationAttributes.java | 16 ++++++-------- .../MapAnnotationAttributeExtractor.java | 21 ++++++++----------- .../core/convert/support/ConversionUtils.java | 6 ++---- .../core/io/InputStreamResource.java | 8 +++---- .../util/backoff/ExponentialBackOff.java | 8 +++---- 7 files changed, 29 insertions(+), 41 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/GenericTypeResolver.java b/spring-core/src/main/java/org/springframework/core/GenericTypeResolver.java index acd2191398..c76c911094 100644 --- a/spring-core/src/main/java/org/springframework/core/GenericTypeResolver.java +++ b/spring-core/src/main/java/org/springframework/core/GenericTypeResolver.java @@ -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(); } diff --git a/spring-core/src/main/java/org/springframework/core/MethodParameter.java b/spring-core/src/main/java/org/springframework/core/MethodParameter.java index 150f8cfeb8..9b09d8f685 100644 --- a/spring-core/src/main/java/org/springframework/core/MethodParameter.java +++ b/spring-core/src/main/java/org/springframework/core/MethodParameter.java @@ -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; } diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationAttributes.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationAttributes.java index 395d2dba30..61f002357a 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationAttributes.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationAttributes.java @@ -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"); * you may not use this file except in compliance with the License. @@ -480,18 +480,14 @@ public class AnnotationAttributes extends LinkedHashMap { } 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 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) { diff --git a/spring-core/src/main/java/org/springframework/core/annotation/MapAnnotationAttributeExtractor.java b/spring-core/src/main/java/org/springframework/core/annotation/MapAnnotationAttributeExtractor.java index 2c2606a058..7789c36c5f 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/MapAnnotationAttributeExtractor.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/MapAnnotationAttributeExtractor.java @@ -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())); } } diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/ConversionUtils.java b/spring-core/src/main/java/org/springframework/core/convert/support/ConversionUtils.java index 1806afd218..4082dc575e 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/support/ConversionUtils.java +++ b/spring-core/src/main/java/org/springframework/core/convert/support/ConversionUtils.java @@ -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; } diff --git a/spring-core/src/main/java/org/springframework/core/io/InputStreamResource.java b/spring-core/src/main/java/org/springframework/core/io/InputStreamResource.java index b2587f99df..593331ee48 100644 --- a/spring-core/src/main/java/org/springframework/core/io/InputStreamResource.java +++ b/spring-core/src/main/java/org/springframework/core/io/InputStreamResource.java @@ -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"); * 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}. *

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 : ""); } diff --git a/spring-core/src/main/java/org/springframework/util/backoff/ExponentialBackOff.java b/spring-core/src/main/java/org/springframework/util/backoff/ExponentialBackOff.java index b7c6efd29c..d0cbe277a6 100644 --- a/spring-core/src/main/java/org/springframework/util/backoff/ExponentialBackOff.java +++ b/spring-core/src/main/java/org/springframework/util/backoff/ExponentialBackOff.java @@ -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."); }