3

I have many enum classes in my project which have many constants. Is there a way to find all the constants which are not used anywhere automatically in all the classes in Eclipse?

For example:

public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}

public enum Color {
    RED, BLUE,GREEN, YELLOW
}

public static void main(String[] args) {
    System.out.println(Day.FRIDAY);
    System.out.println(Color.GREEN);
}

I want to find all the constants which are not used in my project automatically. So here all days except FRIDAY and all colors except GREEN should be removed.

Note: Can't use third party plugins for Eclipse.

6
  • You can search manually for references (ctrl+shift+G) on every constant. Apr 18, 2017 at 16:41
  • But I have many classes with many constants so it will be quite difficult to do on every field Apr 18, 2017 at 16:43
  • I know, that why I didn't post that as an answer. I don't have any viable answer for that. Maybe using a code analyzer like Sonar ? Apr 18, 2017 at 16:50
  • Are you absolutely sure none of your clients depend on your enum values though? If that is the case, in addition to Jeremy suggestion, I can suggest to remove all values from enum, and then add them back until project compiles again. Apr 18, 2017 at 16:55
  • Yes, but with many classes and constants it will be a very long process. Apr 18, 2017 at 17:00

2 Answers 2

1

Eclipse doesn't do cross-file analysis like this by default, it only warns for unused fields in the same file.

But there are plug-ins that do cross-file analysis.

The Unnecessary Code Detector is such a plug-in.

Disclaimer: I haven't used this tool myself, but it looks very promising. The homepage says it can detect unused enum constants.

Screen shot:

Unnecessary Code Detector screen shot

5
  • Then I think you're out of luck. If this is a big problem it might be possible to download a new copy of Eclipse, import your project there and then install the Unnecessary Code Detector.
    – Lii
    Apr 18, 2017 at 17:04
  • @user2173372: You maybe can find a way to install a third party plug-in even if your Eclipse instance is read-only. Perhaps by using an external dropin directory, as described in this answer.
    – Lii
    Apr 18, 2017 at 17:56
  • Due to company constraints, can't install anything from outside Apr 18, 2017 at 18:02
  • @user2173372 Sounds like a weird company, Apr 18, 2017 at 19:57
  • @user2173372: Maybe there is a loophole in the policy: You don't have to actually "install" a new Eclipse. You just download it and run it, there is no "installation" step!
    – Lii
    Apr 20, 2017 at 9:10
1

Open one enum in "Package Explorer" and click down until you see the constants of this enum. Mark all constants (inside the Package Explorer!) by clicking the first constant, then hold SHIFT key and click the last constant. Now, right click on the marked constants and choose "Open Call Hierarchy Ctrl+Alt+H".

A "Call Hierarchy" window should open with all marked enums and their use. Enums without call hierarchy information are not used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.