javac -Xlint:overrides not working
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.
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"/>
The unchecked option is being followed, but the overrides option is being ignored. I also tried sep开发者_C百科arating the two Xlint options into two separate <compilerarg> elements, to no avail. Am I misunderstanding what this option does?
One note: this is JDK6 on MacOSX (10.6). Could I be running into a OSX-specific bug?
I believe you are misunderstanding the Xlint:overrides behaviour.
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.
EDIT: 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.
The documentation on Oracle's website doesn't even mention the Xlint:overrides option so I'm guessing it's not implemented.
Xlint:overrides is more subtle than @Override. @Override (as Cameron Skinner points out) will cause an error to be emitted by the compiler if it annotates a method which does not in fact override another method. The Xlint:overrides option, however, produces a warning if one has marked a method with a varargs parameter to override another which doesn't have a varargs parameter but instead uses an array. A good explanation is found here:
http://marxsoftware.blogspot.com/2010/10/javacs-xlint-options.html
加载中,请稍侯......
精彩评论