Bug 339337 - isLocal() in IType returns true for anonymous types
Summary: isLocal() in IType returns true for anonymous types
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.7   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.7 M7   Edit
Assignee: Jay Arthanareeswaran CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-03-09 06:41 EST by Andreas Thies CLA
Modified: 2011-04-26 10:04 EDT (History)
3 users (show)

See Also:
markus.kell.r: review+


Attachments
Proposed patch (1.19 KB, patch)
2011-03-10 13:03 EST, Jay Arthanareeswaran CLA
markus.kell.r: review-
Details | Diff
Updated patch (2.48 KB, patch)
2011-03-14 01:41 EDT, Jay Arthanareeswaran CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Andreas Thies CLA 2011-03-09 06:41:03 EST
Build Identifier: M20100909-0800

The Java Language Specification states that "A local class is a nested class (...) that is not a member of any class and that has a name." (§14.3)

Therefore IType.isLocal() should always return false for anonymous classes (because they do not have a name). But in fact, it might happen, that IType.isLocal() returns true for anonymous types.

Reproducible: Always

Steps to Reproduce:
1. Generate a Java project named "TestProject" and a Java package named testpackage and a java class file containing the following class
"class A {static void m() {Object o = new Object(){};}}"

2. Run the following code within an Eclipse plugin
IType type = (IType) JavaCore.create("=TestProject/<testpackage{A.java[A~m[");
Assert.assertTrue(type.exists());        // green
Assert.assertTrue(type.isAnonymous());   // green
Assert.assertFalse(type.isLocal());      // fail
Comment 1 Jay Arthanareeswaran CLA 2011-03-10 00:46:54 EST
Interestingly, IType#isLocal() returns true for the given test only in the case of source types. Looks like the binary types already handle this case well - look at ClassFileReader#isLocal()
Comment 2 Jay Arthanareeswaran CLA 2011-03-10 06:20:29 EST
A fix here will break the fix made to bug #85298. Now I am beginning to wonder about the relevance between IType#isLocal and JLS §14.3. 

Markus/Olivier what do you think?
Comment 3 Markus Keller CLA 2011-03-10 09:47:56 EST
I agree that we're not consistent with the JLS here, but it's too late for a breaking API change like this. The only thing we can do is document the mismatch.

The prime definition of JDT's view of the Java type system is in ITypeBinding. There, isLocal() is explicitly specified like this:

 * A local class is any nested class or enum type not declared as a member
 * of another class or interface. A local class is a subspecies of nested
 * type, and mutually exclusive with member types. Note that anonymous
 * classes are a subspecies of local classes.

Since we can't just change ITypeBinding#isLocal() now and since ITypeBinding and IType should stay in sync, nothing should be changed in code.
Comment 4 Olivier Thomann CLA 2011-03-10 09:53:27 EST
(In reply to comment #3)
> Since we can't just change ITypeBinding#isLocal() now and since ITypeBinding
> and IType should stay in sync, nothing should be changed in code.
I agree. The only thing we can do is improve the doc to make it clear what isLocal() is returning for anonymous class.
So Jay, please propose an improvement for the javadoc of the method:
org.eclipse.jdt.core.IType#isLocal(). This is the best we can do.
Comment 5 Andreas Thies CLA 2011-03-10 10:01:39 EST
(In reply to comment #3)
> The only thing we can do is document the mismatch.

From a client's perspective that would be enough. A workaround is pretty simple (just check IType#isAnonymous); the only damage for me resulted from the unexpected behavior.
Comment 6 Jay Arthanareeswaran CLA 2011-03-10 13:03:46 EST
Created attachment 190889 [details]
Proposed patch

Patch with updated javadoc for IType#isLocal()
Comment 7 Olivier Thomann CLA 2011-03-10 13:30:04 EST
Looks good enough to me.
Comment 8 Markus Keller CLA 2011-03-11 06:24:57 EST
Comment on attachment 190889 [details]
Proposed patch

(In reply to comment #6)
> Created attachment 190889 [details] [diff]

The note is unclear. "As per ..." sounds like we follow the spec. But since we don't, this should be stated explicitly ("Note: This deviates from JLS3 14.3..."). Furthermore, it's confusing that you use differing terms "anonymous type" and "anonymous inner classes" for the same thing.

And please also update ITypeBinding#isLocal().
Comment 9 Jay Arthanareeswaran CLA 2011-03-14 01:41:19 EDT
Created attachment 191080 [details]
Updated patch

Javadoc updated as according to Markus' comments.
Comment 10 Markus Keller CLA 2011-03-14 12:31:51 EDT
Perfect, thx.
Comment 11 Olivier Thomann CLA 2011-03-16 09:57:44 EDT
Closing since this has been released.
Released for 3.7M7.
Comment 12 Olivier Thomann CLA 2011-04-26 10:04:57 EDT
Verified.