Community
Participate
Working Groups
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 { } }
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.
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).
(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.
(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.
Ayushman, please investigate.
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.