finnw
finnw

Reputation: 48619

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?

Upvotes: 22

Views: 15603

Answers (1)

tddmonkey
tddmonkey

Reputation: 21184

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.

Upvotes: 24

Related Questions