Wednesday, February 8, 2012

What Do You Need to know while Moving Your Source Code to Java 7?

Are you shifting your project to Java 7? Almost all existing programs should run on Java SE 7 without modification. But still there are some minor potential source and binary incompatibilities in the JRE and JDK that involve rare circumstances and "corner cases".

Oracle documentation here explains in detail all the incompatibilities which an application developer needs to take care while moving the project to Java 7. I strongly recommend to read all the incompatibilities from the Oracle site. However, in order to save time, few of the more commonly used are described below:

Area: JSR 334
Synopsis: Improved Exception Handling May Cause source Incompatibility
Description: The following example shows two source incompatibilities: 
class Foo extends Exception {}
class SonOfFoo extends Foo {}
class DaughterOfFoo extends Foo {}
...
try {
   throw new DaughterOfFoo();
} catch (final Foo exception) {
   try {
      throw exception; // used to throw Foo, now throws DaughterOfFoo
   } catch (SonOfFoo anotherException) { // Reachable? }
}

 The first incompatibility is that the throw exception; statement throws a Foo exception in JDK 6, but throws a DaughterOfFoo exception in Java SE 7.
 The second incompatibility is that the catch (SonOfFoo ...) statement compiled under JDK 6 but, under Java SE 7, gets the following error: 
error: exception SonOfFoo is never thrown in body of corresponding try statement
 Such code is likely to be rare, but should be fixed by removing the non-reachable statements.
Nature of Incompatibility: source


Area: API: Language
Synopsis: Spec for java.lang.Float.parseFloat(String) and parseDouble(String) Updated to Document Exception
Description: The java.lang.Float.parseFloat(String) and java.lang.Float.parseDouble(String) methods have been throwing an undocument NullPointerException when passed a null argument since the methods were introduced in J2SE 1.2. The spec has been updated to document the NPEs.
Nature of Incompatibility: behavioral


Area: API: Language
Synopsis: java.lang.Character.isLowerCase/isUpperCase Methods Are Updated to Comply with the Specified Unicode Definition
Description: The specification and the implementation of the isLowerCase and isUpperCase methods have been updated to comply with the Unicode Standard definition to be GD=Lu/Ll + Other_UpperCase/LowerCase. Two new methods, java.lang.Character.isAlphabetic(int) and java.lang.Character.isIdeographic(int) have also been added.
Nature of Incompatibility: behavioral

 
Area: API: Utilities
Synopsis: Inserting an Invalid Element into a TreeMap Throws an NPE
Description: Due to an error in java.util.TreeMap, it was previously possible to insert invalid null elements and elements not implementing the Comparable interface into an empty TreeMap or TreeSet. Only a single invalid element could be inserted into an empty TreeMap or TreeSet; additional elements would cause the expected NullPointerException or ClassCastException. Most other operations upon the collection would also fail. As of Java SE 7, inserting an invalid null element or an element not implementing Comparable into an empty TreeMap or TreeSet throws a NullPointerException.
Nature of Incompatibility: behavioral

 
Area: API: Utilities
Synopsis: Formatter.format() Now Throws FormatFlagsConversionMismatchException
Description: The Formatter.format(String,Object...) method now throws a FormatFlagsConversionMismatchException exception when the "#" flag is specified for conversion "s" and the following argument is not a Formattable instance (including the special case "null").
Nature of Incompatibility: behavioral

 
Area: API: NIO
Synopsis: The Behavior for Several java.nio.channels.DatagramChannel Methods have Changed
Description: The DatagramChannel is the selectable channel for datagram-oriented sockets in the java.nio.channels package. The behavior for the send, receive, and connect methods have been modified. To restore the previous behavior, the sun.nio.ch.bugLevel property can be set to the value of "1.4", "1.5", or "1.6". See the DatagramChannel class specification for more information.
Nature of Incompatibility: behavioral


Area: API: Internationalization
Synopsis: Separation of User Locale and User Interface Locale
Description: The default locale can be independently set for two types of uses: the format setting is used for formatting resources, and the display setting is used in menus and dialogs. The new Locale.getDefault(Locale.Category) method takes a Locale.Category parameter. Previous behavior can be restored by setting the sun.locale.formatasdefault system property to true.
Nature of Incompatibility: behavioral


Area: API: JDBC
Synopsis: New RowSetFactory Interface to allow Creation of a RowSetFactory
Description: New API was introduced to support RowSet 1.1 and, specifically, the ability to write more portable code by creating a RowSetFactory. As part of this update, the definition of some constants has changed slightly, but should not affect most users.
Nature of Incompatibility: source

 
Area: API: JDBC
Synopsis: New JDBC Methods, Including new Methods in Interfaces
Description: For the Java SE 7 release, there are new methods to support JDBC 4.1. This includes methods added to the java.sql.Connection, java.sql.Driver, javax.sql.CommonDatasource, and java.sql.Statement interfaces. Because all methods of an interface must be implemented, previous code that uses these interfaces will not compile on Java SE 7 unless you add the new methods. See the JDBC documentation for more information.
Nature of Incompatibility: source


Area: API: Utilities
Synopsis: Updated sort behavior for Arrays and Collections may throw an IllegalArgumentException
Description: The sorting algorithm used by java.util.Arrays.sort and (indirectly) by java.util.Collections.sort has been replaced. The new sort implementation may throw an IllegalArgumentException if it detects a Comparable that violates the Comparable contract. The previous implementation silently ignored such a situation.
 If the previous behavior is desired, you can use the new system property, java.util.Arrays.useLegacyMergeSort, to restore previous mergesort behavior.
Nature of Incompatibility: behavioral


Area: API: IO
Synopsis: java.io.File.setReadOnly and setWriteable Methods Have New Behavior
Description: As of JDK 7, on Windows, the java.io.File setReadOnly and setWritable methods no longer set the DOS readonly attribute on directories. This means that these methods will fail, by returning false, if the file is a directory. To preserve the relationship with canWrite, the canWrite method returns true if the file is a directory.
 Applications that want to set directories on Windows to be read only must use the new API. In particular, the Files.isWritable method takes into account the effective access (as determined by the file's discretionary access control list) and whether the file is located on a writable volume.
Nature of Incompatibility: behavioral

No comments: