Bug 426988 - [1.8][quick fix] "Remove explicit 'this' parameter" for incorrect locations
Summary: [1.8][quick fix] "Remove explicit 'this' parameter" for incorrect locations
Status: ASSIGNED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.4   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on: 427357
Blocks:
  Show dependency tree
 
Reported: 2014-01-30 07:24 EST by Noopur Gupta CLA
Modified: 2014-04-22 08:34 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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?