Enabling and Disabling Assertions:
By default assertions are disabled during runtime in the new JDK 1.4 runtime.
Java allows you to enable assertions through a command line parameter
–enableassertions or –ea during runtime.
Eg. java –ea MyFile
java –enableassertions MyFile
To disable assertions a similar command line switch can be used:
–disableassertion or –da
Assertions can be enabled or disabled at different levels of granularity.
A few of the options are listed below.
java –ea(da) - Enables or disables assertions on all classes except
system classes
java –ea(da):<Class Name> Enables or disables assertions on the
specified class or classes
java –ea(da):… Enables or disables all classes in the default package of the
directory
java –ea(da):<Package Name> Enables or disables all classes in the
package and it's sub packages
Specific
classes or packages can have assertions either enabled or disabled. You can have a
whole package assertion enabled and disable assertions on a few classes inside
it. In the same way a derived class can have assertions enabled while the base
class may have assertions disabled.
In
addition to enabling assertions from the command line java, provides a facility in
the class loader to enable or disable assertions programmatically. The
classloader java.lang.ClassLoader has been modified to include a few more
assertion specific methods.
public void setDefaultAssertionStatus(boolean enabled);
public void setPackageAssertionStatus(String packageName,boolean enabled);
public void setClassAssertionStatus(String className,boolean enabled);
public void clearAssertionStatus();
The first method sets the assertion status for all classes and packages
loaded by the class loader. The second method can be used to override the
default status set by enabling or disabling the status for a package and sub
packages. The third method overrides the status for a specific class. The last
one resets the assertion status for all classes and packages to false and also
resets the class loader’s assertion status to false.
However the above methods only work for classes not already loaded by the
class loader.
Conclusion:
Assertions in the new J2SE 1.4 Specification are a welcome inclusion, but it
remains to be seen how the developer community will react to it and
start using it.
The idea to have assertions has been around since the first version. Sun admits
that it had proposed to include assertions in the first version but could not
because of time constraints. Programmers familiar with Smalltalk and Eiffel
might have had exposure to assertions but to most of the new age programmers who
graduated from C++ this remains as a new concept that needs to be explored and
implemented. Moreover the idea to include assertions as a change to the
specifications rather than an additional API shows Sun’s commitment to make it a
permanent and sustaining change. Unlike the changes Sun made to the collections
interface where most of the implementation were transparent to the programmer,
this change is a more serious one that requires the programmer to change the way
he or she writes code.
This could be compared to the event handling change Sun made to the JDK 1.1
specification. However in this case sun provided backward compatibility for a
long time before completely doing away with the jdk1.0 event model.
The present version of assertions is a basic
one. It remains to be seen how assertions will evolve over
time and become a part of the java language.
References:
JSR 41 A simple Assertion Facility http://www.jcp.org/content/main/jsr/detail/materials/asrt_prop.html
Implementing Assertions in java. http://www.ddj.com/documents/s=924/ddj9801d/9801d.htm
Applying Design by Contract by Bertrand Mayer. http://www.inf.ethz.ch/personal/meyer/publications/computer/contract.pdf
An introduction to design by contract: http://archive.eiffel.com/doc/manuals/technology/contract/<
/a>
JDK 1.4 Logging Join Keld Hansen as he goes where few men has gone
before.... deep into the JDK 1.4 Logging API. http://javaboutique.internet.com/tutorials/logging_API/index.html
JDK 1.4 Exception chaining http://javaboutique.internet.com/tutorials/Chained_Exceptions/
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|