Saturday, March 31, 2012

Seven NetBeans Hints for Modernizing Java Code

In the post Seven Indispensable NetBeans Java Hints, I talked about using NetBeans hints in general and then focused on seven hints in particular. The seven hints emphasized in that post are listed next:

  • Suspicious Method Call
  • Comparing Strings Using == or != AND String Constructor
  • Overridable Method Call in Constructor
  • .equals Incompatible Types
  • Incorrect Column Index in ResultSet
  • Cast Incompatible with instanceof
  • Generate .equals or .hashCode Method

In this post, I look focus on a different set of seven NetBeans hints that may not be as "indispensable" as the first set, but which I have found to be very useful in moving existing code and my own mindset about writing code in Java into the era of JDK 7. NetBeans 6.9 began the introduction of numerous new hints that I've become quite fond of for moving forward into improved performance and safety in JDK 7.

Suggesting EnumMap and EnumSet

Two of the hints that were introduced with NetBeans 6.9 in the "Performance" category are "Map replaceable with EnumMap" (described in NetBeans Java Hints as "Finds instantiations of Maps that can be replaced with EnumMap") and "Set replaceable with EnumSet" (described as "Finds instantiations of Sets that can be replaced with EnumSet"). I have posted before regarding the advantages of using EnumMap and EnumSet, so I find these to be two very useful hints when working with existing code. Although the Enum and associated EnumMap and EnumSet have all been around since J2SE 5, there may be code bases where these were not used because the code is older or because developers did not think of them when creating their maps or sets.

The next series of snapshots demonstrate in a static fashion how these hints identify potential situations where EnumSet and EnumMap are potentially more efficient. The first image shows an example of the hint to use EnumSet. The second through fourth images demonstrate the hint for using EnumMap along with selecting the action to apply an EnumMap and the result of that action.

JDK 7 Upgrade Hints

NetBeans 7.x offers several hints related to Java 7 syntax and language support in the "JDK 1.5 and later" category of hints. These hints provide more examples of where NetBeans hints can help bring existing Java code bases into a newer and more current version of the JDK. One important thing to note here is that NetBeans will only identify these hints if the source version associated with the NetBeans project is JDK 7 (1.7).

Two of the NetBeans hints related to JDK 7 are related to catching exceptions. Both the "Join catch sections using multicatch" hint ("Join catch sections using multicatch") and the "Use specific catch" hint ("Converts catch (Throwable) or catch (Exception) to multicatch catching the exceptions thrown by the try body.") were introduced with NetBeans 7.0.

The three screen snapshots that follow show an extremely convoluted piece of code that does demonstrate the "Join catch section using multicatch" hint. The three images show display of the hint, choosing to apply the hint, and the result of applying the hint.

Another exception handling hint in NetBeans related to JDK 7 is the "Use specific catch hint." The idea of catching a more specific exception than Exception (or the even more general Throwable) is not new to JDK 7, but this is a JDK 7-dependent hint because it places multiple specific checked exceptions in a JDK 7 multicatch when it does the conversion. In fact, the hint doesn't apply if there is only one known checked exception that could be more specific than Exception or Throwable. Runtime exceptions (unchecked exceptions) are not considered for obvious reasons (they're not checked after all!) and only the presence of multiple checked exceptions in the try clause will lead to this hint.

All of this is depicted in the following screen snapshots. The first screen snapshot depicts the hint appearing because two checked exceptions are possible in the try block. The second image proves that the presence of one checked exception is insufficient for the hint to appear (one of them is commented out). The third image shows both checked exceptions applicable again and how to select the action to take place. The fourth image depicts the results of accepting the hint's recommended action: the general Exception is changed to a multicatch with the two specific checked exceptions that might be encountered.

NetBeans 6.9 introduced the "Use switch over Strings where possible." hint ("Marks cascades of ifs which can be converted to switch over Strings."). This allows developers to more readily recognize a lengthy series of conditionals based on Strings that can be refactored to use JDK 7's support for switching on Strings. The next three screen snapshots demonstrate the providing of this hint, the ability to apply the hint, and the result of the application of the hint.

JDK 7 introduced the diamond syntax to make use of generics a little more concise. NetBeans 7.1 has introduced the hint "Can Use Diamond" (described as "Warns about places where the diamond operator in JDK 7 can be used instead of explicit type parameters") to help migrate code to use of this more concise syntax. The next three screen snapshots show how the hint appears, the action that can be taken by clicking on it, and the result of that action being taken.

The "Convert to try-with-resources" hint ("Converts try finally block to try-with-resources") introduced with NetBeans 7.0 helps developers identify situations in which the handy and safer approach of using the new try-with-resources can be applied.

The next four images depict use of the "Convert to try-with-resources" hint in NetBeans. The first screen snapshot shows that if a resource is first checked in a conditional for non-null status, the hint does not appear. The second image shows that removing the condition on the resource leads to the hint appearing. The third screen snapshot shows the prompt to apply the action associated with the hint and the fourth image shows the results of applying the hint. The example in these snapshots is adapted from the example provided in the Java Tutorial page The try-with-resources Statement. NetBeans converts the try-finally (no catch in this case) example provided in that tutorial to essentially the try-with-resources example shown in the same tutorial.

Upgrade to NetBeans 7.1 for Latest and Greatest Hint Support

I used NetBeans 7.1 and NetBeans 7.1.1 for the screen snapshots shown in this post. Although I have not covered it here, an interesting hints-related feature of NetBeans 7.1 is the ability to create custom hints.

Conclusion

This blog post has covered seven NetBeans hints that aid developers in taking advantage of newer features of Java (JDK 7 in particular). Enabling these hints (including associating the NetBeans project with JDK 7) enables developers to quickly identify pieces of legacy code that can be modernized when the compiler is upgraded to JDK 7 and can also help developers learn to write new code using these new constructs and features. The hints covered in this post are:

  • Map replaceable with EnumMap
  • Set replaceable with EnumSet
  • Join catch sections using multicatch
  • Use specific catch
  • Use switch over Strings where possible
  • Can Use Diamond
  • Convert to try-with-resources

No comments: