Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-generate stub methods that throw in eclipse

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?

like image 916
finnw Avatar asked Jul 07 '09 13:07

finnw


1 Answers

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.

like image 101
tddmonkey Avatar answered Nov 07 '22 02:11

tddmonkey