Bug 493131 - [refactoring] Support refactoring to generate generic utility methods
Summary: [refactoring] Support refactoring to generate generic utility methods
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.6   Edit
Hardware: All All
: P3 enhancement with 1 vote (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-05-06 07:06 EDT by Lukas Eder CLA
Modified: 2016-05-06 08:08 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Lukas Eder CLA 2016-05-06 07:06:16 EDT
I run into this issue quite frequently, and then I have to resort to a lot of manual work, which I imagine might be possible for Eclipse to refactor automatically. For instance, I have the following method, which doesn't compile:

------------------------------------------
    public static void swap(List<?> list) {
        list.set(0, list.set(1, list.get(0)));
        System.out.println(list);
    }
------------------------------------------

It doesn't compile because of the JLS's wildcard capturing specification (which is a bit unfortunate). The "?" type returned from "list.get(0)" is the same "?" as the one from the "list" parameter, but it is a different "?" from the "?" that "list.set(1, ?)" takes as an argument, even if we're always operating on the same parameter "list".

Now, it would be great if I could select some code and refactor the code into a method that introduces a type variable just for that. The result would look like this:

------------------------------------------
    public static void swap(List<?> list) {
        refactored(list);
        System.out.println(list);
    }

    private static <T> void refactored(List<T> list) {
        list.set(0, list.set(1, list.get(0)));
    }
------------------------------------------

Of course, I could manually introduce this type variable, but let's assume that "swap" is some public API that I don't want to modify. In particular, I don't want the type variable to leak into the public API.
Comment 1 Jay Arthanareeswaran CLA 2016-05-06 08:04:05 EDT
I don't know how complicated this is going to be, but moving to UI for consideration.
Comment 2 Lukas Eder CLA 2016-05-06 08:08:20 EDT
Thanks for moving. Indeed, it might get a bit complicated. The simplest feature description that I can imagine is, though:

1. When refactoring/extracting a method...
2. ... check if the selection captures any wildcards
3. ... check if those wildcards are really the "same thing", from an "intuitive perspective", i.e. if they can bind to a single type variable.