2012-03-10 105 views
5

对于这个源代码软件包和类之间的名称冲突。 Eclipse或javac中的错误?

enter image description here

... Eclipse的报告以下错误:

Only a type can be imported. pkg.a resolves to a package

...而太阳javac编译它的罚款。

类似的情况,如果我尝试完全限定类pkg.a这样的:

enter image description here

Eclipse的报告...

pkg.a cannot be resolved to a type

...而太阳javac编译它的罚款。


看起来Eclipse似乎将标识符解释为类名称上的包,而javac则完全相反。那么,是Eclipse还是javac中的错误?

(A参考语言规范显然是一个利好。)

回答

1

这是一个造型约定进行授课开始以大写字母和包以小写字母的名称;那会解决你的问题。

+0

你读过这个问题吗? (提示,找粗体文字:) – aioobe 2012-03-10 11:57:39

+0

我的确看过这个问题;我认为你的问题是那些只会让你的生活更加艰难的事情,因为遵循一个造型惯例就会避免它。 – Tom 2012-03-10 12:40:13

3

这绝对是一个Eclipse的bug:

6.4.2. Obscuring

A simple name may occur in contexts where it may potentially be interpreted as the name of a variable, a type, or a package. In these situations, the rules of §6.5 specify that a variable will be chosen in preference to a type, and that a type will be chosen in preference to a package. Thus, it is may sometimes be impossible to refer to a visible type or package declaration via its simple name. We say that such a declaration is obscured.

的明确含义是,它是合法的类和包具有相同的名称。否则,JLS会说一个模糊一个包的类是非法的......并不是说该类被优先使用。


话虽如此,您已经进入这种情况的唯一原因是您已选择忽略Java的命名惯例。不要期望太多的同情......

+0

你知道这个bug是否已经被报告?在我的情况下,我依赖于包含类(com.foo.Bar)和一些与类名冲突的文件夹(com/foo/Bar/boo.xml)的库(JAR)。 不要认为有任何违反命名约定...仍然Eclipse无法编译。 – 2016-09-07 07:44:13

+0

已报告但已关闭:https://bugs.eclipse.org/bugs/show_bug.cgi?id = 117182 – 2016-09-07 07:52:51