The Wurblet

The wurblet should create the Java code for the declaration of attributes and their getter- and setter methods, like this:
      private String name;    // the object's name
      private long id;        // the object's id
      ...

      /**
       * Sets attribute name.
       *
       * @param name the object's name
       */
      public String setName(String name) {
        this.name = name;
      }
      ...
      
The model is simply a list of attributes, each described by a name, a Java type and some optional comment.
In real life, models are much more extensive, but this is for demonstration only.
A wurblet can access models in different ways. This can be a simple text file, an sql database or some xml documents. Because the generator language is Java, there are no restrictions beyond the restrictions of Java itself.
For the quickstart example, we've chosen a so-called here-document. With here-documents it is very easy to create model information from within comment blocks. The motivation is the same as with javadoc: everything is kept in the source. If you change the model, the code changes as well.
The model in src/Quickstart.java looks like this:

model

When the wurbler parses Quickstart.java, it creates a document with the name .quickstartModel. The dot causes the document to be created in heap space, i.e. not in the filesystem. When the wurbler executes the wurblet, it will access the model by that name.

Writing a wurblet usually involves two steps:
  1. write code to parse the model
  2. write the generator
You will find the very simple parser in wurblets/src/AbstractAttributes.java:

model parser

If you look at AbstractAttributes.java more closely, you will discover that it is an extension of org.wurbelizer.AbstractWurblet. This is a common pattern for designing wurblets because it separates the modeling and tooling part from the generator part and thus makes the code clearer and easier to maintain. The wurblet code then extends this interim class. This is exactly what we do in wurblets/wrbl/Attributes.wrbl:

wurblet source

Wow! That looks pretty much like Java, but what about all these @-characters!
That's easliy explained: Now that the wurblet is completed, we need to bind it to the source file. Please take a look into src/Quickstart.java again:

wurblet anchor

The wurbler scans all comments for wurblets (no matter whether single-line or block-comment), invokes the wurblets and inserts the generated code into the source file (usually immediately following the comment).
The binding of a wurblet is called a wurblet anchor, which is defined as:
      @wurblet <tag> <wurbletname> <arg1> <arg2> .... <argN>
      
where: The wurbelizer is able to identify the generated code by its tag. Thus, it is possible to keep manual changes in the source in between wurbelizer passes. The generated code is embraced by special strings for the wurbelizer to distinguish it from manually written code. Netbeans displays generated code in a gray color (blue up to NB 5.5.1) and prevents the user from accidently modifying it.

next >