简体   繁体   English

为什么Eclipse会在接口方法上抱怨@Override?

[英]Why does Eclipse complain about @Override on interface methods?

I have an existing project that uses @Override on methods that override interface methods, rather than superclass methods. 我有一个现有的项目,它使用@Override覆盖接口方法的方法,而不是超类方法。 I cannot alter this in code, but I would like Eclpse to stop complaining about the annotation, as I can still build with Maven. 我不能在代码中改变它,但我希望Eclpse停止抱怨注释,因为我仍然可以用Maven构建。

How would I go about disabling this error? 我该如何禁用此错误?

Note: Due to project requirements, I need to compile for Java 1.5. 注意:由于项目要求,我需要编译Java 1.5。

Using the @Override annotation on methods that implement those declared by an interface is only valid from Java 6 onward. 对实现接口声明的方法的方法使用@Override注释仅从Java 6开始有效。 It's an error in Java 5. 这是Java 5中的错误。

Make sure that your IDE projects are setup to use a Java 6 JRE, and that the "source compatibility" is set to 1.6 or greater: 确保您的IDE项目设置为使用Java 6 JRE,并且“源兼容性”设置为1.6或更高:

  1. Open the Window > Preferences dialog 打开Window> Preferences对话框
  2. Browse to Java > Compiler. 浏览Java>编译器。
  3. There, set the "Compiler compliance level" to 1.6. 在那里,将“编译器合规级别”设置为1.6。

Remember that Eclipse can override these global settings for a specific project, so check those too. 请记住,Eclipse可以覆盖特定项目的这些全局设置,因此请检查这些设置。


Update: 更新:

The error under Java 5 isn't just with Eclipse; Java 5下的错误不仅仅是Eclipse; using javac directly from the command line will give you the same error. 直接从命令行使用javac会给你同样的错误。 It is not valid Java 5 source code. 它不是有效的Java 5源代码。

However, you can specify the -target 1.5 option to JDK 6's javac , which will produce a Java 5 version class file from the Java 6 source code. 但是,您可以为JDK 6的javac指定-target 1.5选项,该选项将从Java 6源代码生成Java 5版本类文件。

Do as follows: 做如下:

Project -> Properties -> java compiler -> 项目 - >属性 - > java编译器 - >

  • Enable project specific settings - 'yes' 启用项目特定设置 - '是'
  • Compiler compliance - 1.6 编译器合规性 - 1.6
  • generated class files and source compatibility - 1.5 生成的类文件和源兼容性 - 1.5

Check also if the project has facet. 检查项目是否有方面。 The java version may be overriden there. 可以在那里覆盖java版本。

Project specific settings may be enabled. 可以启用项目特定设置。 Select your project Project > Properties > Java Compiler , uncheck the Enable project specific settings or change Jdk 1.6 and above not forgetting the corresponding JRE . 选择项目Project > Properties > Java Compileruncheck Enable project specific settings或更改Jdk 1.6及更高版本,不要忘记相应的JRE
Incase it does not work, remove your project from eclipse, delete .settings folders , .project , .classpath files. 如果它不起作用,从eclipse中删除你的项目,删除.settings folders.project.classpath文件。 clean and build the project, import it back into eclipse and then reset your Java compiler. Clean and build your project clean and build the project, import it back into eclipse and then reset your Java compiler. Clean and build your project and eclipse. clean and build the project, import it back into eclipse and then reset your Java compiler. Clean and build your project和日食。 It worked for me 它对我有用

You could change the compiler settings to accept Java 6 syntax but generate Java 5 output (as I remember). 您可以更改编译器设置以接受Java 6语法,但生成Java 5输出(我记得)。 And set the "Generated class files compatibility" a bit lower if needed by your runtime. 如果运行时需要,请将“生成的类文件兼容性”设置得稍低一些。 Update : I checked Eclipse, but it complains if I set source compatibility to 1.6 and class compatibility to 1.5. 更新 :我检查了Eclipse,但是如果我将源兼容性设置为1.6并且类兼容性为1.5,则会产生抱怨。 If 1.6 is not allowed I usually manually comment out the offending @Override annotations in the source (which doesn't help your case). 如果不允许1.6,我通常会手动注释掉源中有问题的@Override注释(这对你的情况没有帮助)。

Update2: If you do only manual build, you could write a small program which copies the original project into a new one, strips @Override annotations from the java sources and you just hit Clean project in Eclipse. Update2:如果你只进行手动构建,你可以编写一个小程序,将原始项目复制到一个新项目中,从java源中删除@Override注释,然后点击Eclipse中的Clean项目。

Use Eclipse to search and replace (remove) all instances of "@Override". 使用Eclipse搜索并替换(删除)“@Override”的所有实例。 Then add back the non-interface overrides using "Clean Up". 然后使用“清理”添加回非接口覆盖。

Steps: 脚步:

  1. Select the projects or folders containing your source files. 选择包含源文件的项目或文件夹。
  2. Go to "Search > Search..." (Ctrl-H) to bring up the Search dialog. 转到“搜索>搜索...”(Ctrl-H)以显示“搜索”对话框。
  3. Go to the "File Search" tab. 转到“文件搜索”选项卡。
  4. Enter "@Override" in "Containing text" and "*.java" in "File name patterns". 在“包含文本”中输入“@Override”,在“文件名模式”中输入“* .java”。 Click "Replace...", then "OK", to remove all instances of "@Override". 单击“替换...”,然后单击“确定”,以删除“@Override”的所有实例。
  5. Go to "Window > Preferences > Java > Code Style > Clean Up" and create a new profile. 转到“窗口>首选项> Java>代码样式>清理”并创建新的配置文件。
  6. Edit the profile, and uncheck everything except "Missing Code > Add missing Annotations > @Override". 编辑配置文件,并取消选中除“缺少代码>添加缺失注释> @Override”之外的所有内容。 Make sure "Implementations of interface methods" is unchecked. 确保未选中“接口方法的实现”。
  7. Select the projects or folders containing your source files. 选择包含源文件的项目或文件夹。
  8. Select "Source > Clean Up..." (Alt+Shift+s, then u), then "Finish" to add back the non-interface overrides. 选择“Source> Clean Up ...”(Alt + Shift + s,然后是u),然后选择“Finish”以添加非接口覆盖。

您还可以尝试使用Retroweaver从Java6类创建Java5版本。

我理解你的问题,把你的jdk从jdk更改为greaterthan 1.5

By configuring that the IDE projects are setup to use a Java 6 JRE or above sometimes does not remove the eclipse error. 通过配置IDE项目设置为使用Java 6 JRE或更高版本,有时不会删除eclipse错误。 For me a restart of the Eclipe IDE helped. 对我来说, 重启 Eclipe IDE有所帮助。

Even after changing the compiler compliance setting to 1.6 or 1.7 from windows tab, then prefernces, then java, then compiler and setting the compiler compliance, I was still having this issue. 即使将编译器合规性设置从Windows选项卡更改为1.6或1.7,然后更喜欢,然后是java,然后编译器并设置编译器合规性,我仍然遇到此问题。 The idea is that we need to go the the project folder, right click, Java and set compiler compliance to 1.6 or higer. 我们的想法是,我们需要转到项目文件夹,右键单击,Java并将编译器符合性设置为1.6或更高。 This worked for me. 这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM