仅按子包进口按需报告 [英] Import-on-demand declaration with subpackages only

查看:134
本文介绍了仅按子包进口按需报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相关:如何编译import pack。*与ant / javac,当没有这样的类?



假设我们有给定的包结构

  parent 
|
--- a
--- b

其中包 parent 只包含两个子包 a b (没有类是在父项的包下



代码 import parent。* 位于一个包含不同于 parent 的包,并使用Maven编译(即 javac )抛出一个编译时错误。错误是:


包父不存在


我研究了有关这种情况的Java语言规范(实际导入没有类型的按需导入的声明)。 第7.5.2段关于import-on-demand声明的JLS 似乎没有指定这种行为。



这是一个 javac的错误?我不是问如何规避编译错误。我想知道为什么 javac 抛出一个错误,因为我在JLS中找不到任何引用;导致我相信这可能是一个错误。



我使用JDK 1.4,1.6,1.7和1.8进行测试,错误是一样的。



作为附注,Eclipse中没有使用相同的代码编译错误(使用Eclipse Indigo,Juno,Luna和Mars进行测试)。



注意:当mavenizing只依赖于Eclipse编译器的(旧的)现有项目时,我遇到这种情况。我花了一段时间来确定这是编译错误的根本原因。

解决方案

我想你是什么正在寻找的是 section7.4.3


一个包是可观察的,当且仅当以下两者之一:




  • 包含该包的声明的编译单元是
    observable(§7.3)。


  • A包的子包是可以观察到的。




包java,java.lang和java.io总是可以观察到的。


而在 7.5.2


这是一个如果命名的包或类型不可访问,则编译时错误(§6.6)。


所以如果没有g在一个包中它不存在,并且该bug实际上在Eclipse编译器的一边;它在LATER中留下的非常旧的错误报告中进行了描述状态,直到最终该状态被弃用,并将其移动到WONT_FIX。如果您认为这是值得修复的话,请随时重新打开(现在JLS已经在此领域得到澄清)


Related : How can I compile "import pack.*" with ant/javac, when there are no such classes?

Suppose we have the given package structure

parent
|
---a
---b

where the package parent only contains the two subpackges a and b (no class is under the package parent).

The code import parent.*, situated in a package other than parent, compiled with Maven (i.e. javac) throws a compile-time error. The error is:

package parent does not exist

I looked into the Java Language Specification about such a case (import-on-demand declaration where no types are actually imported). Paragraph 7.5.2 of the JLS about import-on-demand declaration does not seem to specify this behavior.

Is this a bug of javac? I am not asking how to circumvent the compile error. I would like to know why javac throws an error because I cannot find any reference in the JLS that it should; leading me to believe that this might be a bug.

I tested with JDK 1.4, 1.6, 1.7 and 1.8, the error is the same.

As a side note, there is no compile error with the same code in Eclipse (tested with Eclipse Indigo, Juno, Luna and Mars).

NB: I encountered this behaviour when "mavenizing" an (old) existing project that only relied on the Eclipse compiler. It took me a while to identify that this was the root cause of the compile error I was getting.

解决方案

I think what you are looking for is section7.4.3

A package is observable if and only if either:

  • A compilation unit containing a declaration of the package is observable (§7.3).

  • A subpackage of the package is observable.

The packages java, java.lang, and java.io are always observable.

And in 7.5.2

It is a compile-time error if the named package or type is not accessible (§6.6).

So if nothing is in a package it does not exists and the bug is actually on the side of the Eclipse compiler; it's described in a very old bug report that was left in "LATER" state until eventually that state was deprecated and it was moved to WONT_FIX instead. Feel free to re-open it if you think it's worth fixing (now that the JLS is clarified in this area)

这篇关于仅按子包进口按需报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆