为 Eclipse 中的接口实现添加缺少 @Override 注释的警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/224945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 13:22:20  来源:igfitidea点击:

Add warning for missing @Override annotation for interface implementation in Eclipse

eclipseeclipse-3.4jdk1.6

提问by Benno Richters

In Eclipse 3.4.1 using JDK1.6.0_10 how can I activate a warning on a missing @Overrideannotation on a method that implements an interface method?

在使用 JDK1.6.0_10 的 Eclipse 3.4.1 中,如何@Override在实现接口方法的方法上激活缺少注释的警告?

In the Preferences window this option:

在首选项窗口中,此选项:

Java > Compiler > Errors / Warnings > Annotations > Missing '@Override' annotation

Java > 编译器 > 错误/警告 > 注释 > 缺少“@Override”注释

works fine for missing annotations on methods that override a superclass method, but it does nothing for interface methods.

对于覆盖超类方法的方法上缺少注释可以正常工作,但对于接口方法没有任何作用。

采纳答案by Benno Richters

It is not possible in Eclipse 3.4.1. It is a known issue. See this bug report page for more information.

这在 Eclipse 3.4.1 中是不可能的。这是一个已知问题。有关更多信息,请参阅此错误报告页面。

回答by Benno Richters

Belay what I said in the other thread. Apparently it's not a compiler option, but a Java compatibility version issue: if your compatibility version is 5, you won't be able to annotate interface implementation methods. If your compatibility version is 6, then you will be able to.

保护我在另一个线程中所说的话。显然这不是编译器选项,而是Java兼容性版本问题:如果您的兼容性版本是5,您将无法注释接口实现方法。如果您的兼容版本是 6,那么您将能够。

However I don't think there's a way of actually forcing Eclipse to generate an error if you do not annotate each implemented method. From this page:

但是,如果您不注释每个实现的方法,我认为没有办法实际强制 Eclipse 生成错误。从这个页面

Missing '@Override' annotation: When enabled, the compiler will issue an error or a warning whenever it encounters a method overriding another implemented method, and the '@Override' annotation is missing.

缺少 '@Override' 注释:启用后,编译器将在遇到覆盖另一个已实现方法的方法时发出错误或警告,并且缺少 '@Override' 注释。

回答by Elmo R.

The compatibility setting to 1.6 instead of 1.5 worked for me on: Eclipse Java EE IDE for Web Developers.

兼容性设置为 1.6 而不是 1.5 对我有用:Eclipse Java EE IDE for Web Developers。

Build id: 20100218-1602

版本号:20100218-1602

回答by fhe

Maybe I'm missing something but interface methods are already required to be implemented by implementation classes.

也许我遗漏了一些东西,但是接口方法已经需要由实现类来实现。

The compiler will generate an error if the interface is not completely implemented.

如果接口没有完全实现,编译器会产生错误。