Find self-referencing code in IntelliJ

In IntelliJ, when the code is not used anywhere, it will be grayed out. Is there any way to see that the set of classes is not used anywhere?

I have this set of classes with links to each other, so IntelliJ considers this set of classes to be used. In this case, I know that the code is useless, but it would be nice to be able to automatically detect such things. The logic for this is not surprisingly complex ... Does anyone know if this is possible in IntelliJ?

+4
source share
1 answer

This greyed out symbol simply reflects the use of the declaration in other source code files or infrastructure configuration files. A search for using a declaration cannot detect orphaned clusters of classes, as these classes are officially referenced.

There is a technique that can help here: identify some root set of entry points (methods main(), declarations web.xml, etc.) and trace all the links, effectively plotting the classes / methods used. Once the graph is complete, you can consider invisible classes as dead code. Pretty similar to what the Java garbage collector does during a collection of young people. This is quite complicated and requires resources to analyze the code on the fly, so Intellij implements it as a separate check that can be run manually.

, , :

public class Main {
    public static void main(String[] args) {
        System.out.println(new Used());
    }
}

class Used {}

class ObviouslyUnused {}

class TrickyUnused1 {
    TrickyUnused1() {
        System.out.println(new TrickyUnused2());
    }
}

class TrickyUnused2 {
    TrickyUnused2() {
        System.out.println(new TrickyUnused1());
    }
}

, ObvoiuslyUnused . " ":

enter image description here

enter image description here

, , :

enter image description here

, : , , , SPI, .. , anlisys 100 % .

+3

Source: https://habr.com/ru/post/1543288/


All Articles