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

Bug 167468

Summary: [quick fix] Quickfix behavior for generic static methods is incomplete
Product: [Eclipse Project] JDT Reporter: Luke Hutchison <luke.hutch>
Component: UIAssignee: JDT-UI-Inbox <jdt-ui-inbox>
Status: CLOSED FIXED QA Contact:
Severity: normal    
Priority: P3    
Version: 3.2.1   
Target Milestone: ---   
Hardware: PC   
OS: Linux   
Whiteboard:

Description Luke Hutchison CLA 2006-12-11 14:12:40 EST
Consider this class:

public class Tmp {
	public static void test(int a) {
		test(a, a);   // error here
	}
}

Quickfix on the commented line offers the choices:
  - Remove argument to match "test(int)"
  - Change method "test(int)": add argument "int" 
  - Create method "test(int,int)"
  - Rename in file

This is correct.  Choosing "Create method" and then adding the following:

	private static void test(int a, int a2) {
		test(a, a, a);  // new error here
	}

Now trying quick fix on the new error line, I get one remove and one change method option for each version of test(), and one Create Method option.  This is also correct.

However the same options don't apply for the following:

public class Tmp {
	public static <T> void test(T a) {
		test(a, a);
	}
}

Specifically I don't get the "Create method test(T, T)" option.

Manually creating this method:

	public static <T> void test(T a, T b) {
		test(a, a, a);  // new error here
	}

I also don't get the "Create method test(T, T, T)" option here.  But this has other problems.  It only gives one set of "Add parameter" and "Remove parameter" options, rather than two.  Also it is arbitrary as to which version of test() is listed as the option that you can add parameters to or remove them from.  (It seems that it is whatever version of the method that most closely matches the call to the non-existent method.  In some cases it will match test(T) and in other cases test(T,T), depending on what the actual types are, for more complex examples than what is given here.)
Comment 1 Luke Hutchison CLA 2006-12-11 14:21:39 EST
*** Bug 167467 has been marked as a duplicate of this bug. ***
Comment 2 Martin Aeschlimann CLA 2006-12-13 09:28:27 EST
The problem is that your T is a method type parameter, not visible outside the scope of that method.
I that case we decide to not suggest to create a method, as things get complicated: The new method also needs a type parameter, using the same boundries as the other method.
Not sure if we should invest more there. Is this a common scenario?
Comment 3 Luke Hutchison CLA 2006-12-13 09:38:16 EST
I don't think it's that complicated -- the new static method can't see non-static type parameters of the class, and can't even see the static type parameters of other static methods inside the class -- so all it needs to do is copy any of the type parameters across to the new method that are actually used in any of the parameters in the call.  (e.g. if the call to the new method doesn't use T, then the declaration of the method doesn't need the parameter T).

It is not a highly common scenario but will become more common as everybody starts genericizing their methods (after all it's just as common in a generic context as the creation of a static method in a non-generic context).  And the current behavior is not consistent with the normal behavior of Eclipse in creating non-existent declarations.

Comment 4 Luke Hutchison CLA 2018-09-14 06:06:38 EDT
"Quick Fix > Add Method" is now present for the generic case, but there's a new bug: the type parameter is not added to the new method (bug #539067).