2

Eclipse provides a feature to open the declarations of fields, invoked methods etc.

(F3 or Right click => Open declaration or Ctrl + click on the invoked method)

However, in the case of enum methods this feature doesn't function, e.g. MyEnum.values(); is called somewhere in my code and trying to open the declaration of values() with the combinations denoted above doesn't work obviously.

Why doesn't Eclipse open the declaration of such enum methods?

Juvanis
  • 25,802
  • 5
  • 69
  • 87

1 Answers1

3

Why doesn't Eclipse open the declaration of such enum methods?

Because they're not declared in source code at all. They're automatically supplied by the compiler - where would you expect to be taken? Ctrl-clicking on MyEnum (rather than the values() method) should open the enum with no problems though.

From section 8.9.3 of the JLS:

The members of an enum type E are all of the following:

  • ...
  • The following implicitly declared methods:

    /* javadoc... */
    public static E[] values();
    
    /* javadoc... */
    public static E valueOf(String name);
    

Note that the normal "go to declaration" techniques should work for any methods which genuinely exist in source code.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Thanks for the answer. Of course, MyEnum declaration gets open with no problems. I didn't know that enum methods aren't declared in source code. Can you also point to the other compiler-supplied code in Java other than enums please? – Juvanis Apr 25 '14 at 13:20
  • @Juvanis: There isn't a lot - parameterless constructors spring to mind, and nested classes can involve some synthesis in order to propagate the enclosing member, give access to private members etc. – Jon Skeet Apr 25 '14 at 13:23
  • So we have a public API without Javadoc? Ugly. IDEs should at least try to mixin the Javadocs at JLS 8.9 into that synthetized methods. – Robert Hume Aug 30 '20 at 08:43