Jesper Rønn-Jensen
Jesper Rønn-Jensen

Reputation: 111806

IntelliJ is it possible to add @Overrides to all methods of a particular interface?

I have created an interface, with about 30 methods and implemented in 30 classes.

I want to add @Override to each of the implementations, but i would like not to do it by hand.

How can IntelliJ help me?

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 

}

Upvotes: 52

Views: 16087

Answers (5)

For Eclipse (tested on Mars.2):

Window / Preferences (or Project Properties) Java / Compiler Error/Warnings Annotations Missing @Override annotation - Set to Error (or Warning)

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. Select all and apply the fix.

This worked for me to properly annotate a few hundreds of classes...

Upvotes: -3

user1991776
user1991776

Reputation: 580

For IntelliJ:

  1. Check if you have "Missing @Override annotation" checked inside Preferences -> Project Settings -> Inspections -> Inheritance issues.
  2. Use Analyze -> Inspect Code
  3. Find Inheritance issues -> Missing @Override annotation inside "Results for Inspection Profile" window.
  4. Apply fix "Add @Override annotation" Apply fix "Add @Override annotation"

Upvotes: 55

manocha_ak
manocha_ak

Reputation: 902

Its not eclipse or idea specfic. Its compiler specfic and what compliance you adhere to. In eclipse its 1.6 or higher. And In idea I believe its for 1.5 or higher.

In the above cases it will bechecked automatically by respective IDEs.

Upvotes: -3

Jesper Rønn-Jensen
Jesper Rønn-Jensen

Reputation: 111806

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 Add @override for tests in changed files only

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

Upvotes: -3

CrazyCoder
CrazyCoder

Reputation: 402591

Easily done with Alt+Enter intention, then press Right arrow for the sub-menu:

Fix all

Upvotes: 66

Related Questions