Assertion in Java
来源:互联网 发布:linux oracle 重启 编辑:程序博客网 时间:2024/06/11 00:38
An assertion is a statement in the Java language that enables us to test our assumptions about our program. For example, if we write a method to calculate the money in our pocket, then we might assert that the total value is more than 0.
1. Assert forms.
1) assert Expression1;
Where the Expression1is a Boolean expression. When the system run the assert, it evaluate Expression, and if it is false, it would throw AssertionError with no detail message. Pls noted that AssertionError inherit from Error
2) assert Expression1: Expression2;
where:
- Expression1 is a boolean expression.
- Expression2 is an expression that has a value. (It cannot be an invocation of a method that is declared void.)
Pls noted that AssertionError inherit from Error, so we may hv no need to handle, also may catch it as normal Error.
2. Putting assert into Code.
1) Internal Invariants
You should now use an assertion whenever you would have written a comment that asserts an invariant.
if (i % 3 == 0) {
...
} else if (i % 3 == 1) {
...
} else {
assert i % 3 == 2 : i;
...
}
2) Control-Flow Invariants
It points out another general area where you should use assertions: place an assertion at any location you assume will not be reached.
void foo() {
for (...) {
if (...)
return;
}
assert false; // Execution should never reach this point!
}
3) Preconditions.
private void setDelegationInfoIntoSubmissionDto(…) {
assert isf10Snapshot.getIsfDelegation()!=null;
String delegatorCompanyId = isf10Snapshot.getIsfDelegation().getDelegatorCompanyID();
submissionDTO.setDelegator(true);
…
}
3. Enabling/Disabling Assertions.
1) To enable assertions at various granularities, use the -enableassertions, or -ea, switch. To disable assertions at various granularities, use the -disableassertions, or -da, switch. You specify the granularity with the arguments that you provide to the switch:
· no arguments
Enables or disables assertions in all classes except system classes.
Enables or disables assertions in all classes except system classes.
· packageName...
Enables or disables assertions in the named package and any subpackages.
Enables or disables assertions in the named package and any subpackages.
· ...
Enables or disables assertions in the unnamed package in the current working directory.
Enables or disables assertions in the unnamed package in the current working directory.
·className
Enables or disables assertions in the named class
Enables or disables assertions in the named class
2) Enable it in oc4j
Use -esa | -enablesystemassertions to enable system assertions;
Use -dsa | -disablesystemassertions to disable system assertions
3) Enable it in Eclipse
In run configuration dialog, switch to Arguments tab, input –ea/-da in VM arguments filed.
4.Performance
The Assertion mechanism is as following. When the compiler finds an assertion in a class, it adds a generated static final field named $assertionDiabled to the class. And the assertion itself is compiled into a statement of the form:
if($assertionDisabled)
if(!boolean_expression)
throw new assertionError(String_Expression);
so, if assertion overhead, it would introduce an performance issue into system, no matter the assertion is enabled or disabled.
- Assertion in Java
- java assertion
- Assertion failure in -[GPUImageMovieWriter createDataFBO]
- *** Assertion failure in -[UITableView _endCellAnimationsWithContext:]
- Assertion failure in -[UIView layoutSublayersOfLayer:],
- Assertion failure in -[UICGColor encodeWithCoder:]
- *** Assertion failure in -[UITableView layoutSublayersOfLayer:]
- Assertion failure in -[UIView layoutSublayersOfLayer:]
- Assertion failure in -[MASViewConstraint install]
- *** Assertion failure in -[UICollectionViewData validateLayoutInRect:]
- Assertion failure in -[UICollectionView layoutSublayersOfLayer:]
- Assertion failure in -[UITableView layoutSublayersOfLayer:]
- Assertion failure in -[UICollectionViewData validateLayoutInRect:],
- *** Assertion failure in -[MBProgressHUD initWithView:],
- 学习 java assertion (zz)
- Java的特性-assertion
- [转] java assertion使用
- java assertion详解
- 浅谈C语言的可变参数
- oracle学习资料,从小潘那里偷看过来的,作个copy,哈哈~~
- Spool:增强版Instapaper
- 开门第一篇
- C# 进制转换
- Assertion in Java
- 重定向 1>&2 2>&1
- C++基础5:函数和函数指针
- Http和Socket连接区别
- TPS游戏设计
- 后台控制改变Silverlight 中的Ellipse控件的Fill填充图片
- 扭矩
- (2011.08.28 - 2011.09.12)十六天的学习小记
- 对openGLES的粗浅认识