Writing a Java adder analyzer, method code template for Eclipse

Too much time when I am faced with the need to write sum / delete methods for lists:

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

If I understood correctly, then Eclipse templates can support automatic completion ... for example, if I have:

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

I would like the template to add this method to Auto-Complete from Eclipse and automatically complete the whole method:

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

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

Any idea how to accomplish something like this ... maybe give me some reading material pertaining to this type of subject.

** UPDATE **

Well, this is what I still have:

 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):

At 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); } 

At 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); } 

At the method level, and the assembly reference is a local variable of the method, I get the correct syntax, but the add method is inside another method.

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

What does it mean that I don’t get the link at the field level, how can I get 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.

+4
source share
3 answers

I 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 using a method and cut it out at the class level ... :)

+2
source

You can read about the available template variables in the Eclipse help . You must be a template module based on existing templates in Eclipse, see "Viewing Templates" in Eclipse.

+2
source

I would suggest using Source > Generate delegate methods , so you do not need to write a template for each method that you want to generate.

Assign a hotkey for the Generate delegate methods , whenever you declare an object

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

Press the hotkey, Eclipse will detect the currently selected object, then it will bring up a list of available methods for you.

+2
source

Source: https://habr.com/ru/post/1399891/


All Articles