Revisit Assert to avoid single-arg assert methods (with refined messages)

Issue: SPR-15196
This commit is contained in:
Juergen Hoeller
2017-01-30 22:15:53 +01:00
parent 768802fa96
commit 1b2dc3638f
118 changed files with 708 additions and 828 deletions

View File

@@ -59,7 +59,7 @@ public class Constants {
* @throws IllegalArgumentException if the supplied {@code clazz} is {@code null}
*/
public Constants(Class<?> clazz) {
Assert.notNull(clazz);
Assert.notNull(clazz, "Class must not be null");
this.className = clazz.getName();
Field[] fields = clazz.getFields();
for (Field field : fields) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2017 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.
@@ -47,9 +47,9 @@ public abstract class ParameterizedTypeReference<T> {
protected ParameterizedTypeReference() {
Class<?> parameterizedTypeReferenceSubclass = findParameterizedTypeReferenceSubclass(getClass());
Type type = parameterizedTypeReferenceSubclass.getGenericSuperclass();
Assert.isInstanceOf(ParameterizedType.class, type);
Assert.isInstanceOf(ParameterizedType.class, type, "Type must be a parameterized type");
ParameterizedType parameterizedType = (ParameterizedType) type;
Assert.isTrue(parameterizedType.getActualTypeArguments().length == 1);
Assert.isTrue(parameterizedType.getActualTypeArguments().length == 1, "Number of type arguments must be 1");
this.type = parameterizedType.getActualTypeArguments()[0];
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -408,7 +408,10 @@ abstract class SerializableTypeWrapper {
private void readObject(ObjectInputStream inputStream) throws IOException, ClassNotFoundException {
inputStream.defaultReadObject();
this.method = ReflectionUtils.findMethod(this.declaringClass, this.methodName);
Assert.state(Type.class == this.method.getReturnType() || Type[].class == this.method.getReturnType());
if (this.method.getReturnType() != Type.class && this.method.getReturnType() != Type[].class) {
throw new IllegalStateException(
"Invalid return type on deserialized method - needs to be Type or Type[]: " + this.method);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -23,15 +23,15 @@ import java.util.List;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import org.springframework.util.Assert;
/**
* {@link CommandLinePropertySource} implementation backed by a JOpt {@link OptionSet}.
*
* <h2>Typical usage</h2>
*
* Configure and execute an {@code OptionParser} against the {@code String[]} of arguments
* supplied to the {@code main} method, and create a {@link JOptCommandLinePropertySource}
* using the resulting {@code OptionSet} object:
*
* <pre class="code">
* public static void main(String[] args) {
* OptionParser parser = new OptionParser();
@@ -44,7 +44,7 @@ import org.springframework.util.Assert;
*
* See {@link CommandLinePropertySource} for complete general usage examples.
*
* <p>Requires JOpt version 4.3 or higher. Tested against JOpt up until 4.6.
* <p>Requires JOpt Simple version 4.3 or higher. Tested against JOpt up until 5.0.
*
* @author Chris Beams
* @author Juergen Hoeller
@@ -98,7 +98,7 @@ public class JOptCommandLinePropertySource extends CommandLinePropertySource<Opt
List<?> argValues = this.source.valuesOf(name);
List<String> stringArgValues = new ArrayList<>();
for (Object argValue : argValues) {
stringArgValues.add(argValue instanceof String ? (String) argValue : argValue.toString());
stringArgValues.add(argValue.toString());
}
if (stringArgValues.isEmpty()) {
return (this.source.has(name) ? Collections.emptyList() : null);
@@ -111,8 +111,7 @@ public class JOptCommandLinePropertySource extends CommandLinePropertySource<Opt
List<?> argValues = this.source.nonOptionArguments();
List<String> stringArgValues = new ArrayList<>();
for (Object argValue : argValues) {
Assert.isInstanceOf(String.class, argValue, "Argument values must be of type String");
stringArgValues.add((String) argValue);
stringArgValues.add(argValue.toString());
}
return (stringArgValues.isEmpty() ? Collections.emptyList() :
Collections.unmodifiableList(stringArgValues));

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -49,9 +49,9 @@ import java.util.function.Supplier;
*
* @author Keith Donald
* @author Juergen Hoeller
* @author Sam Brannen
* @author Colin Sampaleanu
* @author Rob Harrop
* @author Sam Brannen
* @since 1.1.2
*/
public abstract class Assert {
@@ -66,6 +66,7 @@ public abstract class Assert {
* @param messageSupplier a supplier for the exception message to use if the
* assertion fails
* @throws IllegalArgumentException if {@code expression} is {@code false}
* @since 5.0
*/
public static void isTrue(boolean expression, Supplier<String> messageSupplier) {
if (!expression) {
@@ -87,17 +88,6 @@ public abstract class Assert {
}
}
/**
* Assert a boolean expression, throwing an {@code IllegalArgumentException}
* if the expression evaluates to {@code false}.
* <pre class="code">Assert.isTrue(i &gt; 0);</pre>
* @param expression a boolean expression
* @throws IllegalArgumentException if {@code expression} is {@code false}
*/
public static void isTrue(boolean expression) {
isTrue(expression, "[Assertion failed] - this expression must be true");
}
/**
* Assert that an object is {@code null}.
* <pre class="code">
@@ -107,6 +97,7 @@ public abstract class Assert {
* @param messageSupplier a supplier for the exception message to use if the
* assertion fails
* @throws IllegalArgumentException if the object is not {@code null}
* @since 5.0
*/
public static void isNull(Object object, Supplier<String> messageSupplier) {
if (object != null) {
@@ -127,16 +118,6 @@ public abstract class Assert {
}
}
/**
* Assert that an object is {@code null}.
* <pre class="code">Assert.isNull(value);</pre>
* @param object the object to check
* @throws IllegalArgumentException if the object is not {@code null}
*/
public static void isNull(Object object) {
isNull(object, "[Assertion failed] - the object argument must be null");
}
/**
* Assert that an object is not {@code null}.
* <pre class="code">
@@ -146,6 +127,7 @@ public abstract class Assert {
* @param messageSupplier a supplier for the exception message to use if the
* assertion fails
* @throws IllegalArgumentException if the object is {@code null}
* @since 5.0
*/
public static void notNull(Object object, Supplier<String> messageSupplier) {
if (object == null) {
@@ -166,16 +148,6 @@ public abstract class Assert {
}
}
/**
* Assert that an object is not {@code null}.
* <pre class="code">Assert.notNull(clazz);</pre>
* @param object the object to check
* @throws IllegalArgumentException if the object is {@code null}
*/
public static void notNull(Object object) {
notNull(object, "[Assertion failed] - this argument is required; it must not be null");
}
/**
* Assert that the given String is not empty; that is,
* it must not be {@code null} and not the empty String.
@@ -187,6 +159,7 @@ public abstract class Assert {
* assertion fails
* @see StringUtils#hasLength
* @throws IllegalArgumentException if the text is empty
* @since 5.0
*/
public static void hasLength(String text, Supplier<String> messageSupplier) {
if (!StringUtils.hasLength(text)) {
@@ -209,19 +182,6 @@ public abstract class Assert {
}
}
/**
* Assert that the given String is not empty; that is,
* it must not be {@code null} and not the empty String.
* <pre class="code">Assert.hasLength(name);</pre>
* @param text the String to check
* @see StringUtils#hasLength
* @throws IllegalArgumentException if the text is empty
*/
public static void hasLength(String text) {
hasLength(text,
"[Assertion failed] - this String argument must have length; it must not be null or empty");
}
/**
* Assert that the given String contains valid text content; that is, it must not
* be {@code null} and must contain at least one non-whitespace character.
@@ -233,6 +193,7 @@ public abstract class Assert {
* assertion fails
* @see StringUtils#hasText
* @throws IllegalArgumentException if the text does not contain valid text content
* @since 5.0
*/
public static void hasText(String text, Supplier<String> messageSupplier) {
if (!StringUtils.hasText(text)) {
@@ -255,19 +216,6 @@ public abstract class Assert {
}
}
/**
* Assert that the given String contains valid text content; that is, it must not
* be {@code null} and must contain at least one non-whitespace character.
* <pre class="code">Assert.hasText(name, "'name' must not be empty");</pre>
* @param text the String to check
* @see StringUtils#hasText
* @throws IllegalArgumentException if the text does not contain valid text content
*/
public static void hasText(String text) {
hasText(text,
"[Assertion failed] - this String argument must have text; it must not be null, empty, or blank");
}
/**
* Assert that the given text does not contain the given substring.
* <pre class="code">
@@ -278,6 +226,7 @@ public abstract class Assert {
* @param messageSupplier a supplier for the exception message to use if the
* assertion fails
* @throws IllegalArgumentException if the text contains the substring
* @since 5.0
*/
public static void doesNotContain(String textToSearch, String substring, Supplier<String> messageSupplier) {
if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) &&
@@ -301,18 +250,6 @@ public abstract class Assert {
}
}
/**
* Assert that the given text does not contain the given substring.
* <pre class="code">Assert.doesNotContain(name, "rod");</pre>
* @param textToSearch the text to search
* @param substring the substring to find within the text
* @throws IllegalArgumentException if the text contains the substring
*/
public static void doesNotContain(String textToSearch, String substring) {
doesNotContain(textToSearch, substring,
() -> "[Assertion failed] - this String argument must not contain the substring [" + substring + "]");
}
/**
* Assert that an array contains elements; that is, it must not be
* {@code null} and must contain at least one element.
@@ -323,6 +260,7 @@ public abstract class Assert {
* @param messageSupplier a supplier for the exception message to use if the
* assertion fails
* @throws IllegalArgumentException if the object array is {@code null} or contains no elements
* @since 5.0
*/
public static void notEmpty(Object[] array, Supplier<String> messageSupplier) {
if (ObjectUtils.isEmpty(array)) {
@@ -344,18 +282,6 @@ public abstract class Assert {
}
}
/**
* Assert that an array contains elements; that is, it must not be
* {@code null} and must contain at least one element.
* <pre class="code">Assert.notEmpty(array);</pre>
* @param array the array to check
* @throws IllegalArgumentException if the object array is {@code null} or
* contains no elements
*/
public static void notEmpty(Object[] array) {
notEmpty(array, "[Assertion failed] - this array must not be empty: it must contain at least 1 element");
}
/**
* Assert that an array contains no {@code null} elements.
* <p>Note: Does not complain if the array is empty!
@@ -366,6 +292,7 @@ public abstract class Assert {
* @param messageSupplier a supplier for the exception message to use if the
* assertion fails
* @throws IllegalArgumentException if the object array contains a {@code null} element
* @since 5.0
*/
public static void noNullElements(Object[] array, Supplier<String> messageSupplier) {
if (array != null) {
@@ -395,17 +322,6 @@ public abstract class Assert {
}
}
/**
* Assert that an array contains no {@code null} elements.
* <p>Note: Does not complain if the array is empty!
* <pre class="code">Assert.noNullElements(array);</pre>
* @param array the array to check
* @throws IllegalArgumentException if the object array contains a {@code null} element
*/
public static void noNullElements(Object[] array) {
noNullElements(array, "[Assertion failed] - this array must not contain any null elements");
}
/**
* Assert that a collection contains elements; that is, it must not be
* {@code null} and must contain at least one element.
@@ -417,6 +333,7 @@ public abstract class Assert {
* assertion fails
* @throws IllegalArgumentException if the collection is {@code null} or
* contains no elements
* @since 5.0
*/
public static void notEmpty(Collection<?> collection, Supplier<String> messageSupplier) {
if (CollectionUtils.isEmpty(collection)) {
@@ -439,19 +356,6 @@ public abstract class Assert {
}
}
/**
* Assert that a collection contains elements; that is, it must not be
* {@code null} and must contain at least one element.
* <pre class="code">Assert.notEmpty(collection, "Collection must contain elements");</pre>
* @param collection the collection to check
* @throws IllegalArgumentException if the collection is {@code null} or
* contains no elements
*/
public static void notEmpty(Collection<?> collection) {
notEmpty(collection,
"[Assertion failed] - this collection must not be empty: it must contain at least 1 element");
}
/**
* Assert that a Map contains entries; that is, it must not be {@code null}
* and must contain at least one entry.
@@ -462,6 +366,7 @@ public abstract class Assert {
* @param messageSupplier a supplier for the exception message to use if the
* assertion fails
* @throws IllegalArgumentException if the map is {@code null} or contains no entries
* @since 5.0
*/
public static void notEmpty(Map<?, ?> map, Supplier<String> messageSupplier) {
if (CollectionUtils.isEmpty(map)) {
@@ -483,29 +388,6 @@ public abstract class Assert {
}
}
/**
* Assert that a Map contains entries; that is, it must not be {@code null}
* and must contain at least one entry.
* <pre class="code">Assert.notEmpty(map);</pre>
* @param map the map to check
* @throws IllegalArgumentException if the map is {@code null} or contains no entries
*/
public static void notEmpty(Map<?, ?> map) {
notEmpty(map, "[Assertion failed] - this map must not be empty; it must contain at least one entry");
}
/**
* Assert that the provided object is an instance of the provided class.
* <pre class="code">Assert.instanceOf(Foo.class, foo);</pre>
* @param type the type to check against
* @param obj the object to check
* @throws IllegalArgumentException if the object is not an instance of type
* @see Class#isInstance
*/
public static void isInstanceOf(Class<?> type, Object obj) {
isInstanceOf(type, obj, "");
}
/**
* Assert that the provided object is an instance of the provided class.
* <pre class="code">
@@ -519,11 +401,12 @@ public abstract class Assert {
* in ":" or "." so that the generated message looks OK when appended to it.
* @throws IllegalArgumentException if the object is not an instance of type
* @see Class#isInstance
* @since 5.0
*/
public static void isInstanceOf(Class<?> type, Object obj, Supplier<String> messageSupplier) {
notNull(type, "Type to check against must not be null");
if (!type.isInstance(obj)) {
isInstanceCheckFailed(type, obj, nullSafeGet(messageSupplier));
instanceCheckFailed(type, obj, nullSafeGet(messageSupplier));
}
}
@@ -541,26 +424,26 @@ public abstract class Assert {
public static void isInstanceOf(Class<?> type, Object obj, String message) {
notNull(type, "Type to check against must not be null");
if (!type.isInstance(obj)) {
isInstanceCheckFailed(type, obj, message);
instanceCheckFailed(type, obj, message);
}
}
private static void isInstanceCheckFailed(Class<?> type, Object obj, String message) {
throw new IllegalArgumentException(
(StringUtils.hasLength(message) ? message + " " : "") +
"Object of class [" + (obj != null ? obj.getClass().getName() : "null") +
"] must be an instance of " + type);
/**
* Assert that the provided object is an instance of the provided class.
* <pre class="code">Assert.instanceOf(Foo.class, foo);</pre>
* @param type the type to check against
* @param obj the object to check
* @throws IllegalArgumentException if the object is not an instance of type
* @see Class#isInstance
*/
public static void isInstanceOf(Class<?> type, Object obj) {
isInstanceOf(type, obj, "");
}
/**
* Assert that {@code superType.isAssignableFrom(subType)} is {@code true}.
* <pre class="code">Assert.isAssignable(Number.class, myClass);</pre>
* @param superType the super type to check
* @param subType the sub type to check
* @throws IllegalArgumentException if the classes are not assignable
*/
public static void isAssignable(Class<?> superType, Class<?> subType) {
isAssignable(superType, subType, "");
private static void instanceCheckFailed(Class<?> type, Object obj, String message) {
String className = (obj != null ? obj.getClass().getName() : "null");
throw new IllegalArgumentException(StringUtils.hasLength(message) ? message + ": " + className :
"Object of class [" + className + "] must be an instance of " + type);
}
/**
@@ -575,11 +458,12 @@ public abstract class Assert {
* by this method in order to provide further context. It should normally end
* in ":" or "." so that the generated message looks OK when appended to it.
* @throws IllegalArgumentException if the classes are not assignable
* @since 5.0
*/
public static void isAssignable(Class<?> superType, Class<?> subType, Supplier<String> messageSupplier) {
notNull(superType, "Super type to check against must not be null");
if (subType == null || !superType.isAssignableFrom(subType)) {
isAssignableCheckFailed(superType, subType, nullSafeGet(messageSupplier));
assignableCheckFailed(superType, subType, nullSafeGet(messageSupplier));
}
}
@@ -596,12 +480,23 @@ public abstract class Assert {
public static void isAssignable(Class<?> superType, Class<?> subType, String message) {
notNull(superType, "Super type to check against must not be null");
if (subType == null || !superType.isAssignableFrom(subType)) {
isAssignableCheckFailed(superType, subType, message);
assignableCheckFailed(superType, subType, message);
}
}
private static void isAssignableCheckFailed(Class<?> superType, Class<?> subType, String message) {
throw new IllegalArgumentException((StringUtils.hasLength(message) ? message + " " : "") +
/**
* Assert that {@code superType.isAssignableFrom(subType)} is {@code true}.
* <pre class="code">Assert.isAssignable(Number.class, myClass);</pre>
* @param superType the super type to check
* @param subType the sub type to check
* @throws IllegalArgumentException if the classes are not assignable
*/
public static void isAssignable(Class<?> superType, Class<?> subType) {
isAssignable(superType, subType, "");
}
private static void assignableCheckFailed(Class<?> superType, Class<?> subType, String message) {
throw new IllegalArgumentException(StringUtils.hasLength(message) ? message + ": " + subType :
subType + " is not assignable to " + superType);
}
@@ -618,6 +513,7 @@ public abstract class Assert {
* @param messageSupplier a supplier for the exception message to use if the
* assertion fails
* @throws IllegalStateException if {@code expression} is {@code false}
* @since 5.0
*/
public static void state(boolean expression, Supplier<String> messageSupplier) {
if (!expression) {
@@ -641,19 +537,6 @@ public abstract class Assert {
}
}
/**
* Assert a boolean expression, throwing an {@link IllegalStateException}
* if the expression evaluates to {@code false}.
* <p>Call {@link #isTrue} if you wish to throw an {@code IllegalArgumentException}
* on an assertion failure.
* <pre class="code">Assert.state(id == null);</pre>
* @param expression a boolean expression
* @throws IllegalStateException if {@code expression} is {@code false}
*/
public static void state(boolean expression) {
state(expression, "[Assertion failed] - this state invariant must be true");
}
private static String nullSafeGet(Supplier<String> messageSupplier) {
return (messageSupplier != null ? messageSupplier.get() : null);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -903,7 +903,7 @@ public class ConcurrentReferenceHashMap<K, V> extends AbstractMap<K, V> implemen
@Override
public void remove() {
Assert.state(this.last != null);
Assert.state(this.last != null, "No element to remove");
ConcurrentReferenceHashMap.this.remove(this.last.getKey());
}
}