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.

Define, compile and run dynamic code in RAM

Define, compile and run dynamic code in RAM

jMDA provides a feature to conveniently define Java source code as String, to compile the content of such a String and finally run the compiled byte code. The following example demonstrates how to do this. The code and everything else necessary to compile and run the example is available here.

package de.jmda.sample.ramcomp;

import java.io.IOException;

import de.jmda.core.util.ramcomp.RAMCompiler;
import de.jmda.core.util.ramcomp.RAMCompiler.RAMCompilerData;

public class DefineCompileAndRunJavaSourceCodeInRAM
{
  public static void main(String[] args)
      throws InstantiationException,
             IllegalAccessException,
             ClassNotFoundException,
             IOException
  {
    RAMCompilerData data1 =
        new RAMCompilerData
        (
"C1",
"public class C1 implements Runnable           \n" +
"{                                             \n" +
" public void run()                            \n" +
" { System.out.println(getClass().getName()); }\n" +
"}"
        );

    RAMCompilerData data2 =
        new RAMCompilerData
        (
"C2",
"public class C2 implements Runnable           \n" +
"{                                             \n" +
" public void run()                            \n" +
" { System.out.println(getClass().getName()); }\n" +
"}"
        );

    RAMCompiler.compile(data1, data2);

    Object object;

    // run first class
    object =
        data1.getClassLoader()
             .loadClass(data1.getQualifiedTypeName()).newInstance();
    ((Runnable) object).run();

    // run second class
    object =
        data2.getClassLoader()
             .loadClass(data2.getQualifiedTypeName()).newInstance();
    ((Runnable) object).run();
  }
}

The above example demonstrates that it is easily possible to define and compile even multiple Java source code strings at the same time. After compiling the code you can run each program using standard Java reflection.

No comments:

Post a Comment