Bug 335845 - [compiler] compiler wrongly suggests to add a static qualifier to a method
Summary: [compiler] compiler wrongly suggests to add a static qualifier to a method
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.7   Edit
Hardware: PC Windows 7
: P3 normal (vote)
Target Milestone: 3.7 M6   Edit
Assignee: Ayushman Jain CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-01-31 08:54 EST by Philippe Coucaud CLA
Modified: 2011-03-07 11:13 EST (History)
2 users (show)

See Also:
satyam.kandula: review+


Attachments
proposed fix v1.0 + regression tests (10.92 KB, patch)
2011-02-01 07:07 EST, Ayushman Jain CLA
no flags Details | Diff
proposed fix v1.1 + regression tests (12.74 KB, patch)
2011-02-02 09:50 EST, Ayushman Jain CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Philippe Coucaud CLA 2011-01-31 08:54:15 EST
Build Identifier: I20110127-2034 (3.7M5)

public class Test {
   private class Bar {}

   private void foo() {
      new Bar();
   }
}

compiler emits a warning (or an error depending on the compiler settings):
The method foo() from the type Test can be declared as static

However, adding a static qualifier produces a compiler error because no enclosing instance of type Test is accessible.

Reproducible: Always

Steps to Reproduce:
Might be related to bug 335780 (although it is about a potential match and not a definitive one like this one).
Comment 1 Ayushman Jain CLA 2011-02-01 07:07:31 EST
Created attachment 188036 [details]
proposed fix v1.0 + regression tests

Allocation of a (non-static) member typewithout an enclosing instance of a parent type is done via AllocationExpression, while with an enclosing instance is done via QualifiedAllocationExpression. This patch adds a check in AllocationExpression#analyseCode(..) to reset the static flag for a method if an allocation expression is encountered for a non-static member type.
Comment 2 Ayushman Jain CLA 2011-02-01 09:18:57 EST
All tests pass. Satyam, please review. Thanks!
Comment 3 Satyam Kandula CLA 2011-02-02 08:26:15 EST
Ayush, The following test fails with the patch.
public class SuperTest {
 class Bar {}
}

public class Test extends SuperTest{
   private void foo() {
      new Bar();
   }
}
Comment 4 Ayushman Jain CLA 2011-02-02 09:50:39 EST
Created attachment 188147 [details]
proposed fix v1.1 + regression tests

Here's the updated fix. I was using the wrong bits - ASTNode.IsMemberType bits, whereas I should've used TagBits.IsMemberType, since i'm checking the binding bits and not the AST node bits.

Added another test to take care of this.
Comment 5 Satyam Kandula CLA 2011-02-03 01:56:47 EST
+1. The patch looks good for me.
Comment 6 Ayushman Jain CLA 2011-02-03 08:31:46 EST
Released in HEAD for 3.7M6
Comment 7 Olivier Thomann CLA 2011-03-07 11:13:06 EST
Verified for 3.7M6.