Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 351048

Summary: [1.7][hovering] Show inferred type for Diamond types
Product: [Eclipse Project] JDT Reporter: Ayushman Jain <amj87.iitr>
Component: UIAssignee: Markus Keller <markus.kell.r>
Status: VERIFIED FIXED QA Contact:
Severity: enhancement    
Priority: P2 CC: daniel_megert, deepakazad, markus.kell.r, raksha.vasisht
Version: 3.7   
Target Milestone: 3.7.1   
Hardware: All   
OS: All   
Whiteboard:
Bug Depends on: 351165    
Bug Blocks:    
Attachments:
Description Flags
Fix none

Description Ayushman Jain CLA 2011-07-04 02:23:26 EDT
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>.
Comment 1 Markus Keller CLA 2011-07-05 07:05:12 EDT
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.
Comment 2 Markus Keller CLA 2011-07-05 07:10:12 EDT
Created attachment 199107 [details]
Fix
Comment 3 Markus Keller CLA 2011-08-08 02:17:46 EDT
Fixed in HEAD and R3_7_maintenance.
Comment 4 Markus Keller CLA 2011-08-10 11:06:23 EDT
Forgot to mark as fixed.
Comment 5 Dani Megert CLA 2011-08-25 03:11:44 EDT
Verified in M20110824-0800 on Linux.

>I removed the UnionType from this bug,
Filed bug 355795.