How find all unused classes in Intellij Idea?

后端 未结 4 611
孤城傲影
孤城傲影 2020-12-02 05:57

There is an inspection \"Unused declaration\" which can find all unused code in Intellij Idea. (see this question) But I want to find all unused classes, not

4条回答
  •  盖世英雄少女心
    2020-12-02 06:26

    I don't think this is doable. I suspect this feature is intentionally left out of IDEs because it can't be used safely the way that other "remove unused XXX" refactorings can.

    The unused declarations IDEA (and AFAIK, NetBeans) looks for are for private members and local variables: things that are not accessible, even dynamically, from outside that class or scope. (Well, at least without doing things with Reflection or JVM hacking that you're not supposed to.) No matter what outside code does with your library, it won't cause those things to be used, because their scope is limited and the IDE can see all of it. The compiler can determine this by looking at just your code.

    For classes, even if they don't have public access, they can be referenced dynamically with Class.forName(), and this actually happens in live code. So even if they're not apparently used within the code of your project, they might be used depending on what you or external code using your library runs. So the IDE can't guarantee that removing those classes won't change externally observable behavior.

    Which is why I think IDEA just doesn't provide this behavior: it might give users false expectations of safety, and removing them is not a safe refactoring.

提交回复
热议问题