![](/img/trans.png)
[英]Compilation with javac fails, with eclipse-compiler it works
[英]Eclipse fails where javac and IDEA succeed
请考虑以下自包含示例:
package bloopers;
import java.lang.annotation.Annotation;
public final class Blooper5
{
interface Converter<T,F>
{
T convert( F from );
}
interface Identifier<T>
{
}
static class ConvertingIdentifier<F,T> implements Identifier<F>
{
ConvertingIdentifier( Converter<T,F> converter )
{
}
}
static final class AnnotationIdentifier
{
Identifier<Annotation> I1 = new ConvertingIdentifier<>(
a -> a.annotationType() );
Identifier<Annotation> I2 = new ConvertingIdentifier<>(
Annotation::annotationType ); //<-- ERROR
Identifier<Annotation> I3 = new ConvertingIdentifier<>(
(Converter<Class<? extends Annotation>,Annotation>)
Annotation::annotationType );
}
}
上面的代码在下面编译得很好:
javac
。 IntelliJ IDEA
配置为使用javac
编译器。 但它无法编译以下内容:
Eclipse
IntelliJ IDEA
配置为使用Eclipse
编译器。 Eclipse无法编译标有<-- ERROR
,给出以下消息:
The constructor Blooper5.ConvertingIdentifier<Annotation,Class<capture#5-of ? extends Annotation>>(Blooper5.Converter<Class<? extends Annotation>,Annotation>) is undefined
不可否认,这段代码确实推动了编译器的泛型参数类型推理功能,但是,我仍然想知道差异是什么,无论多小。
如果有人设法看到我看不到的错误,我的方法有些暴露:
我用javac编译的命令是"c:\\Program Files\\Java\\jdk1.8.0_40\\bin\\javac" Blooper5.java
。
我有IntelliJ IDEA的14.1版。 在Project Structure/SDKs
我只有“1.8”指向C:\\Program Files\\Java\\jdk1.8.0_40
,在Project Structure/Modules
,特定模块配置为使用“Project SDK(1.8)”列出为1.8 (java version "1.8.0_40")
。
至于Eclipse,我使用Eclipse for RCP and RAP Developers - Version: Luna Release (4.4.0) - Build id: 20140612-0600
。 在Preferences/Java/Installed JREs
我只有jdk1.8.0_40,这是默认值。 在Execution Environments
它还被检查为“JavaSE-1.8”的“兼容JRE”。 在我的Project/Properties/Java Build Path/Libraries
,“JRE系统库”是[jdk1.8.0_40]
。
更值得注意的事实:
这不仅仅是我; 它也失败了一个同事(非常相似)的eclipse安装。
IntelliJ IDEA的说,lambda表达式a -> a.annotationType()
可以用一个方法引用来代替,但如果要求这样做,它不会将其转换为Annotation::annotationType
; 相反,它将它转换为(Converter<Class<? extends Annotation>, Annotation>) Annotation:: annotationType
。
那么,问题是:
是什么导致Eclipse和其他人之间的这些差异,以及可以采取哪些措施来消除这些差异?
(显然,目标是消除不幸发生的情况,即一个开发人员提交无法在另一个开发人员的IDE上编译的代码。)
编辑:当我最初发布这个问题时,我认为使用Eclipse
编译器的IDEA编译也很好,但我错了。 事实证明,通过选择Eclipse
编译器,IDEA可能无法编译上述代码。 不过,问题是为什么eclipse和javac之间存在差异。
“为什么存在差异”的答案是直截了当的,但也许并不十分令人满意:因为编译器存在错误,并且对于非常复杂的语言规范的解释更加开放。 确定它是否是javac或Eclipse中的错误是一项艰巨的任务; 我已经看到这样的差异最终被双重声明,有时候是Eclipse编译器的bug,有时候是javac bug。 这种决心,特别是涉及泛型和新语言特征(如lambdas)时,可能会变得相当乏味和神秘。 例如,看看这个被证明是javac bug的那个,但确实揭示了Eclipse编译器中的一个相关问题: https : //bugs.eclipse.org/bugs/show_bug.cgi?id = 456459
最好的办法就是像我一样将它报告为Eclipse bug,看看Eclipse编译器团队是否可以/将跟踪它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.