Bug 423358 - Occurrence count computed incorrectly for certain anonymous inner classes
Summary: Occurrence count computed incorrectly for certain anonymous inner classes
Status: CLOSED WONTFIX
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.4   Edit
Hardware: All All
: P3 major (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords: needinfo
Depends on:
Blocks:
 
Reported: 2013-12-05 14:02 EST by Eric Bodden CLA
Modified: 2020-02-20 14:05 EST (History)
2 users (show)

See Also:


Attachments
screenshot showing duplicate numbers (112.39 KB, image/png)
2013-12-05 14:02 EST, Eric Bodden CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Eric Bodden CLA 2013-12-05 14:02:14 EST
Created attachment 238082 [details]
screenshot showing duplicate numbers

Regard the following minimal example:

public class JustProceed {	
	WebViewClient a = new WebViewClient() {		
		public void onReceivedSslError (...) {
		}		
	};
	
	WebViewClient w = new WebViewClient() {
	
	};	
}

After compilation, this will result in classes JustProceed$1 and JustProceed$2, the first of which contains the method definition, the second of one which does not - so far so good.

It turns out that in the JavaModel both anonymous inner classes will return the same name JustProceed$1, though, if we invoke getFullyQualifiedName() on the corresponding SourceType instances, see also the screenshot. For me this is a blocker because I am writing a plugin that needs to correlate source-type information with bytecode information and the mapping currently fails for anonymous inner classes.

I have found the root cause of the problem. The problem has to do with the invocations in org.eclipse.jdt.internal.core.CompilationUnitStructureRequestor.resolveDuplicates(SourceRefElement). This method looks as follows:
	int occurenceCount = this.occurenceCounts.get(handle);
	if (occurenceCount == -1)
		this.occurenceCounts.put(handle, 1);
	else {
		this.occurenceCounts.put(handle, ++occurenceCount);
		handle.occurrenceCount = occurenceCount;
	}
The first call to get() relies on types with the same name and the same parent returning the same hash code (and being equals). The problem is that this condition is incorrect in the case I describe: The parent of the anonymous inner classes is not (the same) outer class! Instead the parents are the two different field declarations! Since these are not the same but the occurenceCounts are computed in these two different scopes, they both become 1.

A fix would thus be to compute the hashCode and equality not directly based on the parent but instead based on the surrounding top-level type. This should mirror exactly what the compiler does.

I went through a lot of effort to find this out. I would appreciate a speedy fix. Thanks!
Comment 1 Dani Megert CLA 2014-06-30 06:49:59 EDT
Does it also happen with our latest release:
http://download.eclipse.org/eclipse/downloads/drops4/R-4.4-201406061215/

Can you attach a sample that is complete? We don't have access to the Android tools.
Comment 2 Eclipse Genie CLA 2020-02-20 14:05:34 EST
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.