advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Tutorials : Assertions in Java :

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/

 

How to Add Java Applets to Your Site

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.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs