现在的位置: 首页 > 综合 > 正文

Assertion in Java

2013年08月12日 ⁄ 综合 ⁄ 共 2751字 ⁄ 字号 评论关闭
文章目录
   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 Expression1Expression2;
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.
· packageName... 
   Enables or disables assertions in the named package and any subpackages.
· ...
   Enables or disables assertions in the unnamed package in the current working directory.
·className
   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.

抱歉!评论已关闭.