Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 483662

Summary: Eclipse ignores explicit static import overriding previous wildcard import
Product: [Eclipse Project] JDT Reporter: Stefan Ionescu <ionescu.s.c>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: CLOSED DUPLICATE QA Contact:
Severity: enhancement    
Priority: P3 CC: stephan.herrmann
Version: 3.1   
Target Milestone: ---   
Hardware: PC   
OS: Windows 7   
Whiteboard:

Description Stefan Ionescu CLA 2015-12-04 07:11:50 EST
The following code snippet is reported with the error "The method any(Class<DetailedLocation>) is ambiguous for the type LiftSequenceStrategyTest".

import static org.hamcrest.Matchers.*;
...
import static org.mockito.Matchers.any;
...

@RunWith(MockitoJUnitRunner.class)
public class LiftSequenceStrategyTest {
... any(DetailedLocation.class) ...

Eclipse thinks this refers to the harmcrest any method instead of the mockito one. This must be wrong by eclipse as java resolves this correctly as well as intellij.
Switching the import order fixes the error in eclipse.

Am using JDK 1.7.79.

-- Configuration Details --
Product: Eclipse 4.5.1.20150917-1200 (org.eclipse.epp.package.java.product)
Installed Features:
 org.eclipse.jdt 3.11.1.v20150904-0015
Comment 1 Stephan Herrmann CLA 2015-12-05 09:04:40 EST
I'm unable to reproduce the error. I tried various versions of ecj and all accept my test program, selecting the mockito variant of "any" indeed.

Not sure whether versions of hamcrest and mockito make a difference. I happened to use:
  hamcrest-all-1.3.jar
  mockito-all-1.10.19.jar

Please try to create a self-contained example. TIA
Comment 2 Stefan Ionescu CLA 2015-12-07 05:10:21 EST
Ok, i extracted the minimal needed stuff to cause the error. Sorry it seems to differ a bit from my first assumption.

import static org.hamcrest.Matchers.*;
import static org.mockito.Matchers.*;

import org.junit.Test;

public class BugExample {

   @Test
   public void test1() {
      any( BugExample.class );
   }
}

--------------------

This causes the issue and i cannot fix it by adding "import static org.mockito.Matchers.any;" as last import to force eclipse to use mockito's "any" which it should and does for jdk and intellij.

Used versions are mockito-core-1.9.5 and hamcrest-all-1.3.
Comment 3 Stephan Herrmann CLA 2015-12-07 11:03:34 EST
A few notes before investigating in detail:

- order of imports should be irrelevant, so adding an individual static import "last* should not be different from inserting it anywhere else.

- IIRC there is no such thing as overload resolution for static methods from different classes.

What problem do you see when you do add the individual static import?
Is it the same problem as bug 442580? If so, please try if moving the static import *up* works around the issue.
Comment 4 Stefan Ionescu CLA 2015-12-08 03:59:12 EST
The problem is that the example is not compiling in eclipse but is compiling in jdk and intellij if i add the explicit last static import.

Changing the order fixes it in eclipse but breaks it for jdk and intellij. So this is no option.

If you say there is no kind of overriding of imports based on their order in java, no rules how to resolve redefinition during imports then it is strange that jdk itself seems to follow such an order.

I believe you that there might not be any defined ordering/overriding but wouldn't it be good if eclipse behaved as jdk does, like intellij?
Comment 5 Stephan Herrmann CLA 2015-12-08 05:45:42 EST
(In reply to Stefan Ionescu from comment #4)
> The problem is that the example is not compiling in eclipse but is compiling
> in jdk and intellij if i add the explicit last static import.
> 
> Changing the order fixes it in eclipse but breaks it for jdk and intellij.
> So this is no option.

Thanks, so the situation looks identical to bug 442580

And if changing the order breaks it for javac, then they may have another variant of the same bug...

> I believe you that there might not be any defined ordering/overriding but
> wouldn't it be good if eclipse behaved as jdk does, like intellij?

I'm sure there are rules for this in JLS, just they differ from normal overload resolution, IIRC. We'll bring ecj in line with JLS via bug 442580

*** This bug has been marked as a duplicate of bug 442580 ***
Comment 6 Stefan Ionescu CLA 2015-12-08 05:59:18 EST
Oh ok. Thank you and sorry for the duplicate.
Comment 7 Stephan Herrmann CLA 2015-12-08 06:18:40 EST
No problem.
But please let's keep the duplicate link. This way we'll see also your example when working on the other bug.

*** This bug has been marked as a duplicate of bug 442580 ***