22

Similar to How to change "Generate Method Stub" to throw NotImplementedException in VS?, but for Eclipse instead of Visual Studio

Both NetBeans and Eclipse have a function that, if you declare a Java class to implement an interface but omit one or more methods, will automatically generate a stub method for you.

The difference is that the Eclipse version will do nothing, and return zero or null, e.g.

public String munge(String foo) {
    // TODO Auto-generated method stub
    return null;
}

The NetBeans version will throw an exception instead:

public String munge(String foo) {
    throw new UnsupportedOperationException("Not supported yet.");
}

which I prefer.

Is it possible to configure Eclipse to do this?

Community
  • 1
  • 1
finnw
  • 47,861
  • 24
  • 143
  • 221

1 Answers1

24

Go to Windows -> Preferences -> Java -> Code Style -> Code Templates. On the right you'll see "Comments" and "Code". Expand "Code" and the one you're looking for is "Method Body". Click "Edit..." and put whatever you want in there.

lycono
  • 1,094
  • 7
  • 10
tddmonkey
  • 20,798
  • 10
  • 58
  • 67
  • 7
    It's actually now called "Method Body" instead of "Code in created function stubs". If you think above should be the default in Eclipse, vote for my https://bugs.eclipse.org/bugs/show_bug.cgi?id=402347. – vorburger Mar 04 '13 at 15:37
  • As of writing, that bug ticket in the comment above is marked as RESOLVED WONTFIX, because it is possible for users to modify the template for auto-generating the method stubs per workspace or per project. By default, it will always be set to generate a TODO comment. – tom_mai78101 Feb 10 '20 at 14:30