Bug 426988

Summary: [1.8][quick fix] "Remove explicit 'this' parameter" for incorrect locations
Product: [Eclipse Project] JDT Reporter: Noopur Gupta <noopur_gupta>
Component: UIAssignee: JDT-UI-Inbox <jdt-ui-inbox>
Status: ASSIGNED --- QA Contact:
Severity: enhancement    
Priority: P3 CC: markus.kell.r
Version: 4.4   
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard:
Bug Depends on: 427357    
Bug Blocks:    

Description Noopur Gupta CLA 2014-01-30 07:24:28 EST
We have the following problem ids in org.eclipse.jdt.core.compiler.IProblem for the cases where the explicit 'this' parameter is present at incorrect locations:

IllegalDeclarationOfThisParameter
ExplicitThisParameterNotBelow18
DisallowedExplicitThisParameter
ExplicitThisParameterNotInLambda

We can provide a quick fix "Remove explicit 'this' parameter" which will remove the 'this' parameter (along with its type use annotations, if any). 

Example: Hover over 'this' at different locations in the following code to view the error:
----------------------------------------------
package com.test.todo;

import java.util.function.Function;

public class Test {
	void m1(int i, Test this) { }
	static void m2(Test this) { }
	Runnable r= new Runnable() {
		public void run(Runnable this) {	}
	};
	Function<String, String> f= (Function this, String s) -> s;
}
----------------------------------------------
Comment 1 Noopur Gupta CLA 2014-01-30 07:26:01 EST
The quick fix can also be used as a quick assist to remove the explicit 'this' parameter if required.
Comment 2 Noopur Gupta CLA 2014-01-30 08:36:04 EST
For the cases where the invalid (to be removed) 'this' parameter is present in a method (not lambda expr), it should be present in either the parameters list of MethodDeclaration or its type should be set in the MethodDeclaration.RECEIVER_TYPE_PROPERTY.
Only then it can be removed from the AST. If it is not present anywhere in the AST then how can it be removed (see case #m2 below)?

Consider the following example:

abstract class Test2 {
	abstract void m0(Test2 this, int i);
	
	void m1(int i, Test2 this) { }
	static void m2(Test2 this, int i) { }
	
	Function<String, String> f1= (String s, Function this) -> s;
	Function<String, String> f2= (Function this, String s) -> s;
}

Here, 'this' parameter is present as given below:
- In #m0: present in RECEIVER_TYPE_PROPERTY, not present in parameters list.
- In #m1: not present in RECEIVER_TYPE_PROPERTY, present in parameters list.
- In #m2: not present in RECEIVER_TYPE_PROPERTY, not present in parameters list.
- In f1 and f2: present in parameters list of LambdaExpression.

For #m2, since it is not a valid receiver parameter, probably it should be present only in parameters list (as in #m1) and not set in RECEIVER_TYPE_PROPERTY.

Markus, please share your thoughts on:
1. Is the new quick fix required?
2. AST for 'this' in case of #m2.
3. Where to check the value of IMethodBinding.getDeclaredReceiverType() in AST view?