Change in javac handling of anonymous/local classes
John Spicer
jhs at edg.com
Wed Feb 27 13:22:31 PST 2013
On Feb 27, 2013, at 4:16 PM, Alex Buckley <alex.buckley at oracle.com> wrote:
> On 2/27/2013 12:33 PM, John Spicer wrote:
>> Javac 1.7 has what is a surprising difference from 1.6 with regard to this example:
>>
>> For example,
>>
>> abstract class TT {
>> void f1(String s) {}
>> void f1(int i1, int i2) {}
>> // Note: no f2
>> void f3(String s) {}
>> }
>> abstract class X1 {
>> void f1(int i) {}
>> void f2(int i) {}
>> void f3(int i) {}
>> void bar() {
>> new TT() {
>> {
>> f1(0); // 1.6 error, 1.7 accepts if TT has > 1 f1, finds X1.f1
>> f2(0); // 1.6 and 1.7 accept, find X1.f2
>> f3(0); // 1.6 and 1.7 error, finds TT.f3
>> }
>> };
>> }
>> }
>
> Let's focus solely on 'f3(0);' for now, so we can be sure about the rules:
>
> - The scope of the declaration of the method TT.f3(String) includes the body of the anonymous class declaration.
>
> - The scope of the declaration of the method X1.f3(int) includes the body of the anonymous class declaration.
>
> - The declaration of the method X1.f3(int) does not shadow anything. Nothing called f3 is declared in an enclosing scope at the point where X1.f3(int) is declared.
>
> - Therefore, the declarations of both methods are visible at 'f3(0);'.
>
> - Therefore, I would expect overload resolution to choose X1.f3(int).
>
> - I suspect javac 1.6 and 1.7 choose TT.f3(String) because of a different belief about shadowing. Maurizio, please comment on what's visible at 'f3(0);'.
>
It seems to me that the issue is not whether members of TT shadow something, but whether members of the anonymous class shadow or hide something.
Clearly, within TT any reference to f1 or f3 can only refer to members of TT.
But within the anonymous class, the members of TT are inherited and as a result, hide the members of the enclosing class X1.
In other words, at the call of f3(0), I think that only the inherited f3 should be visible.
John.
More information about the compiler-dev
mailing list