views:

52

answers:

1

I have several classes like in the following example:

abstract class AClass {
  boolean validate(){
    return true;
  }
}

When another class extends AClass, I do it like this:

class BClass extends AClass {
  @Override
  boolean validate() {
    if (!super.validate()) {
        return false;
    }
    // TODO validate
    return true;
  }
}

Is there an eclipse plugin that generates that code for me when I create a new class from the menu(File>New>Class)?
I'm thinking to use an annotation

@Target(ElementType.METHOD)
@interface Code {
    String content();
}

And add it to the method:

abstract class AClass {
    @Code(content = "\tif (!super.validate()) {\r\n" 
        + "\t\treturn false;\r\n" 
        + "\t}\r\n" 
        + "\t// TODO validate\r\n"
        + "\treturn true;")
    boolean validate() {
        return true;
    }
}

The plugin should look for the annotation and generate the code in the newly created class.

A: 

Under Windows -> Preferences, you can type in Java code templates.

You'll find code templates under Java -> Code Style -> Code Templates

On Eclipse 3.5, the pattern for class body is blank. You can add your own pattern.

Here's the pattern for new Java files, to give you an idea what a pattern looks like.

${filecomment}
${package_declaration}

${typecomment}
${type_declaration}
Gilbert Le Blanc
I want that the generated code to be for *some* methods only, not for *all methods*.
True Soft