javac -Xlint:覆盖不起作用 [英] javac -Xlint:overrides not working
问题描述
当我有一个类覆盖超类方法而不指定 @Override 注释时,我试图让我的 java 构建失败.
I'm trying to get my java build to fail when I have a class that overrides a superclass method without specifying the @Override annotation.
构建是通过 ant 完成的,我已将以下元素添加到我的 <javac>
任务中:
The build is being done via ant, and I've added the following elements to my <javac>
task:
<compilerarg value="-Werror"/>
<compilerarg value="-Xlint:unchecked,overrides"/>
遵循了 unchecked
选项,但忽略了 overrides
选项.我还尝试将两个 Xlint
选项分成两个单独的
元素,但无济于事.我误解了这个选项的作用吗?
The unchecked
option is being followed, but the overrides
option is being ignored. I also tried separating the two Xlint
options into two separate <compilerarg>
elements, to no avail. Am I misunderstanding what this option does?
注意:这是 MacOSX (10.6) 上的 JDK6.我会遇到 OSX 特定的错误吗?
One note: this is JDK6 on MacOSX (10.6). Could I be running into a OSX-specific bug?
推荐答案
我相信您误解了 Xlint:overrides
行为.
I believe you are misunderstanding the Xlint:overrides
behaviour.
据我所知,启用此检查会导致编译器在遇到用 @Override
注释的方法实际上并未覆盖超类方法时发出警告(或可能是错误).但是,它不会检查所有重写的方法是否都被正确注释.
To my knowledge, enabling this check will cause the compiler to emit warnings (or maybe errors) when it encounters a method annotated with @Override
that does not actually override a superclass method. It does not, however, check that all overridden methods are annotated correctly.
刚刚测试过.当您在不覆盖超类方法的方法上指定 @Override
时,无论是否使用 Xlint
选项,编译器都会发出错误.
Just tested it. The compiler will emit an error when you specify @Override
on a method that does not override a superclass method, with or without the Xlint
option.
Oracle 网站 上的文档没有甚至没有提到 Xlint:overrides 选项,所以我猜它没有实现.
The documentation on Oracle's website doesn't even mention the Xlint:overrides option so I'm guessing it's not implemented.
这篇关于javac -Xlint:覆盖不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!