4

There are too many time I encounter the need to write an adder/remover methods for lists:

public void addSomething(Something something){
    somethings.add(something);
}

public void removeSomething(Something something){
    somethings.remove(something);
}

If I got it right, then Eclipse templates can support auto complete... for example, if I have a:

Vector<Something> somethings=new Vector<Something>();

I would like the template to add the method to the Auto-Complete of Eclipse, and would auto complete the entire method:

public void addSomething(Something something){
    somethings.add(something);
}

when I type "add" + (Ctrl + Space)...

Any idea how to accomplish something like this... perhaps point me to a reading material specific to this sort of subject.

** UPDATE **

Well, this is what I have so far:

public final void add${type:elemType(collection)}(${type} ${varName:newName(type)}) {
    ${collection}.add(${varName}); 
}

Here is the problem though, If I type "add" + (Ctrl + Space):

In the class level, and the collection is declared as a field, I get an empty variable as a collection.

public final void addtype(type type) {
    collection.add(type);
}

In the method level, and the collection is declared as a field, I get an empty variable as a collection.

public final void addtype(type type) {
    collection.add(type);
}

In the method level, and the collection reference is a local variable of the method I get the correct syntax, but the add method is inside the other method.

public void method(...) {
    public final void addSomething(Something something) {
        this.somethings.add(something);
    }
}

Which means I don't get field level reference, how can I obtain it?

** UPDATE 2 **

This also gives the same result:

public final void add${type:elemType(collection)}(${type} ${varName:newName(type)}) {
    ${collection:field(java.util.Collection)}.add(${varName}); 
}

public final void remove${type:elemType(collection)}(${type} ${varName:newName(type)}) {
    ${collection:field(java.util.Collection)}.remove(${varName}); 
}

Thanks,

Adam.

TacB0sS
  • 10,106
  • 12
  • 75
  • 118

3 Answers3

2

I've finally used the following:

public final void add${type:elemType(collection)}(${type} ${varName:newName(type)}) {
    ${collection:field(java.util.Collection)}.add(${varName}); 
}

public final void remove${type:elemType(collection)}(${type} ${varName:newName(type)}) {
    ${collection:field(java.util.Collection)}.remove(${varName}); 
}

I create this with in a method, and cut it to the class level... :)

TacB0sS
  • 10,106
  • 12
  • 75
  • 118
2

You can read about available template variables in Eclipse help. You should be fashion a template based on the existing templates in Eclipse, see 'Templates view' in Eclipse.

Deepak Azad
  • 7,903
  • 2
  • 34
  • 49
2

I would suggest you use Source > Generate delegate methods, so you don't have to write template for every method you want to generate.

Assign a hot key for Generate delegate methods, whenever you declare a object

List<Integer> integer = new ArrayList<Integer>();| <- your cursor

Press the hot key, Eclipse will detect current selected object then come up a list of available methods for you.

Rangi Lin
  • 9,303
  • 6
  • 45
  • 71
  • Good one my friend, but not what I want... I want something that would do 100% of the work, not 70%. :) Thanks, see my update. – TacB0sS Mar 07 '12 at 11:41
  • I also want a different name "addSomething(Something s)", not only add(Something s); – TacB0sS Mar 07 '12 at 12:19