Bug 71761 - [import rewrite] ImportRewrite should let me add explicit import to existing on demand import
Summary: [import rewrite] ImportRewrite should let me add explicit import to existing ...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.0   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 4.5 M6   Edit
Assignee: John Glassmyer CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 313036 62719 71707 430303
  Show dependency tree
 
Reported: 2004-08-11 05:45 EDT by Markus Keller CLA
Modified: 2015-03-19 05:25 EDT (History)
9 users (show)

See Also:


Attachments
Test case patch (1.91 KB, patch)
2012-03-29 07:01 EDT, Satyam Kandula CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Markus Keller CLA 2004-08-11 05:45:45 EDT
ImportRewrite is too smart for me. In order to fix 71707, I need to add an
explicit import p.A to a CU with an existing import p.* (to avoid hiding of
references to "A" in an existing CU "pack.User" when a new type "A" is added to
package "pack").

setFindAmbiguosImports() and setFilterImplicitImports() don't allow me to
configure this behavior - the added import p.A is always swallowed when p.*
already exists. setFindAmbiguosImports(true) doesn't work in my use case, since
the ambiguity is only to be introduced by the refactoring.
Comment 1 Dani Megert CLA 2011-05-02 06:41:11 EDT
We should look at this during 3.8. Paste is also not working as expected and therefore the additional search to do the conflict resolution - which costs time (see e.g. bug 343837) - is useless.
Comment 2 Dani Megert CLA 2011-05-02 07:23:03 EDT
setFindAmbiguosImports(boolean) also needs to get better Javadoc.
Comment 3 Srikanth Sankaran CLA 2012-03-20 11:33:22 EDT
If you plan to include a fix for this in 3.8 M7, please adjust the
target suitably, so it becomes easier to track.
Comment 4 Satyam Kandula CLA 2012-03-29 06:59:43 EDT
Actually this seems to work fine in JDT/Core.
ImportRewrite doesn't expose the API setFindAmbiguosImports(), but always does 'find ambiguous imports'. It does seem to work fine now.
Comment 5 Satyam Kandula CLA 2012-03-29 07:01:00 EDT
Created attachment 213326 [details]
Test case patch

Test patch to confirm that problem mentioned in comment 0 works fine now.
Comment 6 Markus Keller CLA 2012-03-29 16:02:12 EDT
The problem is that pack1.A doesn't exist in our use cases. At the time we call 'imports.addImport("pack2.A");', the conflict with pack1.A is not yet there. But after the refactoring is executed, there will be a pack1.A, so the explicit 'import pack2.A;' will be necessary.

To reproduce in your test case, comment out the line 'pack1.createCompilationUnit("A.java", ...'.

To solve the problem, we somehow need to inject knowledge about the soon-to-exist "pack1.A" into ImportRewriteAnalyzer#evaluateStarImportConflicts(..). At the end of this method, we have to iterate over the foundTypes to check whether one of the simpleNames is going to be available in any of the other packages.

I first thought we could use an ImportRewriteContext to let the client decide this, but then I realized that the context is used to configure behavior on addImport(..), but it's not used when evaluating star imports.

I think we need new API in ImportRewrite to solve this, e.g. a callback parameter for rewriteImports(..) that tells whether a type is found or not.
Comment 7 Markus Keller CLA 2012-03-30 05:58:13 EDT
> I think we need new API in ImportRewrite to solve this, e.g. a callback
> parameter for rewriteImports(..) that tells whether a type is found or not.

Or we could store this information when adding imports, e.g. by having the ImportRewriteContext return a new RES_NAME_UNKNOWN_NEEDS_EXPLICIT_IMPORT which prevents the given type from being star-imported.
Comment 8 Satyam Kandula CLA 2012-03-30 06:13:29 EDT
(In reply to comment #7)
> > I think we need new API in ImportRewrite to solve this, e.g. a callback
> > parameter for rewriteImports(..) that tells whether a type is found or not.
> 
> Or we could store this information when adding imports, e.g. by having the
> ImportRewriteContext return a new RES_NAME_UNKNOWN_NEEDS_EXPLICIT_IMPORT which
> prevents the given type from being star-imported.
Yes, this should be good approach. Considering that we are past the API freeze, do you want this in 3.8?
Comment 9 Markus Keller CLA 2012-03-30 08:23:45 EDT
Let's defer this. Stability of import rewrite is paramount, and the blocking bugs didn't get much attention and were both filed by (former) JDT committers.
Comment 10 Jay Arthanareeswaran CLA 2013-03-05 00:52:44 EST
Markus, I am thinking of moving this out of 4.3. Do you have any objections?
Comment 11 Markus Keller CLA 2013-03-05 07:01:51 EST
Not a top priority, moving out of 4.3.
Comment 12 John Glassmyer CLA 2014-06-06 13:27:06 EDT
I'm adding this functionality to ImportRewriteAnalyzer as part of my work on it, and adding the new constant ImportRewriteContext.RES_NAME_UNKNOWN_NEEDS_EXPLICIT_IMPORT that Markus suggested.
Comment 13 Dani Megert CLA 2014-06-07 08:35:38 EDT
(In reply to John Glassmyer from comment #12)
> I'm adding this functionality to ImportRewriteAnalyzer as part of my work on
> it, and adding the new constant
> ImportRewriteContext.RES_NAME_UNKNOWN_NEEDS_EXPLICIT_IMPORT that Markus
> suggested.

So, can we assign this bug to you John?
Comment 14 John Glassmyer CLA 2014-06-09 00:45:50 EDT
(In reply to Dani Megert from comment #13)
> So, can we assign this bug to you John?

Yes.
Comment 15 Markus Keller CLA 2015-03-05 14:54:28 EST
Fixed via bug 430303.
Comment 16 shankha banerjee CLA 2015-03-19 05:24:49 EDT
Verified for 4.5 M6 with build I20150316-2000.