Code Generator (1)

Source: Internet
Author: User

Computer experts are exploring a method that allows less and less repeated code, such as function encapsulation, object-oriented, AOP, MDA, and ORM ...... all these related or unrelated technologies are trying to eliminate repeated code, but along the way, people suddenly find that repeated Code cannot be completely eliminated, at a higher level, there must be more advanced repetitive code that needs to be dealt with, because code generation is gradually no longer demonized. Isn't Web Editor, compiler, IDE, and other very important tools the code generator? As long as the system is well designed, it is better to use code generators to eliminate the remaining repetitive code than to use academic methods that seriously affect performance.

 

Back to reality, in the process of developing programs, especially in the process of developing some business systems, some repeated code is always inevitable, for example, the pojo code and configuration file in the Orm, the code on the data entry interface, and the database DDL statement can not only reduce the efficiency if manual completion is required, in addition, there will be many bugs. The most important thing is that it is very easy for developers to get bored, so as to passively neglect or even leave their jobs, which increases the project's HR costs and project risks. Therefore, many development teams are using various open or self-developed code generation tools, and more people are choosing self-developed tools, this is because third-party code generation tools often cannot meet their individual needs.

We can write code generation tools in multiple ways, such as stringbuffer or groovy template or velocity, however, this book describes the Eclipse plug-in development, so I will mainly look at the code generation solutions in eclipse.

1. Use stringbuffer concatenation to generate code
In some simple code generation, this method is more convenient, but when the generated code structure becomes more and more complex, the Sb in the code. the append () and logic judgment code are mixed together, and the program becomes very difficult to maintain.

2. Use AST in jdt API
Jdt will compile the Java code into AST (Abstract syntax tree, abstract syntax tree), so that the complex Java code becomes a relatively simple tree structure, we can use ast to traverse Java code to parse the code or modify the code. Java code refactoring in eclipse is based on AST.
In eclipse, AST is called compilationunit, and the corresponding interface is icompilationunit. The simplest way to generate compilationunit through Java code is to use the method in ipackagefragment interface.

Createcompilationunit. Specify the name and content of the compilation unit. Therefore, a compilation unit is created in the package and a new icompilationunit is returned. We can also create a compilationunit from scratch to generate a compilationunit independent of Java code, and then add classes, methods, and Code on the compilationunit, call the ast parser of jdt to output compilationunit into Java code. This method is the most rigorous method, but when the code to be generated is complicated, the program becomes bloated and can only generate Java code, and cannot generate xml configuration files and other formats.

 

3. Jet
Jet is a very powerful code generation tool in eclipse. With jet, you can use syntaxes similar to JSP, so that we can easily compile code generation templates. It can be used to create code generators for SQL statements, XML, Java source code, and other files. The following describes jet. To be continued.

 

 

PS: transferred from Yang zhongke.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.