|
JDK 1.4 added a keyword called
assert. You can use assert on a boolean expression that you believe will be
true. If the expression is not true, the program throws an error. Using assert
(and not receiving any errors) increases your confidence in your code. You can
use assert (as opposed to System.out) to debug your code. Visit this link to
learn more about assert: http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html
So far so good but I wouldn't be
writing this blog entry if assert was this simple. Since assert was NOT a
keyword prior to jdk 1.4, you could use it as an identifier as in
private String assert;
Obviously, jdk 1.4 would give you
an error because assert is a keyword in jdk 1.4 and the statement above isn't
the legal use of assert. To avoid this problem, jdk 1.4 compiler was designed
to be 1.3 compatible by default. So, if you write code in jdk 1.4 using assert
as an identifier and compile it, you will simply get a warning because for
.class files, it uses jdk 1.3 compatibility.
The so called 'feature' described
above caused me some trouble when I recently switched my IDE. My code was using
assert in jdk 1.4 way (as in)
assert idao != null;
When I compiled my code, RAD
compiled it against jdk 1.3, thought assert was an identifier and gave me an
error. The link above explains that you must use -source 1.4 flag to correctly
recognize assert statements.
To fix this,
- I went to Window > Preferences > expand Java > select Compiler > Compiler and Classfiles
tab
- Unchecked Use default compliance settings
- Selected 1.4 in both source and .class files compatibility
This may not remove all the
compilation errors however. RAD (and eclipse for that matter) by default uses
compiler settings at project level so Window > Preferences settings may not have any effect.
This makes sense because not all the projects in a workspace may belong to a
single application. You may have some projects in an old application that you
might want to compile against an older version of jdk.
To fix this,
- Right click the project and go to Properties
- Select Java Compiler
- Either set source and .class files compatibility to
1.4 OR choose workspace settings
The only pain point is, if you are
migrating an application with many projects to a new RAD or WAS 6.1 toolkit,
you may have to do this for all the projects.
|