Friday, July 22, 2011

Java 7 Support in Eclipse JDT (BETA) - Part III

I have already talked about adding Java 7 support to your Eclipse 3.7 or 4.1 install. Last week I also talked about the new features in JDT for Multi-catch, try-with-resources and Simplified Varargs Method Invocation. Now I will talk about Improved Type Inference for Generic Instance Creation (Diamond).

This language change allows explicit type arguments to constructors of parameterized classes to be omitted in many situations. The compiler infers the omitted type arguments based on the expected type.

The JDT compiler detects redundant specification of type arguments, which you can remove via Remove type arguments quick fix.

 This warning can be configured as shown below.
 You can also insert inferred type arguments via a quick assist.

Where possible, Content assist inserts diamond instead of explicit type arguments.

Future items: We plan to show the inferred type arguments right in the constructor hover, see bug 351048. In 3.8 we also plan to provide Remove redundant type arguments as a cleanup, see bug 351956.

Note that the type inference done by the compiler can at times be non-obvious. For example, at first glance the following two methods might appear equivalent, when in fact the one with conditional operator has an error.

An analogous example from the pre-Java 7 world.

No comments:

Post a Comment