Introduction to Java Generation Java:codemodel

Source: Internet
Author: User
Tags documentation

This is often the case when we write code: part of the code can be based on another part of the code in accordance with a specific pattern of change, and sometimes, as that part of the dependent code changes, the dependent code has to be modified; sometimes, such code will appear more than once as the project progresses. A typical example is when you need to implement the data access layer yourself, usually each entity class corresponds to a DAO (data Access Object) class, and generally the DAO class code writing is very mechanical. At this point, we need to use "code generation" to improve our development efficiency and improve the maintainability of the code.

There are many ways to implement "code generation" in Java, such as using Print statements directly or using the template engine. In contrast, using CodeModel makes the code more flexible and easier to maintain. CodeModel's official website has not provided very good learning documentation, so we are constantly fumbling with the project. Therefore, this article is a codemodel of the introduction of the article, but also our experience in the use of CodeModel Summary. This article will follow the general learning programming language ideas and combined with the sample code to the reader friends to introduce the use of Codelmodel methods. Hope to be able to help readers friends.

CodeModel Introduction

CodeModel is a Java library for generating Java code, which provides a way to generate Java programs from Java programs.

The CodeModel project is a subproject of JAXB. JAXB (Java architecture for XML Binding) is a technology that generates Java classes based on XML schemas, provides a way to reverse the generation of XML instance documents into Java object trees, and can write the contents of the Java object Tree back to the To an XML instance document. JAXB is part of the JDK. The JAXB RI (Reference Implementation), schema compiler, can map XML schema files to corresponding Java elements.

How to use CodeModel

If you're going to read Jcodemodel API documentation, the Jcodemodel class is the first one we're going to touch. The Jcodemodel class is the root for generating Java code. Usually we build the Java code we need in a top-down order. For example, first create a class Jdefinedclass and then generate the method, method body, and code in the method body from the class ... In turn.

A typical application of a CodeModel is as follows:

Listing 1. Typical applications of CodeModel

Jcodemodel 

cm = new Jcodemodel (); 
Build the CM tree to generate Java code
cm._class (...); 
... 
Generate classes, methods, etc. cm.build from top to bottom
(new File ("."));

In addition to the Jcodemodel class in CodeModel, several commonly used classes are Jdefinedclass, Jmethod, Jblock, Jfieldvar, Jvar, Jtype, jexpr, etc.

The Jdefinedclass class is codemodel to define the class, which provides the creation, inheritance, implementation of the class itself, and the creation of class member variables, member methods, and so on

The Jmethod class is a Java method class that can create a method body, and then there is a Jblock class

The Jblock class is a class that we often use, and it provides a lot of methods: the declaration of local variables, the assignment of variables, the generation of various control statements, calling other methods, setting the return value of the method, and so on.

The Jfieldvar class is used to define the member variables of a class, and it can declare, assign, and so on a member variable

The Jvar class is used to define variables, providing basic operations for variables such as declaration, assignment, initialization, etc.

The Jtype class is used to define various data types in Java

The factory class of the Jexpr class expression that provides TRUE and FALSE two constant values, as well as the various methods of generating expressions and the assignment of expressions

To facilitate the subsequent code demo, now that the CM tree and the DC class are built here, CodeModel will generate a Java class named example under the SRC folder of the current project-> the DW package.

Listing 2. Building cm trees and DC classes

File Destdir = new file ((New file ("src")); 
Jcodemodel cm = new Jcodemodel ()//instantiated CodeModel 
jdefinedclass dc = cm._class ("Dw.example");//Create Example Class

Variable Declaration and assignment

Variable type

The Java variable types include basic data types and reference data types.

Several constants are defined in the Jcodemodel class to map the Java basic data type.

Jcodemodel member Constants Boolean, Byte, char, short, int, long, FLOAT, and DOUBLE cover all of Java's basic data types (Boolean, Byte, char, short, int, long, float, double), and NULL, VOID, and so on. Can be obtained directly through CM.

Reference types can be obtained by means of the Parsetype method of the Jcodemodel class. In the parameters of the Parsetype method, specify the type to reference, and get the object by the type name.

Listing 3. Get reference variable Type

Jtype type = Cm.parsetype

("String"); 
Jtype type1 = Cm.parsetype ("MyType");

You can then use type, type1 to define the type of the variable.

Local variables and member variables

Java variables can be divided into local variables (also local variables) and member variables (also become instance variables).

For local variables, you can use the Decl method in the Jblock class to define and initialize local variables. The local variable is the Jvar type. The Jblock class provides three overloaded Decl methods, respectively:

Decl (int mods, Jtype type, String name, Jexpression init)

Decl (jtype type, String name)

Decl (jtype type, String name, Jexpression init)

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.