Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 335066 - [DOM] Wrong binding created for "this" keyword in anonymous class
Summary: [DOM] Wrong binding created for "this" keyword in anonymous class
Status: CLOSED WONTFIX
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.7   Edit
Hardware: PC Windows 7
: P3 major with 1 vote (vote)
Target Milestone: ---   Edit
Assignee: Ayushman Jain CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2011-01-21 17:39 EST by arlindolima CLA
Modified: 2020-05-15 13:20 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 arlindolima CLA 2011-01-21 17:39:18 EST
Build Identifier: M20100909-0800

According to JLS §15.8.3, the keyword "this" inside an anonymous class, should refer to the enclosing anonymous class.

In the example in "Steps to reproduce", I use the keyword "this" inside an anonymous class three times: in the initializer of an instance variable of the anonymous class, in the instance initializer of the anonymous class and inside a method of the anonymous class.
In the first two cases, the "this" binding erroneously points to the class that encloses the anonymous class, and not to the anonymous class.

Reproducible: Always

Steps to Reproduce:
1. File "ThisBug.java" that produces the error:


public class ThisBug {
	
	{
		Object o = this.getClass();
		// o -> ThisBug class
		System.out.println( o);
	}

	MyClass field = new MyClass() { // anonymous class
		Object p = this.getClass();
		// p -> Anonymous Class class, but
		// binding is created for ThisBug class
		{
			System.out.println( p);

			Object q = this.getClass();
			// q -> Anonymous Class class, but
			// binding is created for ThisBug class
			System.out.println( q);
		}
		
		void method() {
			Object r = this.getClass();
			// r -> Anonymous Class class
			// Correct binding generated
		}
	};
	
	public static void main(String[] args){
		new ThisBug();
	}
	
}


class MyClass { 
	interface InnerMyClass {
		
	}
}
Comment 1 arlindolima CLA 2011-01-21 17:52:26 EST
I forgot to add the output of my program:

class ThisBug
class ThisBug$1
class ThisBug$1

It confirms the "this" should point to the anonymous class.
Comment 2 arlindolima CLA 2011-01-22 05:41:15 EST
Another problem. When adding a type parameter to the ThisBug top-level class:

ThisBug<T> instead of ThisBug,

the "this" in the first line of the method of name "method()":

Object r = this.getClass();

Now points to a class that doesn't exist, instead of pointing to the anonymous class. This new class has a binding key similar to an anonymous class binding, but isAnonymous() returns false. It has NAME: 'new MyClass(){}' and ORIGIN: isNested isLocal. Strangely, the getErasure() of this binding, is the correct anonymous binding (is Anonymous() returns true).
Comment 3 Ayushman Jain CLA 2011-01-24 02:48:58 EST
(In reply to comment #0)
> Build Identifier: M20100909-0800
> 
> According to JLS §15.8.3, the keyword "this" inside an anonymous class, should
> refer to the enclosing anonymous class.

I dont see any clear definition of using "this" in an anonymous class in this section. You're referring to JLS 3.0, right?

Also I can see the correct binding (anonymous type) being generated in the compiler atleast.
I invoked the ASTView plugin on your code and could see the correct binding for the expression this.getClass. I was surprised that instead for the usage of 'this' inside method(), the binding corresponded to ThisBug and not the anon class.
Comment 4 arlindolima CLA 2011-01-24 04:24:06 EST
(In reply to comment #3)
> I dont see any clear definition of using "this" in an anonymous class in this
> section. You're referring to JLS 3.0, right?

I was referring to this sentence:

". . . the keyword this denotes a value that is a reference to the object for which the instance method was invoked . . ."

> Also I can see the correct binding (anonymous type) being generated in the
> compiler atleast.
> I invoked the ASTView plugin on your code . . . . I was surprised that instead
> for the usage of 'this' inside method(), the binding corresponded to ThisBug
> and not the anon class.

I was talking about the org.eclipse.jdt.core.dom API. And I'm glad you were able to reproduce the bug.
Comment 5 Olivier Thomann CLA 2011-01-24 11:27:20 EST
Ayushman, please investigate.
Comment 6 Eclipse Genie CLA 2020-05-15 13:20:51 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. As such, we're closing this bug.

If you have further information on the current state of the bug, please add it and reopen this bug. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.