2

I know we can generate getter and setters by Source -> Generate Getter and Setters... but my question is is there any way we write our own Template for shared preferences getter and setters that can generate all the getter setters against all the data members in exactly the same way as Eclipse does for simple Getter and Setters. Its hectic job when you've a lot to save in preferences. Can eclipse write the following code for me against KEY_SESSTION_TIMEOUT.

private static final String KEY_SESSION_TIMEOUT = "sTimeOut";
public static long getSessionTimeOut()
    {
        return sp.getLong(KEY_SESSION_TIMEOUT, 30000);
    }

public static void setSessionTimeOut(long timeOutSec)
    {
        editor.putLong(KEY_SESSION_TIMEOUT, timeOutSec).commit();
    }
Ishtiaq
  • 1,206
  • 9
  • 18
  • 1
    I'm not sure about Eclipse, but when you use Android Studio, you can create Live Templates. http://www.jetbrains.com/idea/webhelp/creating-and-editing-live-templates.html – Jelle Aug 19 '14 at 09:43
  • You can generate templates in Eclipse. Have a look at this post: http://stackoverflow.com/questions/23426493/is-there-any-way-to-generate-lorem-ipsum-in-eclipse – Phantômaxx Aug 19 '14 at 09:47
  • I want my template to be called as many times as the data members in my class. – Ishtiaq Aug 19 '14 at 09:50

1 Answers1

0

You maybe looking for Annotations which can help you generate code (getters and setters in your case). Please see how Project Lombok generates getters and setters using annotations.

https://stackoverflow.com/a/1918154/1321873 is also a source of some good information on the topic.

Community
  • 1
  • 1
Rajesh
  • 15,724
  • 7
  • 46
  • 95