Sunday, November 19, 2006

Bugs in Eclipse Java compiler (part II)

Version: 3.2.1; Build id: M20060921-0945

Try this:

package iface;

interface DefaultInterface {
String INVISIBLE_CONST = "Invisible string";
, this:

package iface;

public interface PublicInterface extends DefaultInterface {
String VISIBLE_CONST = "Visible String";
and this:

package test;

import static iface.PublicInterface.INVISIBLE_CONST;
import static iface.PublicInterface.VISIBLE_CONST;
import iface.PublicInterface;

public class Test {
public static void main(String[] args) {

Of course Eclipse will compile three classes above without any warning, but when you try to compile this with Sun's compiler (javac 1.5.0_05 in my case - this is the command: javac iface\*.java test\*.java) you will get such error message:

test\ INVISIBLE_CONST in iface.DefaultInterface
is defined in an inaccessible class or interface
1 error

Malicious line is static import of const string from interface which is invisible from within test package:

import static iface.PublicInterface.INVISIBLE_CONST;

Although this const belongs to PublicInterface it is not defined there.

It looks like Java bug not Eclipse bug. Why can I access constant from inaccessible interface and cannot import it statically at the same time? It's nonsense - I should be able neither import it statically nor access it through accessible interface.
Having known this bug it is possible to expose all methods and constants from inaccessible/package-default interfaces just by extending them by public interfaces.

PS. (2008-11-24) This is already fixed - it was a Sun's Java compiler problem (and mine) - everything that is defined in an interface is public (there is no default scope).

No comments: