Spring断言机制

isTrue


isTrue(boolean expression) / isTrue(boolean expression, String message)当 expression 不为 true 抛出异常;

public static void isTrue(boolean expression, String message) {

if (!expression) {

throw new IllegalArgumentException(message);

}

}

public static void isTrue(boolean expression) {

isTrue(expression, “[Assertion failed] - this expression must be true”);

}

notEmpty


notEmpty(Collection collection) / notEmpty(Collection collection, String message)

当集合未包含元素时抛出异常。

notEmpty(Map map) / notEmpty(Map map, String message) 和 notEmpty(Object[] array, String message) / ## notEmpty(Object[] array, String message) 分别对 Map 和 Object[] 类型的入参进行判断;

public static void notEmpty(Object[] array, String message) {

if (ObjectUtils.isEmpty(array)) {

throw new IllegalArgumentException(message);

}

}

public static void notEmpty(Object[] array) {

notEmpty(array, “[Assertion failed] - this array must not be empty: it must contain at least 1 element”);

}

hasLength(String text) / hasLength(String text, String message)


当 text 为 null 或长度为 0 时抛出异常;

public static void hasLength(String text) {

hasLength(text,

“[Assertion failed] - this String argument must have length; it must not be null or empty”);

}

public static void hasLength(String text, String message) {

if (!StringUtils.hasLength(text)) {

throw new IllegalArgumentException(message);

}

}

hasText(String text) / hasText(String text, String message)


text 不能为 null 且必须至少包含一个非空格的字符,否则抛出异常;

public static void hasText(String text, String message) {

if (!StringUtils.hasText(text)) {

throw new IllegalArgumentException(message);

}

}

public static void hasText(String text) {

hasText(text,

“[Assertion failed] - this String argument must have text; it must not be null, empty, or blank”);

}

isInstanceOf(Class clazz, Object obj) / isInstanceOf(Class type, Object obj, String message)


如果 obj 不能被正确造型为 clazz 指定的类将抛出异常;

isAssignable(Class superType, Class subType) / isAssignable(Class superType, Class subType, String message)


subType 必须可以按类型匹配于 superType,否则将抛出异常;

最后

现在正是金三银四的春招高潮,前阵子小编一直在搭建自己的网站,并整理了全套的**【一线互联网大厂Java核心面试题库+解析】:包括Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等**

image
全套的**【一线互联网大厂Java核心面试题库+解析】:包括Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等**

[外链图片转存中…(img-pSpU0Ile-1719723329232)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值