Community
Participate
Working Groups
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.
I don't know how complicated this is going to be, but moving to UI for consideration.
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.