简体   繁体   中英

Can I customize lombok to writing code for each variable in class similar to getters and setters?

I have same set of code that I should write for each variable that I declare in class similar to getters and setters. Can I customize lombok to do this, or is there any other way to do this?

public class Test {
    
    String t1;
    String t2; 

These are the repetitive methods that I write for every variable that I declare.

public String TestT1() {
   test(t1);
   return t1;
}

public String TestT2() {
    test(t2);
    return t2;
}

private void test(String t1) {
    System.out.println("For Example");
}

}

You can do it using Custom Templates in Eclipse IDE. Not exact same but it will help.

  • Go to Windows > Preferences > Java > Editor > Templates .

  • Click on New button add template code as given in screenshot.

    public String Test${arg:localVar}() { test(${arg:localVar}); return ${arg:localVar}; }

在此处输入图像描述

  • In Java Editor, press Ctrl + Space and type your method name. (In my case it will be TestMethods as given while creating New Template) (Refer below screenshot)

在此处输入图像描述

  • Press Enter, eclipse will automatically add code template as created. Type your variable name just after pressing enter. (Refer next two screenshots)

在此处输入图像描述

  • After typing variable name.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM