why does this code compile with eclipse compiler but not with javac (maven)
Asked Answered
H

1

8

There are a bunch of questions like this. I went through most of them but none actually but I couldn't come up with any answer:

I have a weird problem in one of my GWT/GWTP classes.
The class compiles fine with the Eclipse compiler but fails with the javac compiler (Maven).

//additional imports
import com.gwtplatform.mvp.client.PresenterWidget;
import com.gwtplatform.mvp.client.View;

public class MyPresenter extends PresenterWidget<MyPresenter.MyView> {

    public interface MyView extends View {


    }

    some code
}

When i try to compile with maven I get following error:

cannot find symbol symbol: class View

View refers to the View interface in the com.gwtplatform.mvp.client package.

I have other classes which look the same and work fine.
The weird thing is that if I change the order of the imports or I specify the exact package of the View interface it compiles without any problems in maven.
To be specific I moved the import for com.gwtplatform.mvp.client.View

import com.gwtplatform.mvp.client.View;
//additional imports
import com.gwtplatform.mvp.client.PresenterWidget;

I had a similar problem some time ago with cyclic inheritance problem between classes which refer to inner classes (worked in eclipse but didn't in javac). However I am not sure if that is the same problem.

Holmes answered 19/1, 2012 at 15:14 Comment(2)
Not sure on the root cause of your problems, but inner interfaces are not very pretty and can lead to confusions about where your dependencies are coming from.Prater
You are right and I assume that moving the interface out of the class would fix it (tough I haven't verified it). I was just curious what might be the explanation for this weird behavior (moving around imports causes the code to compile or not)Slaw
F
9

Eclipse's compiler is actually a different compiler than the javac compiler. Sometimes they drift apart in behavior, usually they reconcile quickly.

This was very noticable when Java's generics came out. There were cases where eclipse either found fault with a generics directive that javac would permit or javac found fault with generics that eclipse would permit (can't remember which way it drifted apart, too long ago). In either case, javac is more likely to be the correct implementation.

In your case, you pollute the namespace with your generics reference to an inner class. Odds are that eclipse is reaching for the "View" in a different priority order than javac. Odds are excellent that either Javac implements the order as it is specified in the Java language guidelines, or the Java guidelines have not yet pronounced the "one true order" of resolving conflicting like-named classes. Normally this is not a problem because it is not permissible to use the same non-fully-qualified name in Java twice; however, with internal classes the specifications can be sort of "worked around".

I would make the

public interface MyView extends View {


}

bind to just one view (don't know if com.gwtplatform.mvp.client.View or MyPresenter.View is the right one) by making the name explicit.

public interface MyView extends MyPresenter.View {


}

or

public interface MyView extends com.gwtplatform.mvp.client.View {


}

That way you don't fall victim to the interface "binding" to the wrong type in a compiler-dependent manner.

Fertility answered 19/1, 2012 at 15:48 Comment(1)
Wow. I got stuck on this issue during the conversion of a Java project from 'nothing-just-hit-compile-in-Eclipse' to Maven. The Java compiler complains about a missing symbol that isn't missing, so finding the cause of this issue is tricky. Referring to inner-inner classes from an outer class as Inner.Inner works, but Inner with an import for Outer.Inner.Inner only works in Eclipse, not from Maven. Thanks!Benedetta

© 2022 - 2024 — McMap. All rights reserved.