0

I know the traditional way in Windows to generate getter and setter of some attributes in the class: Highlighting the fields --> Right click --> source --> Generate Getters and Setters but is there anyway to create my own shortcut to do that?

What should I enter as name and pattern in this case?

enter image description here

TheBook
  • 1,698
  • 1
  • 16
  • 32

1 Answers1

0

Java editor templates aren't too powerful so the following is the best pattern I could come up with. Name / description is only for referencing the template so those can be anything.

public ${type} get${Variable}() {
    return this.${variable};
}

public void set${Variable}(${type} ${variable}) {
    this.${variable} = ${variable};
}

To use in an open java editor . . .

Ctrl+Space --> Select Name of Template (or start typing template name for autocomplete) --> Enter Variable Type --> Tab --> Enter Uppercase Variable Name --> Tab --> Enter Lowercase Variable Name --> Enter or Tab

The upper/lower case names are there to follow java naming conventions. There is no substring or default functions that I can find to process the variable text, otherwise you'd only have to enter the variable name once. I saw this answer and it suggests creating a custom function for upper-casing the variable name but it does not look trivial nor does the answer have a complete solution.


An alternative but much more time consuming method is to create a plugin (and learn eclipse plugin development) and then bind a plugin command to a key combo in Windows>Preferences under General>Keys (or General>Editors>Keys)


Finally if neither of these are suitable for implementing, you could do the minimal change of binding the "Generate Getters and Setters" command to a key combination to make that existing option slightly faster. You can go to the same Windows>Preferences under General>Keys (or General>Editors>Keys) and search for the command "Generate Getters and Setters", select Binding, and hit the key combination you want it to bind to.

Community
  • 1
  • 1
FriedSaucePots
  • 1,342
  • 10
  • 16