简体   繁体   English

IntelliJ是否可以将@Overrrides添加到特定接口的所有方法?

[英]IntelliJ is it possible to add @Overrrides to all methods of a particular interface?

I have created an interface, with about 30 methods and implemented in 30 classes. 我创建了一个接口,有大约30种方法,并在30个类中实现。

I want to add @Override to each of the implementations, but i would like not to do it by hand. 我想在每个实现中添加@Override,但我不想手动完成。

How can IntelliJ help me? IntelliJ如何帮助我?

interface looks like this: 界面看起来像这样:

public interface PreviewObjectTests {
    void testGetName();
    void testGetType();
    //... 30 similar methods
}

implementation code: 实施代码:

public class PreviewObjectAccountTest implements PreviewObjectTests {

    //I want to add @Override here!
    @Test public void testGetName() {
        assertThat(...);
    }

    //I want to add @Override here!
    @Test public void testGetType() {
        assertThat(...);
    }

    //...30 similar methods 

}

Easily done with Alt + Enter intention, then press Right arrow for the sub-menu: 使用Alt + Enter意图轻松完成,然后按子菜单的右箭头

全部修复

For IntelliJ: 对于IntelliJ:

  1. Check if you have "Missing @Override annotation" checked inside Preferences -> Project Settings -> Inspections -> Inheritance issues. 检查是否在首选项 - >项目设置 - >检查 - >继承问题中选中了“缺少@Override注释”。
  2. Use Analyze -> Inspect Code 使用分析 - >检查代码
  3. Find Inheritance issues -> Missing @Override annotation inside "Results for Inspection Profile" window. 查找继承问题 - >在“检查配置文件的结果”窗口中缺少@Override注释。
  4. Apply fix "Add @Override annotation" 应用修复“添加@Override注释” 应用修复“添加@Override注释”

For Eclipse (tested on Mars.2): 对于Eclipse(在Mars.2上测试):

Window / Preferences (or Project Properties) Java / Compiler Error/Warnings Annotations Missing @Override annotation - Set to Error (or Warning) 窗口/首选项(或项目属性)Java /编译器错误/警告注释缺少@Override注释 - 设置为错误(或警告)

After the rebuild workspace, in the Markers view, right click on one of the errors, Quick Fix (Ctrl+1) and all the errors should appear. 在重建工作区之后,在Markers视图中,右键单击其中一个错误,Quick Fix(Ctrl + 1)并显示所有错误。 Select all and apply the fix. 全选并应用修复。

This worked for me to properly annotate a few hundreds of classes... 这对我有用,可以正确地注释几百个类......

I found a way to do it with the simple find/replace tool. 我找到了一种方法来使用简单的查找/替换工具。

  1. First i changed a method name to provoke all files implementing the interface to change 首先,我更改了一个方法名称,以激活实现要更改的接口的所有文件
  2. Then i created a simple find/replace to look only in changed files. 然后我创建了一个简单的查找/替换,只查看已更改的文件。

Intellij仅为更改的文件中的测试添加@override

This works in my particular case, because there were not really any other methods in the class files. 这适用于我的特定情况,因为类文件中实际上没有任何其他方法。 I will assume this will be a bit more crazy if i had a lot of methods in the files belonging to other interfaces 如果我在属于其他接口的文件中有很多方法,我会认为这会有点疯狂

Its not eclipse or idea specfic. 它不是日食或想法。 Its compiler specfic and what compliance you adhere to. 它的编译器特性以及您遵守的合规性。 In eclipse its 1.6 or higher. 在日食中它的1.6或更高。 And In idea I believe its for 1.5 or higher. 在想法中我相信它的1.5或更高。

In the above cases it will bechecked automatically by respective IDEs. 在上述情况下,它将由各自的IDE自动进行检查。

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

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