A programmer's tale

Tuesday, January 30, 2007

Java 1.5 Bug or What? Java 1.6 really rocks - A real life finding

Recently I have found a little but very interesting real life reason that why Java 6.0 is better, robust and stable JDK than Java 5.0; better to say any previous version.
I have written a dialog in Swing that accept user key for my current project REPro- a 100% pure Java reporting solution. We had migrated our project code base to Java 1.5 quite ago. That’s why the initial target Java platform for my Swing Dialog was Java1.5. Look at the sample code for that here:

public class LicenseDialog extends JDialog {

   public LicenseDialog(String title) {
      //..
      setTitle(title);
      setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
      setSize(400, 300);
      //..
   }
}

And the sample main class:

public class Main {
   public static void main(String[] args) {
      JDialog dlg = new LicenseDialog ("License Key");
      dlg.setVisible(true);
   }
}

Now our dialog is shown correctly but when I click the close button, it does nothing. The program doesn’t exit as expected. First time I’m very surprised, but that behavior meets our requirement, because it’s quite common to many License entry dialog; user can exit by clicking only “cancel” button. Hence I didn’t change my code, it remains unnoticed. But recently I tested our code from java 1.5 to Java 1.6. The problem arises. When I compiled and run the same code in Java 1.6, the program fails during run time giving the following error:

Exception in thread "main" java.lang.IllegalArgumentException:
defaultCloseOperation must be one of:
DO_NOTHING_ON_CLOSE,
HIDE_ON_CLOSE, or DISPOSE_ON_CLOSE

The cause: The method actually works correctly for only three parameter WindowConstants.DO_NOTHING_ON_CLOSE , WindowConstants.HIDE_ON_CLOSE or WindowConstants.DISPOSE_ON_CLOSE. If you put any other parameter, then it the method doesn’t tell you anything but doesn’t work correctly in Java 1.5. The method in Java 1.5 is :

public void setDefaultCloseOperation(int operation) {
   this.defaultCloseOperation = operation;
}

When you put JDialog.EXIT_ON_CLOSE in the method it doesn’t match the required parameter and nothing will happen. But dude, see the same method in Java 1.6:

public void setDefaultCloseOperation(int operation) {
   if (operation != DO_NOTHING_ON_CLOSE &&
    operation != HIDE_ON_CLOSE &&
    operation != DISPOSE_ON_CLOSE) {
    throw new IllegalArgumentException("defaultCloseOperation
    must be one of: DO_NOTHING_ON_CLOSE,
    HIDE_ON_CLOSE,
    or DISPOSE_ON_CLOSE");
   }
   int oldValue = this.defaultCloseOperation;
   this.defaultCloseOperation = operation;
   firePropertyChange("defaultCloseOperation", oldValue, operation);
}


It now handles the parameter correctly and gives the above run time error. It also change the "defaultCloseOperation" property of java.awt.Component by invoking firePropertyChange with required method. When I googled it I found the Bug entry for JDialog.

Java 1.6 is really the best JDK ever and it rocks. Some interesting facts about Java 1.6 are:
1. It is developed and tested on nearly 600 machines worldwide, most in a grid.
2. It covered three architectures: x86, AMD64, SPARC
3. 24 operating system including Red Hat linux 7.2, 9.0, RHAS 2.1, 3.0, 4.0, Fedora Core 3, 4, Solaris 8, 9, 10, 11, SuSE Linux 8.1, 0.9, 0.3, Windows 98 SE, ME, NT, 2000, 2000 Pro, 2000 Server, 2003, 2003 AS, XP Home, XP Pro and Vista, etc.
4. 84 tested configurations, 92,316 conformance tests (JCK)
5. 20,244 functional tests
6. 6,930 unit and regression tests
7. It’s test cycle length: 5 weeks – Tier 1, 10 weeks – All configurations.
8. For quality, compatibility and stability, Java 1.6 under gone continuous multi-platform testing.
9. It has community engagement including crack-the-verifier contest. There were 5 submissions but no successful attempts, regressions challenges; 133 submissions resulting in 72 bugs fixed.
You can see the other interesting things about Java 1.6 here.

Labels:

0 Comments:

Post a Comment

<< Home