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

Bug 217984

Summary: [quick assist] Wrong type inferred by assign to new local variable
Product: [Eclipse Project] JDT Reporter: KOVÁCS, István <ikovacs>
Component: UIAssignee: Markus Keller <markus.kell.r>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: ikovacs, manju656, markus.kell.r, martinae, philippe_mulet
Version: 3.3.1   
Target Milestone: 4.4 M7   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Bug Depends on: 218645    
Bug Blocks:    

Description KOVÁCS, István CLA 2008-02-06 07:29:27 EST
If type parameter is ?, quickfix ignores bounds placed on it. Below it is known that T is a subtype of Number, therefore ArrayList<T>.get() returns a Number, yet the quickfix offers the type Object. The code compiles with both Eclipse and javac (1.6.0_03).

Version: 3.3.0
Build id: I20070621-1340

import java.util.ArrayList;

public class NumberThing<T extends Number> extends ArrayList<T> {
	void test() {
		NumberThing<?> questionmarkThing = new NumberThing<Double>();
		// produced by 'Assign statement to new local variable'
		Object object = questionmarkThing.get(0);
		// although this is valid, too
		Number n = questionmarkThing.get(0);
	}
}
Comment 1 KOVÁCS, István CLA 2008-02-06 08:03:07 EST
Forgot to include in the original report - if you have a method in NumberThing:
	T m1() {
		return null;
	}

Invoking the assign to new local variable quickfix on
questionmarkThing.m1();
gives you
Object m1 = questionmarkThing.m1();
instead of
Number m1 = questionmarkThing.m1();
Comment 2 Philipe Mulet CLA 2008-02-06 09:55:29 EST
Martin - is this you or us ?
Comment 3 Philipe Mulet CLA 2008-02-12 08:03:14 EST
Could be an issue related to a missing capture.
Comment 4 Markus Keller CLA 2008-02-12 11:36:18 EST
Filed bug 218645 for a problem with ITypeBinding#getTypeBounds(). The fix for this bug will go into Bindings#normalizeTypeBinding(..) and #normalizeForDeclarationUse(..).

Need to check other clients of these methods. Maybe we have to keep the old methods and add new utility methods for some of the callers.
Comment 5 Markus Keller CLA 2014-03-13 14:32:48 EDT
This becomes more and more important with all the type inference that's used by Java 8's lambda expressions.

Fixed with http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/commit/?id=bde76c7ffed680bfda8eaca694124e585f592522


Need to add tests for all the get(0) expressions here:

class Gen<E extends List<String> & RandomAccess> extends ArrayList<E> {
    void foo() {
        Gen<?> g = new Gen<>();
        g.get(0);
        Gen<? extends Cloneable> ge = new Gen<>();
        ge.get(0);
        Gen<? super Vector<String>> gs = new Gen<>();
        gs.get(0);
    }
}
Comment 6 Martin Mathew CLA 2014-03-17 23:54:29 EDT
Tests released to BETA_JAVA8 and master with http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/commit/?id=e5ad6393fc711135c738e24711150f7b451e535d
Comment 7 Markus Keller CLA 2014-03-18 07:30:51 EDT
(In reply to Manju Mathew from comment #6)
> Tests released to BETA_JAVA8 and master with

This has been reverted (added to wrong test class), and can be re-released to master once the Java 8 swirl is over.
Comment 8 Martin Mathew CLA 2014-04-11 01:43:59 EDT
Tests added to AssistQuickFixTest and is released to master with : http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/commit/?id=82b657350491d4b7f81199528d86896b169f1b17