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.

Lii
  • 11,553
  • 8
  • 64
  • 88
user2173372
  • 483
  • 7
  • 17
  • You can search manually for references (ctrl+shift+G) on every constant. – Jeremy Grand Apr 18 '17 at 16:41
  • But I have many classes with many constants so it will be quite difficult to do on every field – user2173372 Apr 18 '17 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 ? – Jeremy Grand Apr 18 '17 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. – M. Prokhorov Apr 18 '17 at 16:55
  • Yes, but with many classes and constants it will be a very long process. – user2173372 Apr 18 '17 at 17:00
  • It might be a little easier to do this manually using keyboard shortcuts. With the cursor at an enum constant declaration: Open call hierarchy view on that constant (Ctrl+Alt+H); activate editor again (I have bound Ctrl+E for this); navigate to next constant declaration (Ctrl+Shift+Down). – Lii Apr 19 '17 at 08:59

2 Answers2

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

Lii
  • 11,553
  • 8
  • 64
  • 88
  • 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 '17 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](http://stackoverflow.com/a/2831041/452775). – Lii Apr 18 '17 at 17:56
  • Due to company constraints, can't install anything from outside – user2173372 Apr 18 '17 at 18:02
  • @user2173372 Sounds like a weird company, – Thomas Fritsch Apr 18 '17 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 '17 at 09: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.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
UweZ
  • 11
  • 1