简体   繁体   English

生成自定义Java Getter和Setter

[英]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. 我想生成自定义getter和setter,所以当我将这些实例保存到SQL数据库中时,我可以更好地处理变量。 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: 但正如我在Eclipse中看到的,它只生成以下代码:

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. 我有20个课程,我不会手动编写。

I dont know why you need this, but here is the approach to custom Getters and Setters. 我不知道为什么你需要这个,但这里是自定义Getters和Setter的方法。 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: 您可以通过转到首选项> java>代码样式>代码模板并选择代码然后编辑Getter body和Setter body来更新所有生成的setter和getter并将其放入:

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

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

Let me know if that works 如果有效,请告诉我

I recommend that instead of doing what you describe, you should use Spring Data . 我建议您不要使用您描述的内容,而应使用Spring Data Specifically the BeanPropertyRowMapper class in the org.springframework.jdbc.core package will do what you want. 具体来说, org.springframework.jdbc.core包中的BeanPropertyRowMapper类将执行您想要的操作。

Read more in the Spring API documentation . 阅读Spring API文档中的更多内容

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! 你必须手动为所有类中的setter和getter编写代码!

Try write-it-once . 尝试写一次 Template based code generator. 基于模板的代码生成器 You write custom template using Groovy , and generate file depending on java reflections. 您使用Groovy编写自定义模板,并根据Java反射生成文件。 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. 您可以通过生成AspectJ或java文件,基于JPA注释的SQL,基于枚举的插入/更新等来生成getters / settest / toString。

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... 而对于File.txt我只是将所有已定义的常量变为coppy并运行生成器......

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";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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