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 solution to my request would be the following:
Create in a plugin another “New class wizard”, by extending
org.eclipse.jdt.internal.ui.wizards.NewElementWizard(similar to theNewClassCreationWizardclass) with a page that extendsorg.eclipse.jdt.ui.wizards.NewTypeWizardPage(likeNewClassWizardPage)Override
org.eclipse.jdt.ui.wizards.NewTypeWizardPage.createTypeMembers(IType, ImportsManager, IProgressMonitor)First, call to
createInheritedMethods(like inNewClassWizardPage)Then,
type.getMethods()will give you the inherited methods from the superclass.Check if a method from the resulted array has the desired annotation in the superclass (e.g.
@Code). If it does, create the new method code, including it’s declaration:Delete the current method:
Then add the method with the custom code:
Now install the plugin, and when you create a new class with the wizard, the custom code for the methods will be automatically written there.