Community
Participate
Working Groups
BETA_JAVA7 With Java7, some new constructs have been introduced where the type of the expression is not obvious to a reader. These are: Diamond and UnionType. It will be good to have a feature which allows me to hover on one of these constructs and inspect the inferred type. This will help not just the reader of the code to inspect what type is being used, but also the writer to make sure that he is not accidentally using different semantics than what he intends. For example (taken from bug 349336) public class X<T> { T field1; public X(T param){ field1 = param; } public static void main(String[] args) { X.testFunction(new X<Object>("hello").getField()); //prints 2 X.testFunction(new X<>("hello").getField()); // prints 1 } public static void testFunction(String param){ System.out.println(1); } public static void testFunction(Object param){ System.out.println(2); } public T getField(){ return field1; } } the X.testFunction(new X..) method call will behave differently with and without type arguments being specified. A hover on the new X<>("hello") declaration will immediately alert the writer that the inferred type is X<String> and not X<Object>.
In comment 0, the inferred type can be seen in the hover of X in new X<>("hello") , since the type variable T in the constructor invocation has been replaced with String. To make the method overloading resolution clear, we already have Mark Occurrences, Open Declaration, and the Javadoc hover on invocations of testFunction(..). But I agree that it would be nice to see the used type arguments right in the constructor hover, e.g. for the new ArrayList<>() invocations here: package diamond; import java.util.*; class Diamond { void foo() { List<String> a= new ArrayList<>(); List<Object> b= new ArrayList<>().subList(1, 2); List<String> c= new ArrayList<String>(); c.subList(1, 2); c.add(""); List<String> d= true ? new ArrayList<>() : new LinkedList<>(); } } Filed bug 351165 for API to fetch the type arguments. I removed the UnionType from this bug, since it is a separate issue that can be tackled in 3.8 when we fix bug 99931.
Created attachment 199107 [details] Fix
Fixed in HEAD and R3_7_maintenance.
Forgot to mark as fixed.
Verified in M20110824-0800 on Linux. >I removed the UnionType from this bug, Filed bug 355795.