XDoclet starts xDoclets are a code generation tool that can free from heavy duplicate labor in Java development. XDoclets can make your application develop more quickly, and you only need to pay more than a few efforts. You can hand over your lengthy and necessary code to help you, you can escape "Deployment Descriptor Hell", you can also make your application make it easier to manage. And what you have to do, just in your comment, add some kind of Javadoc attributes. Then, you will be surprised that XDoclet is everything to do. Discussing xDoclet, there is a little more prone to confusion, that is, XDoclet is not only a system code generated application, and it is still a code generation framework. Although the details of each application system have changed (such as the EJB code generation and Struts code generation is different, the JMX code generation is another scene), but the core concepts and usage of these code generated are similar. In this chapter, we will see the XDoclet Framework Foundation Concept in all XDoclet code generating programs. But before, let's start with an example. 2.1 XDoclet in an action Every programmer will recognize that their programs will never be completed. There will always be another function to be added, and the other bugs need to be corrected, or constantly reconstructing. So, add a comment in your code, remind yourself (or other programmers) What tasks have to be completed. How to track whether these tasks are completed? Ideally, you will collect a list of TODO tasks. In this regard, XDoclet provides a powerful TODO generator to help you complete this task. This is a good opportunity to introduce xDoclet into the project. 2.1.1 A common task assumes that you are developing a class that uses a spoon. Public class matrix {// Todo - Need to handle if there is no spoon Public void reload () {// ... spoon spot = getspoon (); // ...}} Ideally, you are reading this section next time When you are code, you will handle this "null spoon" problem. But if you have come back for a long time, you will remember that there are some work in this class to do it? Of course, you can search TODO in your source code, and even your integrated development environment has a built-in TODO list support. But if you want to mark the classes and methods where the task is also labeled, the xdoclet can be another option. XDoclets can generate a TODO report for your project. 2.1.2 Adding XDoclet Tags In order to convert your TODO project into another more formal format, you need some subtle changes to the code. As shown below: Public class matrix {/ ** @todo needs to handle when there is no spoon * / public void reload () {// ...}} This javadoc label required for XDoclet is added. XDoclet uses information of these tag tags, and the classes and methods where tags in this case are generated to generate Todo reports. 2.1.3 Integration with ANT To generate Todo reports, you need to make sure you have an xdoclet installed correctly on your machine.
In Ant tasks, at least one goal (eg init target) defines the
If you use
The following example is to add
We will continue to introduce all tasks and subtasks in the next chapter, as well as commonly used configuration options. 2.4 Trying the code to generate the output of interest to generate an output of interest. A parser generator also requires a language description to resolve the generated parser. A business object code generator requires a domain model to know which business objects want to generate. XDoclets require a Java source file as an output to generate a related class or deployment / configuration file. However, the source file may not provide all the information required to generate the code generation. Consider a servlet-based app that when you want to generate a web.xml file, the servlet source file can only provide class names and appropriate servlet interface methods. Other information such as URI Pattern mapping, the information such as initialization parameters required by the servlet is not covered. Obviously, if the class does not provide this information to you, you need to manually fill in this information when you are web.xml file. XDoclets certainly will not know this. Fortunately, the solution is simple. If the required information is not available in the source file, then provide it, the practice is to add some xdoclet properties in the source file. XDoclet parses the source file, extracts these properties, and passes them to templates, and templates use this information to generate code. 2.4.1 Analysis The Attribute XDoclet property is actually a JavaDoc extension. They can be placed in the Javadoc documentation in the Javadoc documentation on the Javadoc Documentation. Document Note Beginning in / **, * / end. Here is a simple example: / *** This is a javadoc comment. * The annotation can be decomposed into multiple lines, and each line begins in an asterisk. * / All text in the comment is considered Javadoc comments and can be accessed by xdoclets. Comment blocks are generally related to a entity in the Java source file, and follow the front of this entity. An annotation block without an entity will not be processed. Class (or interface) can have an comment block, method and domain can also have its own annotation block, such as: / *** class comment block * / public class SomeClass {/ ** domain Note block * / private int id; / * ** Constructor Note Block * / Public SomeClass () {// ...} / ** * Method Note Block * / Public Int GetId () {Return ID;}} The comment block is divided into two parts: the description section and the label section . When you encounter the first Javadoc tag, the label portion starts. The Javadoc label is also divided into two parts: label name and label description. The label description is optional and can be multi-line. For example: / *** This is a description section * @ Tag1 label section starts from here * @ tag2 * @ tag3 front a tag without label description. * This label has multiple line label descriptions. * / Xdoclet Extension Javadoc labels using the parameterized tag. In the xDoclet, you can join the Name = "Value" parameter in the label description section of the Javadoc tag. This tiny change greatly enhances the expression of Javadoc tags that allows the Javadoc tag to describe complex metadata.
The following code shows the use of xdoclet attribute description entity bean methods: / *** @ ejb.interface-method * @ ejb.relation * name = "blog-entries" * role-name = "blog-haas-entries" * @ ejb.value-object * compose = "com.xdocletbook.blog.value.EntryValue" * compose-name = "Entry" * members = "com.xdocletbook.blog.interfaces.EntryLocal" * members-name = "Entries" * Relation = "external" * type = "java.util.set" * / public abstract set getNTries (); parameterized tags allow combination logical properties. You can join the meta information describing this class, so that this class is sufficient to generate code. In addition, the programmer can quickly understand how this class is used by reading such meta information. (If you don't understand this example, don't worry, in Chapter 4, we will learn the EJB-related tags and their connotation.) In addition, pay attention to the above example, all labels Be started with EJB. XDoclets provide a namespace with Namespace.Tagname. In addition to distinguishing between Javadoc, you can organize task-related labels to prevent confusion between tags between tasks.
2.5 Code Generation Mode XDoclet is a template-based code generating engine. From the high-level view, the output file is actually generated by parsing a wide variety of templates. If you understand the template and the context environment it execute, it can be exactly what xDoclets can generate and cannot be generated. If you are evaluating the XDoclet platform, understanding these concepts is very important. Otherwise, you may miss many powerful features of XDoclet, or may be confused by some restrictions on xdoclet. XDoclets are run in an Ant build file environment that provides an ANT-custom task and sub-task to interact with the XDoclet engine. The task is the container of the sub-task, and the sub-task is responsible for performing code generation. Submission call template. The template provides a biscuit mold that will generate a code. XDoclet parses the input source file, extracts the xdoclet attribute metadata in the outgoing file, and then supplies these data to the template, drive template execution. In addition, the template can also provide a merge point, allowing the user to insert some template pieces (merge file Merge Files) to customize code generation as needed. 2.5.1 Template Basic XDoclet uses the code template to generate code. Template is the prototype you want to generate files. The template uses some XML tags to guide the template engine to adjust the generation of the code based on the input class and their metadata. [Definition: Template (Template) is an abstract mold view that generates code or description file. When the template is parsed, the specified details information will be filled. ] Template is in general, there will be an execution environment. Templates may be applied to a class environment (conversion generation transform generation), and it is also possible to apply in a global environment (aggregation generation aggregate generation). Conversion generation and aggregation generation is the two types of task modes of XDoclets, understanding the differences between them are very important for understanding xDoclets. When you use the XDoclet to generate a descriptor file, you use the aggregation generation. Arranging the descriptor file is not only related to a class, in contrast, it needs to aggregate information from multiple classes to an input file. In this architecture, the parsing of the template will only generate an output file, no matter how many input files. In the conversion generation mode, the template will be parsed once every source file, and the output is generated according to the context environment of the file class. This generated mode generates an output file for each input file. A good example of the conversion generation mode is to generate the Local and Remote interfaces of EJB. Obviously, the interface is associated with a bean class. Extract information from each class (class, domain, interface, and xDoclet attribute) to convert an interface. No other information is required. Extracting an interface from the implementation seems to be a bit reverse. If you write a program, you generally say an interface first, then write a class to turn it. But XDoclet can't do, XDoclets are not possible to help you implement an existing interface because it is not possible to help you generate your business logic. Of course, if business logic can get from the interface itself (such as a JavaBean Get / SET Accessor) or use the xdoclet attribute to declare, generate business logic code to implement an interface is not impossible. But in general, this is not really realistic. In contrast, an implementation is provided and the association between the interface and this implementation is much easier. The aggregation generation and conversion generation is mainly different from their environmental information. Even if a Java file is generated in a code generation task, it is generally not commonly used to generate, because generating a Java class also requires some important information, such as the package, the class name you want, is unable to provide in this environment of. If you must use aggregation generation, you need to provide information on another separate place. 2.5.2 Template Tags have been more in-depth to understand it before I haven't seen the template. What is the template file? It is a bit like a JSP file.
They all contain files and XML tags, and the XML tab will be parsed when generating output files, and then generate text and displayed at the location where the XML tag is located. In addition to the XML tab that heads earlier with XDT, the rest of the XML tag xdoclet will ignore regardless of the XDoclet engine. The following code snippet shows XDoclet template "classic style": public class
The standard tag is called the conditional tags here. There are also some Body tags to provide similar iterations, which will be parsed for multiple times. For example, a label parses each method in the class. The XDoclet tag provides many high-level code generation features, but sometimes they may appear to be flexible enough, or the ability to express your needs. At this time, you can choose to extend the XDoclet template engine compared to additional development of a common function. You can develop your own labels with more expressive capabilities, and more powerful Java platforms. 2.6 Use the consolidation of custom code generation systems for use, main reason is that they often generate some deadlines, not flexible code. Most code generation systems do not allow you to change their generated code; if this system is not flexible enough, the best extension you can do is the application inherited the code generated, or use some common design patterns (such as Proxy And adaptor to meet your needs. Whether this, this is not a good way to generate the code you want to generate. The code generator is best to do the resulting Wygiwyg (What You Generate Is What You get, replacing the code that you need to spend a lot of time to generate and do not satisfy the required code. Therefore, for the code generator, supporting flexible customization is a prerequisite for generating code that can fully meet the requirements. XDoclets support customized codes in the template file definition by merge point (Merge Points) to allow runtime to insert custom code when the schedule file definition is allowed. Sometimes, merge points can even affect the generation of global code, not only allow you to add some custom content, but also fundamentally change things that will be generated. [Definition: Merge Points is a pre-defined schedule predefined extension point to add custom content in code generated by code] Let us study a template code from the xdoclet source code. At the end of the template that generates primary keys for entity beans, such a merge point is defined: