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

Bug 515416

Summary: Extract variable (replace all occurrences) replaces only selected one
Product: [Eclipse Project] JDT Reporter: Andrey Loskutov <loskutov>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: CLOSED WONTFIX QA Contact:
Severity: normal    
Priority: P3 CC: jarthana, noopur_gupta, stephan.herrmann
Version: 4.7   
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard: stalebug

Description Andrey Loskutov CLA 2017-04-19 03:43:00 EDT
4.7 build I20170417-2000.

Try to extract list.size() to the variable via Ctrl+1 -> Extract variable (replace all occurrences): 

import java.util.List;
public class X {
	int hello(List<?> list) {
		if(list.size() == 1 || list.size() ==2){
			return list.size();
		}
		System.out.println(list.size());
		return list.size();
	}
}

It will always replace only one selected part, but never all occurrences!
Comment 1 Andrey Loskutov CLA 2017-04-19 03:54:32 EDT
The problem seem to be with the type parameter on the list, because extract with the code below works fine:

	int hello2(List list) {
		if(list.size() == 1 || list.size() ==2){
			return list.size();
		}
		System.out.println(list.size());
		return list.size();
	}
Comment 2 Jay Arthanareeswaran CLA 2017-04-19 06:00:50 EDT
Looks like we decide that List<?> is not same as list<?>, which may be true.

Noopur, can you please confirm if my finding is true?
Comment 3 Andrey Loskutov CLA 2017-04-19 06:03:37 EDT
(In reply to Jay Arthanareeswaran from comment #2)
> Looks like we decide that List<?> is not same as list<?>, which may be true.

I didn't get this. The variable type is same inside the method in all occurences, so whatever type it has (may be just Object), it could be extracted?
Comment 4 Stephan Herrmann CLA 2017-04-20 05:32:53 EDT
(In reply to Andrey Loskutov from comment #3)
> (In reply to Jay Arthanareeswaran from comment #2)
> > Looks like we decide that List<?> is not same as list<?>, which may be true.
> 
> I didn't get this. The variable type is same inside the method in all
> occurences, so whatever type it has (may be just Object), it could be
> extracted?

Each evaluation of an expression with a wildcarded type (here: 'list') creates a fresh capture type. This accommodates the fact that reassigning different values to the same variable could indeed result in 'list' having different types over time.
Comment 5 Andrey Loskutov CLA 2017-04-20 08:14:09 EDT
(In reply to Stephan Herrmann from comment #4)
> (In reply to Andrey Loskutov from comment #3)
> > (In reply to Jay Arthanareeswaran from comment #2)
> > > Looks like we decide that List<?> is not same as list<?>, which may be true.
> > 
> > I didn't get this. The variable type is same inside the method in all
> > occurences, so whatever type it has (may be just Object), it could be
> > extracted?
> 
> Each evaluation of an expression with a wildcarded type (here: 'list')
> creates a fresh capture type. This accommodates the fact that reassigning
> different values to the same variable could indeed result in 'list' having
> different types over time.

I still didn't get this: in the given example, the expression result type is int. Does it really matter which types (wildcard or not) are involved in the expression if the result type is same and not wildcard type?
Comment 6 Stephan Herrmann CLA 2017-04-20 08:49:52 EDT
(In reply to Andrey Loskutov from comment #5)
> (In reply to Stephan Herrmann from comment #4)
> > (In reply to Andrey Loskutov from comment #3)
> > > (In reply to Jay Arthanareeswaran from comment #2)
> > > > Looks like we decide that List<?> is not same as list<?>, which may be true.
> > > 
> > > I didn't get this. The variable type is same inside the method in all
> > > occurences, so whatever type it has (may be just Object), it could be
> > > extracted?
> > 
> > Each evaluation of an expression with a wildcarded type (here: 'list')
> > creates a fresh capture type. This accommodates the fact that reassigning
> > different values to the same variable could indeed result in 'list' having
> > different types over time.
> 
> I still didn't get this: in the given example, the expression result type is
> int. Does it really matter which types (wildcard or not) are involved in the
> expression if the result type is same and not wildcard type?

good point.

I haven't debugged this. Only tried to explain, why indeed different (incompatible) types could be involved. But I agree those probably shouldn't matter.
Comment 7 Eclipse Genie CLA 2020-03-30 20:06:11 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. As such, we're closing this bug.

If you have further information on the current state of the bug, please add it and reopen this bug. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.