Lukas
Lukas

Reputation: 402

Generate custom Java Getters and Setters

I want to generate custom getters and setter, so I can handle variables better when I will be saving these instances into SQL database. I want to generate something like:

public class Test extends SQLEntry {

    private static final String NAME = "Name";

    public String getName() {
        return get(NAME);
    }

    public void setName(String name) {
        set(NAME, name);
    }
}

But as I can see in Eclipse it generates only the following code:

public class Test {

    private String name;

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Is there some plugin, that can do it? Or am I missing something? I have like 20 classes and I will not write this manually.

Upvotes: 0

Views: 3405

Answers (5)

Lukas
Lukas

Reputation: 402

On the end I found it that it is the best to do it your self...

If you like writing a code than you will enjoy this solution the most.

public class CodeGenerator {

private final static String ENCODING = "UTF-8";
private final static String FILE_NAME = "File.txt";

public static void main(String[] args) {
    try {
        ArrayList<Carriage> names = getNames();
        for (Carriage c : names) {
            createSetter(c.name, c.capitalName);
            createGetter(c.name, c.capitalName);
        }
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    }
}

private static ArrayList<Carriage> getNames() throws FileNotFoundException {
    File file = new File("/");
    InputStream is = CodeGenerator.class.getResourceAsStream(FILE_NAME);
    Scanner s = new java.util.Scanner(is, ENCODING).useDelimiter("\\A");
    String content = s.next();
    String[] lines = content.split(System.getProperty("line.separator"));
    ArrayList<Carriage> ret = new ArrayList<Carriage>();
    for (String line : lines) {
        line = line.replaceAll("\\r", "");
        int firstCapitalIndex = line.indexOf("String") + 7;
        int secondCapitalIndex = line.indexOf(" ", firstCapitalIndex);
        int firstIndex = line.indexOf("\"") + 1;
        int secondIndex = line.indexOf("\"", firstIndex + 1);
        Carriage c = new Carriage();
        c.name = line.substring(firstIndex, secondIndex);
        c.capitalName = line.substring(firstCapitalIndex, secondCapitalIndex);
        ret.add(c);
    }
    return ret;
}

public static void createSetter(String name, String capitalName) {
    String str = "public void set" + name + "(String val) {\n"
            + "\tset(" + capitalName + ", val);\n"
            + "}\n";
    System.out.println(str);
}

public static void createGetter(String name, String capitalName) {
    String str = "public String get" + name + "() {\n"
            + "\treturn (String) get(" + capitalName + ");\n"
            + "}\n";
    System.out.println(str);
}

carriage:

package codegenerator;

public class Carriage {
    public String name;
    public String capitalName;
}

And to File.txt I just coppy all defined constants and run the generator...

public static final String NAME = "Name";
public static final String PHONE = "Phone";
public static final String EMAIL = "Email";
public static final String ADDRESS_1 = "Address1";
public static final String ADDRESS_2 = "Address2";
public static final String ADDRESS_3 = "Address3";
public static final String ICO = "Ico";
public static final String DIC = "Dic";
public static final String ADMIN_LOGIN = "AdminLogin";
public static final String ADMIN_PASSWORD = "AdminPassword";
public static final String LANGUAGE = "Language";
public static final String CODE = "CODE";
public static final String MONTHLY_PAYMENT = "MonthlyPayment";

Upvotes: 0

Atmega
Atmega

Reputation: 131

Try write-it-once. Template based code generator. You write custom template using Groovy, and generate file depending on java reflections. It's the simplest way to generate any file. You can make getters/settest/toString by generating AspectJ or java files, SQL based on JPA annotations, inserts / updates based on enums and so on.

Upvotes: 0

tbsalling
tbsalling

Reputation: 4545

I recommend that instead of doing what you describe, you should use Spring Data. Specifically the BeanPropertyRowMapper class in the org.springframework.jdbc.core package will do what you want.

Read more in the Spring API documentation.

Upvotes: 1

Khalil
Khalil

Reputation: 259

I dont know why you need this, but here is the approach to custom Getters and Setters. You can update all generated setters and getters by going to preferences > java > Code Style > code Templates and selecting code then edit Getter body and Setter body and put this:

Getter body: return get(${field});

Setter body: set(${field}, ${param});

Let me know if that works

Upvotes: 1

Nomesh Gajare
Nomesh Gajare

Reputation: 855

there is no other plugin available!

how can some plugin write code that is specific to your business logic!

you have to write the code manually for setters and getters in all the classes!

Upvotes: 0

Related Questions