Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build simple code generator in java

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

like image 489
Amira Elsayed Ismail Avatar asked May 04 '11 10:05

Amira Elsayed Ismail


People also ask

What is a Java code generator?

Code generator is used to produce the target code for three-address statements. It uses registers to store the operands of the three address statement.

Are there generators in Java?

No, Java does not have "generators" or "yield" per-se, but the same functionality is available by using the Observer Pattern. This is enhanced when using a modern implementation like RxJava.


1 Answers

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:

  • Template Code Generator : Apache Velocity - JET - JET2
  • Template Code Generator Part 2 : FreeMarker
  • Jet Tutorial - part 1, part2

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.

like image 156
Stephen C Avatar answered Sep 26 '22 07:09

Stephen C