Monday, April 18, 2011

JDK 7 Support in NetBeans IDE 7.0:

To enable JDK 7 support in the NetBeans IDE:

  1. Download the JDK 7 binary for your platform from this page.
    This is the latest promoted development build.
  2. Install JDK 7 on your system.
  3. In the IDE, choose Tools > Java Platforms from the main menu.
  4. Click Add Platform and specify the directory that contains the JDK (e.g. on Windows, this is the JDK installation directory, default is C:\Program Files\Java\jdk1.7.0). 
    The directory that contains the Java platform is marked with the Java Platform icon icon.
  5. In the Platform Name step, verify that the default locations of the Platform Sources zip file and API documentation are valid.
  6. Click Finish to close the Add Java Platform dialog box.
  7. Ensure JDK 1.7 is chosen in the Platforms list and click Close.

Configuring the Project to Use JDK 7

Once you have registered JDK 7 in the IDE, you need to configure your project to use this JDK for compilation, running, and debugging.

  1. Create a Java project. Choose File > New Project and select Java Application as the project type. Click Next.
  2. Type SwitchTest as the project name and specify its location.
  3. In the Files window, right-click the SwitchTest project's node and choose Properties > Libraries. On this tab, choose JDK 1.7 from the list of Java Platforms.
    Setting JDK 7 as the target format.
  4. Switch to the Sources tab of the Project Properties window and choose JDK 7 as the Source/Binary Format. 
    Specifying JDK 7 as the source format
  5. Click OK to save changes. Your project is set to recognize new JDK 7 language features.

Using New JDK 7 Language Constructs: Switch Statement

JDK 7 brings a number of new features and enhancements in different areas, including internationalization, I/O and networking, security, etc. The best way to illustrate the JDK 7 support by the IDE's Java Editor is to demonstrate a few language changes introduced by Project Coin.

One of these changes is a "String in a switch". In the previous versions of Java, the argument of switch had to be only of the following primitive data types: byte, short, char,int, or enum. Starting from JDK 7, you can use arguments of type String in the expression of a switch statement.

  1. Open SwitchTest.java and add the following code. This small sample displays RGB codes for several colors. 
    With JDK 7, the color variable can be a String.
     package switchtest;      public class SwitchTest {      public static void main(String[] args) {          String color = "red";         String colorRGB;         switch (color.toLowerCase()) {             case "black": colorRGB = "000000"; break;             case "red": colorRGB = "ff0000"; break;             case "green": colorRGB = "008000"; break;             case "blue": colorRGB = "0000ff"; break;             default: colorRGB = "Invalid color"; break;         }         System.out.println(colorRGB);         }     } 

    If the pasted code is formatted incorrectly in the editor, press Alt-Shift-F to reformat.

  2. In the Files window, right-click the project's node and choose Run. You will see the output of the application, which is the RGB code for the red color. 
    You can see that the build is successful and the application works when the target platform and source format is JDK 7. 
    Output of running the project.
  3. Let's rollback to using JDK 6 and test how the application is complied with the JDK 6 compiler. 
    In the Files window, right-click the project's node and choose Properties. On the Libraries tab, set the Java Platform to JDK 1.6 and on the Sources tab, set the Source Format option to JDK 6.
    You can immediately see that the JDK6 parser does not recognize the syntax. The compilation fails because of the incompatible variable type. 
    Output of running the project.
  4. Now, let's rewrite the code using the if-then-else statement instead of switch as shown in the picture. 
    With JDK 7 being the target platform, the IDE recognizes such cases and offers you to convert them to switch. 
    Click the hint and the if-then-else construct will be automatically converted to exactly the same switch that we had before. 
    Converting the if-then-else to switch
  5. Run the application with different values.

JDK 7 Support: More Examples

To demonstrate how the IDE's Java Editor recognizes and automatically fixes code to be compliant with the JDK 7 language spec, let's use a dummy code snippet, which is meaningless but contains all the major language improvements.

When walking through this dummy code snippet and applying editor hints, you will see the following examples of how to:

  • Take advantage of automatic type inference, when the Java compiler is able to infer the type of a generic instance without the need to explicitly specify it. The so-calleddiamond operator is used to flag the type inference case.
  • Use improved exception handling or multi-catch, when one catch block can be used for several types of exceptions. 
  • Use the new syntax of resource closure statements introduced by the Automatic Resource Management feature.
  1. Replace the previous application code in the same SwitchTest.java file with the following
     package switchtest;   import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List;   public class SwitchTest {       public static void main(String[] args) throws IOException {         List list = new ArrayList();         HashMap map = new HashMap();         HashMap map2 = new HashMap();         String a = "ehlo";          try {             throw new FileNotFoundException("adasdf");         } catch (FileNotFoundException fnfo) {             fnfo.printStackTrace();         } catch (IOException ioe) {             ioe.printStackTrace();         }          FileInputStream in = null;         try {             in = new FileInputStream("foo.txt");              int k;             while ((k = in.read()) != -1) {                 System.out.write(k);             }         } finally {             if (in != null) {                 in.close();             }         }     } }  
  2. Note that the IDE displays several hints of how you can optimize your code for the JDK 7 spec. Simply, click on each hint and select the suggested action. 
    IDE's hints on conversion
  3. Finally, after you accept all the suggestions, you should receive the JDK 7 compatible code that is similar to the one shown below.
    Converted code snippet

0 comments: