Welcome to jMDA

To generate software automatically has been a strong ambition since the early days of software development.

jMDA is a new approach in this area. It streamlines proven, widely known and accepted open source technologies into a most comprehensible and easy to use set of Java libraries that are extremely powerful and flexible at the same time. The main purpose of jMDA is

  • to leverage a comprehensible and easy to use modelling environment,

  • to provide convenient and complete access to modelling information and

  • to make available easy to use software generator facilities.

The introduction will briefly explain the main drivers behind this project, the jMDA book provides more detailed information about the most important concepts and the open source software is available here.

jmda.core makes jsr 269 annotation processing a breeze with tasks

You can download everything you need for the examples here and here.

jMDA Core Makes JSR 269 Annotation Processing a Breeze With Tasks

The Java Compiler API is published in JSR 199 and was first delivered with JDK 6. It allows to configure and run the javac compiler programmatically. jMDA combines this with special support for the definition and execution of so called compiler plugins or annotation processors.

An annotation processor is basically a class that does two things:
  • It defines which annotations it is interested in (supported annotations) and
  • it implements a process method that will be called by javac during compilation.
With support for JSR 269 the javac compiler can be configured to execute such annotation processors (call their process method). The compiler then determines and makes available all relevant annotated elements to the annotation processor.

Start Custom Annotation Processing Within Minutes

JMDA core facilitates some rather unwieldy aspects of working with bare JSR 199 and JSR 269 and brings a lot of additional support for typical annotation processing operations. For example instead of defining a full blown custom annotation processor class you can create and configure a Task instance which is much easier to do. To execute your task you simply hand it over to a TaskRunner which does all the work required to configure and run javac together with an annotation processor. Let's demonstrate this in a Hello World example:
package de.jmda.sample.mproc.task;

import static de.jmda.util.CollectionsUtil.asSet;
// … further import statements omitted

public class JUTHelloWorldTypeElementTask
{
  private class SampleTask extends AbstractTypeElementsTaskTypes
  {
    private SampleTask(Set<? extends Class<?>> types)
    {
      super(types);
    }

    @Override
    public boolean execute() throws TaskException
    {
      System.out.println("Hello World!");

      for (TypeElement type : getTypeElements())
      {
        System.out.println("Welcome " + type.getQualifiedName() + ".");
      }

      return false;
    }
  }

  @Test
  public void test() throws IOException
  {
    SampleTask task = new SampleTask(asSet(String.class));

    TaskRunner.run(task);
  }
}
Table 1: Hello World Task example
The example defines SampleTask as a subclass of AbstractTypeElementsTask. All that has to be done is override the constructor and implement the process method. In the test method an instance of that SampleTask is created and handed over to TaskRunner. TaskRunner does everything that's needed to start javac together with an appropriate annotation processor. As a result the task's execute method will be invoked and produce the following output:

Hello World!
Welcome java.lang.String.
Hello World!

Here are a few aspects in our example that deserve some explanation:
  • First, there is no annotation processor and no specification of supported annotations! This seems to be a contradiction to what was said to be necessary for annotation processing. In fact the task runner does this for you transparently behind the scenes so that you can focus on the processing task (the process method) itself.
  • Second, we obtain access to a type element for a class, namely java.lang.String, that has no particular annotation and that comes from a .jar file for that we might not even have the source code! This means, we can obtain access to elements from any third party libraries as soon as they are available in our classpath! Again jMDA task runner does this trick for us.
  • Finally, the produced output contains “Hello world!” twice. The reason for this is explained in annotation rounds explained.

No comments:

Post a Comment