问题描述:

In Eclipse Luna, I want to change the content of the autogenerated methods, so I went to Window->Preferences->Java->Code Style->Code Templates->Code->Method body and I saw there this declaration:

// ${todo} Auto-generated method stub

${body_statement}

Is it possible to to change the ${body_statement} content?

Edit: @Duncan - I don't want my generated methods to return null but I want them to throw an exception that the method is not implemented. The reason why I want to change the ${body_statement} is because I want to change all occurrences by one edit and I don't want to go through all templates and inspect them one by one.

网友答案:

Just delete the invocation of ${body_statement} in your template

Here is my Method Body template which adds a TODO and an exception should the method be called:

// ${todo} Implement ${enclosing_type}.${enclosing_method}
throw new RuntimeException("Unimplemented Method    ${enclosing_type}.${enclosing_method} invoked.");

Which when invokded after writing

int foo = doSomething();

Generates:

private int doSomething() {
    // TODO Implement ScaledFraction.doSomething
    throw new RuntimeException("Unimplemented Method ScaledFraction.doSomething invoked.");

}
网友答案:

${body_statement} is a "variable". Click on "Edit..." at the right side of the Code Templates list to edit a code template and use "Insert Variable..." to see a list of available variables.

The ${body_statement} variable is actually empty for new methods. If you want to provide some default-code for each new method, you can simple add that text above the ${body_statement}.

Adding real code below the variable in that template will not work, since ${body_statement} will be replaced by a return statement in some cases.

相关阅读:
Top