Sunday, February 22, 2009

java Assertions

Assertions:
It is one of the powerful debugging mechanism which is available in java.This is alternative to simple debugging System.out.println mechanism.If we are performing debugging by using System.out.println after fixing the problem.
If the programmer forget the deletion of those System.out.println,they will execute in the production also,so that it may effect the performance of the system.To resolve this problem,sun people provided the alternative debugging mechanism in 1.4 version.
Key word vs Identifier
Until 1.3 version assert is not a key word,hence we are allowed to used as identifier ,but from 1.4 version onwards ,assert is declared as keyword hence we are not allowed to used as identifier.
Example:
Class Sample
{
public static void main(String ar[])
{
int assert =1.0;
System.out.println(x); // 10 in 1.3 version but 1.4 version compile time error
}
}
Types of assertion statements:
example:
Class Sample
{
public static void main(String arg[])
{
int x=10;
assert (x==10); // valid
System.out.println(x); or assert (x<10)

Javac -ea Sample.java
‘-ea’ for enabling assertion.
There are two types of assertions are there
1.simple
syntax:
Assert(e);
Here e must be of the type Boolean

2.Argumented.
syntax::
Assert(e1):e2;
E1 must be Boolean type.
E2 may be any type.
(String/method call/Boolean/declaration etc).
Example:
class Sample
{
public static void main(String [] sr[])
{
int x=10;
assert(x>10):m1();
System.out.println(x);
}
public static int m1()
{
// return 1000;
}
note: if return statement is commented ,then compile time error void type is not allowed here.
In the second type of assert statement in the second expression we are allowed to keep any type including method call also.But if we are keeping method call it should not be void return type.violation leads to compile time error.
assertion Example:
Class Sample
{
public static void main(String ar[])
{
int x=10;
assert(x==10):++x;
System.out.println(x); // 10.
i.e in assert e1:e2; if e1 is false then only e2 will execute .
asser(x>10):++x;
then compile time error : assertion error :11.
Javac Sample.java
Java –ea Sample

Various Runtime flags:
-ea: or -enableassertion :To enable assertions in all non-system classes.
-da or disableassertion : To disable assertions in all non-system classes.
-esa or -enablesystemassertions: To enable assertions in system classes.
-dsa or disablesystemassertions: To disable assertion in system classes.
-ea -dsa: To enable non –system assertions and system assertions.
-ea -esa: To enabling both system and non-system assertions.
All the assertions run time flags will execute from left to right ,there is no priority for enable or disable flags.
We can enable or disable assertions either class wise or package wise also.
Take for example in pack1 there are classes like A,B,C
To enable assertion only in the class A,the used flag will be
java –ea:pack1.A A
To enable assertions in the B and C classes ,but disable else where ,the used flag will be java –ea: pack1.B -ea:pack1.C B
To enable assertions in all classes present in pack1 and disable else where ,the used flag will be,
Java –ea:pack1…
Take for example in pack1 there are classes like A,B,C and pack2,under pack2 there are some classes D,E,F

To enable assertions in B class and for all the classes present in the pack2 also,the used flag will be,
Java -ea:pack1… -da:pack1.A -da:pack1.C
Or
Java -ea:pack1.B -ea:pack1.pack2…
For enable assertions in the classes A,B and E
Java –ea:pack1… -da:pack1.pack2… -ea:pack1.pack2.E -da:pack1.C

Proper and Improper use of Assertions:
We can’t use assertions for validating the arguments of a public method.
We can use assertions for validating the arguments of a private method.
It is not proper to use assertions for validating command line arguments.
Example:
int x=10;
Assert(b):++x; // not proper way
System.out.println(x);
We can’t keep the programming logic as part of assert statement .It is not proper use as above example.
If the control never allowed to reach any place,there we can keep assert statement ,this is the best use of assertions.
Example:
Switch(weak)
{
case 1:
System.out.println(“sun”);
Case 2:
System.out.println(“mon”);
................
...............
Case 7:

System.out.println(“sat”);
default:assert(false); // proper way

Throwable ---> error -----> AssertionError

Example:
Class Sample
{
public static void main (String ar[])
{
int x=10;
try
{
assert(x>10); // completely foolish thing
}
catch(Exception e)
{
System.out.println(e)
}
}
We can catch Assertion Error by using try – catch block ,but it is totally stuped type of activity.
We can enable or disable assertions programaticaly by using the methods of classLoader class.
Public void setPackageAssertionStatus(String packagename,Boolean enable)
Public void setClassAssertionStatus(String className,Boolean enable)

0 comments:

Post a Comment