Bug 300734 - Extract temp misses duplicate occurrence.
Summary: Extract temp misses duplicate occurrence.
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.5   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.6 M6   Edit
Assignee: Srikanth Sankaran CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-01-25 16:02 EST by Brian Miller CLA
Modified: 2010-03-08 07:30 EST (History)
4 users (show)

See Also:
Olivier_Thomann: review+


Attachments
Patch under consideration (4.99 KB, patch)
2010-02-04 05:41 EST, Srikanth Sankaran CLA
no flags Details | Diff
Proposed fix + regression test (4.78 KB, patch)
2010-02-04 23:19 EST, Olivier Thomann CLA
no flags Details | Diff
Proposed fix + regression test (5.20 KB, patch)
2010-02-04 23:20 EST, Olivier Thomann CLA
no flags Details | Diff
Revised patch (8.10 KB, patch)
2010-02-08 01:40 EST, Srikanth Sankaran CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Brian Miller CLA 2010-01-25 16:02:53 EST
Build Identifier: 20090920-1017

class Bug {{
	final Runtime runtime = Runtime.getRuntime();
	runtime.getClass() // LINE 3
	.toString();
	runtime.getClass().hashCode();
}}

Reproducible: Always

Steps to Reproduce:
Please try to extract a temporary from the expression on LINE 3.  See that the duplicate expression in the next statement is wrongly missed.
Comment 1 Markus Keller CLA 2010-01-27 06:44:27 EST
Minimal example:

class Try {
	{
		getClass().equals(getClass());
	}
}

The two calls to Class#getClass() resolve to different bindings, which makes JdtASTMatcher think the fragments don't match.

Moving to Core, since the two bindings are not identical, but their keys are the same and isEqualTo(..) returns true. I expect identical bindings.
Comment 2 Markus Keller CLA 2010-01-27 06:46:32 EST
Note that this is only a problem for the getClass() method with it's special return type (JLS 4.3.2). Works fine for other cases e.g. this:

public class Try {
	{
		get().equals(get());
	}

	protected Class<? extends Try> get() {
		return getClass();
	}
}
Comment 3 Srikanth Sankaran CLA 2010-02-04 05:41:10 EST
Created attachment 158163 [details]
Patch under consideration
Comment 4 Olivier Thomann CLA 2010-02-04 22:44:41 EST
Looks good except that I would put the regression test inside the org.eclipse.jdt.core.tests.dom.ASTConverter15Test tests.
Comment 5 Olivier Thomann CLA 2010-02-04 23:19:19 EST
Created attachment 158258 [details]
Proposed fix + regression test

Updated patch with test moved to ASTConverter15Test
Comment 6 Olivier Thomann CLA 2010-02-04 23:20:38 EST
Created attachment 158259 [details]
Proposed fix + regression test

Same patch with updated copyright
Comment 7 Srikanth Sankaran CLA 2010-02-05 01:39:49 EST
Thanks for the review & reorganization.
Released in HEAD for 3.6M6.
Comment 8 Markus Keller CLA 2010-02-05 06:06:54 EST
Sorry, I'm a little late, but are these WeakReferences really necessary? The straightforward way to solve this would be to just add a field to TypeBinding where you can cache the special method binding.

If you don't want that for memory space reasons, I think the cache should at least be created in the lookup environment so that the space in the WeakHashMap can be reclaimed when the environment is GC'd. I think the current code also has a concurrency issues, since the unsynchronized map can be accessed from multiple threads.

I think you could even get away without a cache. After all, isn't this just another method that could be fetched from the lookup environment (and gets cached there via exisitng lookup mechanisms)?
Comment 9 Srikanth Sankaran CLA 2010-02-07 23:43:17 EST
(In reply to comment #8)
> Sorry, I'm a little late, but are these WeakReferences really necessary? The
> straightforward way to solve this would be to just add a field to TypeBinding
> where you can cache the special method binding.

This approach was considered but dismissed (for the same reasons you
identify)

> I think the current code also
> has a concurrency issues, since the unsynchronized map can be accessed from
> multiple threads.

Good point. I was less worried about the reclamation of the WeakHashMap
itself (as long as the entries are reclaimed suitably) but while I rework
this fix to address the concurrency issue, I'll also see if I can logically
group this cache with the existing others in LookupEnvironment and eliminate
any hangovers.
Comment 10 Srikanth Sankaran CLA 2010-02-08 01:40:51 EST
Created attachment 158418 [details]
Revised patch

Revised patch under test.
Comment 11 Olivier Thomann CLA 2010-02-08 10:42:29 EST
Even better. +1.
Comment 12 Srikanth Sankaran CLA 2010-02-09 00:14:54 EST
Reworked patch released to HEAD for 3.6M6.
Comment 13 Ayushman Jain CLA 2010-03-08 07:21:59 EST
verified for 3.6M6 using build I20100305-1011.
Comment 14 Jay Arthanareeswaran CLA 2010-03-08 07:30:27 EST
Verified.