Skip to content

Code Generation

landawn edited this page Apr 29, 2018 · 2 revisions

It's easy to generate tens, even hundreds of lines of pretty-formatted entity class with couple of lines of codes. Here is a simple sample: (Account.java)

// 1, Productivity: generate tens, even hundreds of lines of code by couple of lines of codes in one minute.
// 2. Maintainability: It's easy and simple to add/remove fields or change types of fields. And all the codes follow the same format.
// 3. Bug free. No test is required for the auto-generated codes and no test coverage is counted. 

// prepare class with fields:
public class Account {
    private String firstName;
    private String lastName;
    private Date birthdate;
    private String email;
    private String address;
    private Map<String, List<Date>> attrs;
}

// Then just two lines to generate the mostly beautiful and well-formatted entity class:
File srcDir = new File("./src");
CodeGenerator.writeClassMethod(srcDir, Account.class);

// OR:
String packageName = "com.x.y";
Map<String, Object> fields = N.asLinkedHashMap("firstName", String.class, "lastName", String.class, "birthdate", Date.class, "email", String.class,
        "address", String.class, "attrs", "Map<String, List<java.sql.Date>>");
CodeGenerator.generateEntity(srcDir, packageName, "Account", fields);

Entities can also be easily generated from database tables by the steps in Code Generation 2. It will accelerate the development and make your codes much cleaner by the above auto-generated classes. It's a lot of efforts we have spent to write this utility class to generate the pretty-formatted codes. We hope you will like it and enjoy programming with it.