Search code examples
javaintellij-ideacode-duplication

Code duplication anaysis in IntelliJ and source generation


I have three Java classes that implement an Iterable interface. Two of them have the exact same implementation for their iterator() methods, which causes IntelliJ to flag the code as duplicated.

The code duplication soft warning yielded by IntelliJ

Since I'm using Java 8, I could push the implementation as a "default" at the level of the interface, but that would require also adding private fields on the interface which really gives out more information than is necessary, even if it were possible (I'm new to Java 8 so I'm not sure whether Java 8 also allows you to declare or even initialize fields at the level of the interface). Even if I were to do that, there will be more classes implementing the common interface with different implementations for iterator().

I have found this link towards the IntelliJ documentation that says the following:

To do that, select the check box Ignore duplicated code in sources marked as generated in the inspection settings page:[...]

Unfortunately, despite the fact that this checkbox does indeed appear in my inspection settings page:

enter image description here

The problem still persists after checking that checkbox. I'm not entirely sure what "generated sources" means in the link that I posted above and I'm looking for some assistance.

For what it's worth, I'm running IntelliJ 2017.1 Ultimate Edition.


Solution

  • I used @CrazyCoder's response above and found out and ran a so-called "code analysis", as offered by IntelliJ, and found the following functionality:

    enter image description here

    Clicking on "disable inspection" does solve the problem at hand, however in general this is undesirable. I would really like it if I could somehow only keep this "weak warning" for same-source file methods. If I duplicate code across public class definitions, it's usually because I know what I'm doing, as in this case.