Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 516372 - [extract method] Duplicate snippet with non-conflicting return value should be extracted
Summary: [extract method] Duplicate snippet with non-conflicting return value should b...
Status: CLOSED WONTFIX
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.7   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2017-05-09 11:42 EDT by Noopur Gupta CLA
Modified: 2020-03-02 11:47 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Noopur Gupta CLA 2017-05-09 11:42:38 EDT
package p;

public class Test {
	public static void main(String[] args) {
		for (int i = 0; i < 1; i++) {
			int idx = 0;
			for (int j = 0; j < 3; j++) {
				idx++;
				System.out.println(idx);
			}
			// for-loop to extract:
			for (int j = 0; j < 3; j++) {
				idx++;
				System.out.println(idx);
			}
		}
	}
}

Extracting the second for loop does not consider the first for loop as a valid duplicate as the first loop should return an 'int' whereas the second for loop returns 'void'.

This can be improved by creating an extracted method that returns 'int':

public class Test {
	public static void main(String[] args) {
		for (int i = 0; i < 1; i++) {
			int idx = 0;
			idx = extracted(idx);
			idx = extracted(idx);
		}
	}

	private static int extracted(int idx) {
		for (int j = 0; j < 3; j++) {
			idx++;
			System.out.println(idx);
		}
		return idx;
	}
}

See bug 479559 comment #9:
> For the example in comment 0, it would actually be nice to have the
> refactoring find the duplicate code. If you add another
> System.out.println(idx); after to for-loop to extract, or if you extract the
> first for-loop, the refactoring already works fine and uses the return value
> of the extracted method to update the idx variable. a() and b() in the
> snippet from comment 6 are the same case.
> 
> Implementing that is a bit trickier and should probably left as a separate
> enhancement request: In case the extracted snippet returns void but a
> structural duplicate returns a value, we could create an extracted method
> that returns that value. That would also mean the return type of the
> extracted method would have to change depending on whether duplicates should
> be extracted or not. 

It should also consider the case where multiple duplicates are present and have different return values. See bug 479559 comment #6 attachment 265525 [details] for example. In this case, we can either extract only the selected snippet returning 'void' without any duplicates (the current behavior) or, choose a return value (random or one being returned the max no. of times) from the duplicate snippets to create the extracted method.
Comment 1 Eclipse Genie CLA 2020-03-02 11:47:19 EST
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.