Get rid of all @Override errors in Eclipse

来源:互联网 发布:网络维护考题 编辑:程序博客网 时间:2024/04/28 19:02

Eclipse里import 一个可运行的 project经常遇到的一个问题:报一大堆错,当解决了引入jar包、sdk问题后,发现好多error都有一个共同的要求:“remove @Override Annotation”。

How to get rid of all these "remove @Override Annotation" errors?

Is there a way to just comment out all the @Overrides that turn out to be Eclipse errors that prevent your app from building/running?

Solutions:

(1)思路一:There are already @Override annotations in the source code, now I am getting compile errors on them.

不是SDK的问题(SDK引入正确,排除),难道是JDK?

project上右击-->Properties-->Java Compiler, 显示当前我的Compiler compliance level为1.5,修改为1.6,警报解除。

问题1:我明明安装的jdk1.7,compiler compliance level为什么是1.5呢?

compiler compliance level的含义说明:设置编译级别。 

Eclipse compiler compliance level为较低版本,只是让编译器相信你的代码是兼容较低版本的,在编译时生成的bytecode(class)兼容较低版本。这样设置与你写代码时引用的JDK是没关系的,也就是说你在写代码时仍可以引用较高版本的API(这样就可能导致错误)。

设置compiler compliance level为较低版本,这样的好处是当别人使用了较低版本的Jdk时也可以引用你写的编译后的代码。它可以保证编译后的class文件的版本一致性。但是,如果你的代码里面(java source)里面调用了较高版本jdk的API.那么即使设置了compiler compliance level为较低版本,在较低版本的JDK上运行你的代码也会报错。所以建议在写代码时引用的JDK,要跟你compiler compliance level设置的版本,是一致。

问题2:为什么修改为1.6就不再警报了呢?
You need at least jdk1.5 to use those annotations on methods derived from classes.
You need at least jdk1.6 to use those annotations on methods derived from interfaces.


(2)思路二:修改Eclipse警报级别

Window-->Preferences-->Java-->Compiler-->Errors/Warnings-->Annotations

Under there, check that the"Missing '@Override' annotation is set to "Ignore" or "Warning" and not "Error"。

Edit:I wanted to add that you can get Eclipse to automatically add @Override annotations on files that you save in the Save Actions。

Window-->Preferences-->Java-->Editor-->Save Actions

There is a checkbox for "Additional Actions" and if you open the "Configure" window you can select the "Missing Code" tab and select "Add Missing Annotations" where you can select which annotations to automatically add.


(3)思路三:查找-->替换,其实只要remove @Override Annotation就可以

Ctrl+H to bring up the Search dialog

Go to the "File Search" tab.

Enter "@Override" in "Containing text" and "*.java" in "File name patterns".

Click "Replace...", then "OK", to remove all instances of "@Override".  


原创粉丝点击