Community
Participate
Working Groups
This probably is a regression caused by the fix for bug 376550. However, as it erring on the other side of 'not' reporting, this is minor. ### package pkg1; import java.util.ArrayList; import java.util.Collection; public class CanBeStatic { private static Object o = new Object(); public final Collection<Object> go() { return new ArrayList<Object>() { { add(o); } }; } } ### go() can be static in this particular case but it is not getting reported. Note that the field 'o' is static here.
It's actually not the access to 'o' that marks the method as cannot-be-static, but the receiver in "add(o)". To check just replace "add(o)" with "add(null)" and still the compiler thinks that go cannot be static.
Created attachment 215768 [details] test & tentative fix This sketch marks how that problem could be solved.
While digging deeper into this (during review of bug 376550): To make things clearer for future readers I suggest to rename the argument of BlockScope.resetDeclaringClassMethodStaticFlag(TypeBinding declaringClass): If I understand correctly, this argument signals the type of which an enclosing instance is required. With a name like "enclosingInstanceType" it should be clear that MessageSend indeed has to pass "this.actualReceiverType", not "this.binding.declaringClass".
Here's a variant of ProblemTypeAndMethodTest.test376550_5a(): public class X { int i1 = 1; public void foo(){ class Local{ int i2 = 1; } class Local2 extends Local { void method2() { Local2.this.i2 = 1; } } } } The indirection Local2 -> Local shows that identity tests inside resetDeclaringClassMethodStaticFlag() aren't sufficient, but we probably need to check isCompatibleWith(). The above test does not find that foo() can be static because Local != Local2.
Stephan, assigning to you since you have a preliminary patch in place. :)
Will try and get this into 3.8.1
Let us target only critical/serious issues for back port. This qualifies for 4.3 M1.
Moving out to 4.3M2 to accommodate other pressing bugs.
Test & fix have been released for 4.3 M2 via commit a5b7766b7751acbd9316d0da24707b07ce3a52af
Verified for 4.3 M2 using Build id: I20120917-0800