assert 的学习
来源:互联网 发布:拾柒网络 编辑:程序博客网 时间:2024/06/06 04:08
从JDK1.4版本开始,Java语言引入了断言(assert)机制。该类在package org.springframework.util包下。
目的:程序调试
测试代码或者调试程序时,总会做出一些假设,断言就是用于在代码中捕捉这些假设
当要判断一个方法传入的参数时,我们就可以使用断言。
例如:
public Order create(Cart cart, Receiver receiver, PaymentMethod paymentMethod, ShippingMethod shippingMethod,
BoxMethod boxMethod, CouponCode couponCode, boolean isInvoice) {
Assert.notNull(cart);
Assert.notEmpty(cart.getCartItems());
Assert.isTrue(cart.checkedSize()>0, "购物项选择必须大于0");
Assert.notNull(receiver);
Assert.notNull(paymentMethod);
Assert.notNull(shippingMethod);
}
这样可以检测传入的参数是否符合要求,当这些断言方法在入参不满足要求时就会抛出 IllegalArgumentException。
断言常用的方法
1. notNull(Object object)
当 object 不为 null 时抛出异常,notNull(Object object, String message) 方法允许您通过 message 定制异常信息。和 notNull() 方法断言规则相反的方法是 isNull(Object object)/isNull(Object object, String message),它要求入参一定是 null;
2. isTrue(boolean expression) / isTrue(boolean expression, String message)
当 expression 不为 true 抛出异常;
3. 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[] 类型的入参进行判断;
4. hasLength(String text) / hasLength(String text, String message) 当 text 为 null 或长度为 0 时抛出异常;
5. hasText(String text) / hasText(String text, String message) text 不能为 null 且必须至少包含一个非空格的字符,否则抛出异常;
6. isInstanceOf(Class clazz, Object obj) / isInstanceOf(Class type, Object obj, String message) 如果 obj 不能被正确造型为 clazz 指定的类将抛出异常;
7. isAssignable(Class superType, Class subType) / isAssignable(Class superType, Class subType, String message) subType 必须可以按类型匹配于 superType,否则将抛出异常;
1. notNull(Object object)
当 object 不为 null 时抛出异常,notNull(Object object, String message) 方法允许您通过 message 定制异常信息。和 notNull() 方法断言规则相反的方法是 isNull(Object object)/isNull(Object object, String message),它要求入参一定是 null;
2. isTrue(boolean expression) / isTrue(boolean expression, String message)
当 expression 不为 true 抛出异常;
3. 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[] 类型的入参进行判断;
4. hasLength(String text) / hasLength(String text, String message) 当 text 为 null 或长度为 0 时抛出异常;
5. hasText(String text) / hasText(String text, String message) text 不能为 null 且必须至少包含一个非空格的字符,否则抛出异常;
6. isInstanceOf(Class clazz, Object obj) / isInstanceOf(Class type, Object obj, String message) 如果 obj 不能被正确造型为 clazz 指定的类将抛出异常;
7. isAssignable(Class superType, Class subType) / isAssignable(Class superType, Class subType, String message) subType 必须可以按类型匹配于 superType,否则将抛出异常;
0 0
- assert 的学习
- assert宏的深入学习
- const 与assert的学习
- linux 之assert学习
- assert断言学习
- assert宏学习
- node学习-assert测试
- boost::assert学习笔记
- #python学习笔记#assert
- c++ assert()学习总结
- 标准C的头文件assert.h学习总结
- C语言学习笔记(3)assert的使用
- Python学习笔记(5)--断言assert的用法
- 问题: 什么是ASSERT()? ASSERT()和assert()的区别是什么?
- ASSERT()和assert()的区别是什么?
- assert.h中assert的作用
- assert和de-assert的理解
- ASSERT 宏的使用
- MVC之视图
- 欧拉函数
- BZOJ 2721 [Violet 5]樱花
- Swift学习记录 -- 8.字典的基本操作
- sqlserver 练习题目(存储过程:循环erp数据表,生成另一个表数据)
- assert 的学习
- E. Coins ——BNUOJ 规律题
- 冒泡排序
- stm32 堆和栈(stm32 Heap & Stack)【worldsing笔记】
- 野指针,空指针
- u-boot-1.1.6源代码---start.S
- 缺省值,默认值
- VS 运行程序问题解决
- 代码规范