Assert断言类,这是一个很常见的类,我们调试的时候验证方法参数使用。一个个方法开始看。
public static void state(boolean expression, String message) {
if (!expression) {
throw new IllegalStateException(message);
}
}
判断expression的boolean值,为false时抛出IllegalStateException异常,异常信息为传入的message。为true不处理。
public static void state(boolean expression, Supplier<String> messageSupplier) {
if (!expression) {
throw new IllegalStateException(nullSafeGet(messageSupplier));
}
}
@Nullable
private static String nullSafeGet(@Nullable Supplier<String> messageSupplier) {
return (messageSupplier != null ? messageSupplier.get() : null);
}
和上面那个state方法其实一样的,只是换了一种方式传参异常信息 Assert.state(true, () -> "enigma");
public static void isTrue(boolean expression, String message) {
if (!expression) {
throw new IllegalArgumentException(message);
}
}
判断expression的boolean值,为false时抛出IllegalArgumentException异常,异常信息为传入的message。为true不处理。
public static void isNull(@Nullable Object object, String message) {
if (object != null) {
throw new IllegalArgumentException(message);
}
}
判断object为否为空,不为空时抛出IllegalArgumentException异常,异常信息为传入的message。为空不处理。
public static void notNull(@Nullable Object object, String message) {
if (object == null) {
throw new IllegalArgumentException(message);
}
}
与isNull相反。
public static void hasLength(@Nullable String text, String message) {
if (!StringUtils.hasLength(text)) {
throw new IllegalArgumentException(message);
}
}
判断传入的string值的长度,如果string为空 或者null,则抛出IllegalArgumentException异常,异常信息为传入的message。string不为空不为null不处理。
public static void hasText(@Nullable String text, String message) {
if (!StringUtils.hasText(text)) {
throw new IllegalArgumentException(message);
}
}
判断传入的string值为否为空串或是否为null或是否为空白符,如果是,则抛出IllegalArgumentException异常,异常信息为传入的message,否则不处理。
public static void doesNotContain(@Nullable String textToSearch, String substring, String message) {
if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) &&
textToSearch.contains(substring)) {
throw new IllegalArgumentException(message);
}
}
满足textToSearch不为空或者null并且substring不为空或者null并且textToSearch包括substring,抛出IllegalArgumentException异常,异常信息为传入的message,否则不处理。
public static void notEmpty(@Nullable Object[] array, String message) {
if (ObjectUtils.isEmpty(array)) {
throw new IllegalArgumentException(message);
}
}
判断Object数组array是否为空或者null,如果是,抛出IllegalArgumentException异常,异常信息为传入的message,否则不处理。
public static void noNullElements(@Nullable Object[] array, String message) {
if (array != null) {
for (Object element : array) {
if (element == null) {
throw new IllegalArgumentException(message);
}
}
}
}
当Object数组array不为空array的子元素任意一个为null时,抛出IllegalArgumentException异常,异常信息为传入的message,否则不处理。
public static void notEmpty(@Nullable Collection<?> collection, String message) {
if (CollectionUtils.isEmpty(collection)) {
throw new IllegalArgumentException(message);
}
}
当Collection集合collection为空,抛出IllegalArgumentException异常,异常信息为传入的message,否则不处理。
public static void notEmpty(@Nullable Map<?, ?> map, String message) {
if (CollectionUtils.isEmpty(map)) {
throw new IllegalArgumentException(message);
}
}
当Map集合map为空,抛出IllegalArgumentException异常,异常信息为传入的message,否则不处理。
public static void isInstanceOf(Class<?> type, @Nullable Object obj, String message) {
notNull(type, "Type to check against must not be null");
if (!type.isInstance(obj)) {
instanceCheckFailed(type, obj, message);
}
}
先判断type是否为空,为空抛出IllegalArgumentException异常,异常信息为“Type to check against must not be null”,obj不能转化为type的类型时调用instanceCheckFailed()方法。
private static void instanceCheckFailed(Class<?> type, @Nullable Object obj, @Nullable String msg) {
String className = (obj != null ? obj.getClass().getName() : "null");
String result = "";
boolean defaultMessage = true;
if (StringUtils.hasLength(msg)) {
// 是否以":"、";"、","、"."结尾
if (endsWithSeparator(msg)) {
result = msg + " ";
}
else {
//msg以空格结尾时返回msg + 空串 + typeName 否则返回msg + ": " + typeName
result = messageWithTypeName(msg, className);
defaultMessage = false;
}
}
if (defaultMessage) {
result = result + ("Object of class [" + className + "] must be an instance of " + type);
}
throw new IllegalArgumentException(result);
}
先获取obj的类名,设置result为空串,defaultMessage为true;
然后判断msg是否为空,处理result和defaultMessage;
根据defaultMessage处理result值;
抛出IllegalArgumentException异常,异常信息为result最终结果。
public static void isAssignable(Class<?> superType, @Nullable Class<?> subType, String message) {
notNull(superType, "Super type to check against must not be null");
// isAssignableFrom方法判断A是否是B的父类或者和B类型相同或者B实现了A接口.是类与类之间的比较
if (subType == null || !superType.isAssignableFrom(subType)) {
assignableCheckFailed(superType, subType, message);
}
}
先判断superType是否为空,为空抛出IllegalArgumentException异常,异常信息为“Super type to check against must not be null”,如果superType为空或者 superType不是subType的父类也不是同一种类型,调用assignableCheckFailed()方法处理失败。
private static void assignableCheckFailed(Class<?> superType, @Nullable Class<?> subType, @Nullable String msg) {
String result = "";
boolean defaultMessage = true;
if (StringUtils.hasLength(msg)) {
if (endsWithSeparator(msg)) {
result = msg + " ";
}
else {
result = messageWithTypeName(msg, subType);
defaultMessage = false;
}
}
if (defaultMessage) {
result = result + (subType + " is not assignable to " + superType);
}
throw new IllegalArgumentException(result);
}
assignableCheckFailed()方法与instanceCheckFailed()方法处理类似,不多说。
自此,对Assert断言类的每一个方法基本上都了解差不多了,结合源码的AssertTests试一下好了。