How to build simple code generator in java

28,763

Solution 1

For quick and simple code generator: Just write the java source code into a StringBuilder and dump it's content to a .java file. I never used third party libs for simple autogenerators:

StringBuilder sourceBuilder = new StringBuilder();
sourceBuilder.append("package com.example.beans;\n\n");
sourceBuilder.append("import java.util.*;\n\n");
sourceBuilder.append("public class MyBean {");

for (DBField dbField:getFieldsFromDatabaseModel) {  // this DBField class is pure fiction!
  // bean attribute
  sourceBuilder.append("\tprivate ")
               .append(dbField.getType)
               .append(toFieldName(dbField.getName()))
               .append(" = null;\n");

  // setter method
  sourceBuilder.append("\tpublic void ")
               .append(toSetterName(dbField.getName()))
               .append("(");
               .append(dbField.getType)
               .append(toFieldName(dbField.getName()))
               .append(")\n")
               .append("\t\tthis.")
               .append(dbField.getType)
               .append(" = ")
               .append(dbField.getType)
               .append(";\n\t}");

   // getter method ...
sourceBuilder.append("\t}\n}\n");

Solution 2

If you want to implement your own code generator ...

In my experience ... using a template-based tool like Velocity or FreeMarker or JET reduces the effort in writing and maintaining source code generators.

Here are some relevant links:

Doing code generation using StringBuilder / String concatenation, is a lot of work (for a non-trivial code-generation task) and tends to give you a pile of code that is hard to read and hard to maintain. It is also more difficult to generate well-formatted (e.g, properly indented, line-wrapped) code, if that is relevant to your use-case.

Solution 3

Don't reinvent the wheel, use an existing code generator. Telosys Tools does this kind of job : http://www.telosys.org/

It's an open source project, so you can also look inside if you want to reuse the generator engine (it works with Velocity templates)

Solution 4

Minuteproject is designed to meet this requirement. It reads database metadata, optionally (but in reality extremely useful) enrich the model and generate for any text based language (ex: java, c#, php, jsf, html, js etc...) You can benefit out of the box of shipped solutions (JPA2, Openxava, Primefaces)

But also you can:

  • create additional artifacts (your own that match your architecture/design);
  • decide there naming convention;
  • if there are model, package, application, table, column, view specific;
  • any artifact can find information on other artifacts if they want to correctly reference them.
Share:
28,763
Amira Elsayed Ismail
Author by

Amira Elsayed Ismail

God put me on earth to accomplish a certain number of things, right now I am so far behind, I will never die.

Updated on February 15, 2020

Comments

  • Amira Elsayed Ismail
    Amira Elsayed Ismail about 4 years

    I want to build a simple code generator in java that can take database connection string and then create the model layer for each table in database , I don't know how can I do that , so if any one has any tutorial or links that may help me complete this task i will be thankful

    Thanks in Advance


    I have found a good example on code project that can take the database and make a good code generation for the tables in database , but unfortunately it is written in C# and also generate C# code

    I don't know if it right to generate C# code and then convert them to java using any conversion software , or the right thing is to try to modify it to generate java classes instead of C# classes

    by the way the link for the example is http://www.codeproject.com/KB/codegen/TierGenerator.aspx

    I need your advice as soon as you can

    Thanks in Advance

  • benez
    benez about 4 years
    could not be any simpler, more transparent and expandable than this. any suggestions to help with code formatting?
  • Andreas Dolk
    Andreas Dolk almost 4 years
    Java still misses multi-line template string. For a professional approach, I wouldn't use the string builder but a template engine, velocity for example. The benefit would be readable and maintainable templates. This solution is more like a quick an easy hack for small projects. We can enhance readbility by like passing String.format("<line of code>",<params>) to the append methods and don't indent with tabs but with spaces.