资源预览内容
第1页 / 共8页
第2页 / 共8页
第3页 / 共8页
第4页 / 共8页
第5页 / 共8页
第6页 / 共8页
第7页 / 共8页
第8页 / 共8页
亲,该文档总共8页全部预览完了,如果喜欢就下载吧!
资源描述
关于代码生成技术Working in Java either means writing a little bit of complex code or writing a lot of gruntwork code. J2EE is a prime example; implementing the persistence for a single database table takes five classes and two interfaces using EJBs, and almost all of the classes are clerical work. We have to write them, but we dont have to do it by hand. Code-generation techniques can make building high-quality EJB code a breeze.Will code generation revolutionize computing and change the way we develop forever? Yes, but it will take a while. Software engineering has always concentrated on increasing our level of abstraction. In the beginning, we hand-wrote machine code; then we created assemblers and macro assemblers. After that, we created Fortran and compiled our code into assembler. Then came structure programming, and after that, object-oriented programming. With each step, we have increased our level of abstraction and, thus, our ability to create higher quality applications with more functionality, more quickly.What is Code Generation?What is this panacea for developers called code generation? Code generation is the technique of writing and using programs that build application and system code. To understand code generation, you need to understand what goes in and what comes out. What goes in is the design for the code in a declarative form: I need two tables named book and author with these fields. What comes out is one or more target files. It could be Java code, deployment descriptors, SQL, documentation, or any type of controlled output.Figure 1 shows the basic form of todays code generators:Figure 1. The process of code generationThe components can change slightly between the different models, but the song remains the same.The code generator readsin the design, then uses a set of templates to build output code that implements the design. The separation between code generation logic in the generator and output formatting in the templates is akin to the separation between business logic and user interfaces in web applications.Code generators are not wizards. Wizards are passive generators. They write code once, and then its up to you to maintain the code forever. Code generators are active. They continually maintain code over multiple generation cycles. As the designs change, the input to the generator changes, and new code is created to match the design. This is a key advantage when have you been on a project where the requirements dont change?What Are the Benefits?Before we get into specific examples of code generators for Java, lets make sure we have the end goals firmly in mind. One way to approach this is to think about the qualities we want in an optimal generator. Quality: We want the output code to be at least as good as what we would have written by hand. Thankfully, the template-based approach of todays generators builds code that is easy to read and debug. Because of the active nature of the generator, bugs found in the output code can be fixed in the template. Code can then be re-generated to fix that bug across the board. Consistency: The code should use consistent class, method, and argument names. This is also an area where generators excel because, after all, this is a program writing your code. Productivity: It should faster to generate the code than to write itbyhand.Thisisthefirstbenefitthatmostpeoplethinkofwhen it comes to generation. Strangely, you may not achieve this on the first generation cycle. Thankfully, the real productivity value comes later, as you re-generate the code base to match changing requirements; at this point you will blow the hand-coding process out of the water in terms of productivity. Abstraction:Weshouldbeabletospecifythedesigninanabstract form,freeofimplementationdetails.Thatwaywecanre-targetthe generator atalater date if wewant to moveto another technology platform.Now that we understand that benefits that we want, and how those are addressed by code generation techniques in general, we should understand what we expect to use code generation for in the Java context.What We Expect the Generator to HandleThe output files of a generator are called the target files. There are several generation targets within the Java enterprise application stack. Figure 2 shows the stack:Figure 2. J2EE generation targetsAll four of these elements of the stack are potential generation targets, but some are more common than others. From the bottom to the top: Database: Given Javas object-persistence approaches to database work, there isnt much call for direct generation of SQL for database code or stored procedures. However, if this is your architecture, you can use the custom approaches listed below to generate the required code. Persistence : Database persistence code is the most common generation target in the Java environment. All of the generators Irefertointhesectionsthatfollowbuildpersistencecode.Why? Its
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号