Friday, January 29, 2010

How to create an EMF model from a Java application ?

Is it possible to reuse EMF-based tools with Java source code ? Yes if you have your Java source code translated into an EMF model.

This is the aim of the Java component provided by MoDisco. This component is composed of a ECore definition of a java application and a discoverer which creates EMF models from an EClipse project containing Java source code.

The ECore definition declares all the concepts of the Java language : Package, ClassDeclaration, MethodDeclaration, Parameters, VariableDeclaration, IfStatement, Assignment, CastExpression, LineComment, ...

To create an EMF model using this metamodel, select your project and right-click on the menu "MoDisco>Discover Java model from Java project...". A wizard appears to select the analysis options :
  • You can extend the analysis to dependencies (the JAR used by the project).
  • You can use an incremental mode if you need to optimize the memory usage
  • You can limit the analysis to declaration members if you don't need a model of the instructions contained within the methods
  • You can use a filter to exclude Java classes during the analysis

The result is an EMF model which can be opened with the EMF reflexive editor or with the MoDisco generic browser. This browser allows accessing directly to instances of a specific class (for example all the MethodDeclarations) and navigating through all the references (composed or not).

This model can be used with other EMF-based tools to check architectural or coding norms, to generate other models (UML or Domain-Specific) or to regenerate an other application (if you modify the Java model, MoDisco provides templates to regenerate Java source code).


  1. hi
    does anyone know if there is a way to programmaticly generate the java model from a folder containing java source code?
    Cheers B

  2. Hi,

    Here is an example of code showing how to programmatically create a java model from java source code :

    // Create a discoverer for a Java project
    DiscoverJavaModelFromJavaProject javaDiscoverer = new DiscoverJavaModelFromJavaProject();

    // Parameters of the discoverer
    Map javaDiscoveryParameters = new HashMap();

    // Execute the discoverer (javaProject is a IJavaProject)
    javaDiscoverer.discoverElement(javaProject, javaDiscoveryParameters);

    // Get the result model
    Resource javaModel=(Resource) javaDiscoveryParameters .get(DefaultDiscoverer.PARAMETER_TARGET_RESOURCE)

    If you need help, you can post your questions directly on the MoDisco forum :

  3. Hi Fred,
    I used your example and got it to work in a simple EclipseApp setup...
    Unfortunatly there is a problem with the
    javaDiscoverer.discoverElement(javaProject, javaDiscoveryParameters);
    line 187 from the "DiscoverJavaModelFromJavaProject" class
    keeps throwing an Exception:

    and after that in line 218 inside the catch

    "JavaActivator.getDefault()" returns null...

    do you have an idea why this could be the case?

    Thanx in advance

  4. Fred, thanks for this blog. Is it possible to do something similar for sql. In other words is there ECore definition for all sql language.

    I have need to reverse engineer oracle sql code and convert it to a different model(piglatin). Is it doable.

  5. Hi Ganesh,
    Yes it is possible to do something similar for sql, but it is not provided by MoDisco ... yet.

    There are two things to do:

    1) Define an ECore model for sql. Depending on what you want to reverse-engineer (schema, queries or data), the model will not be the same. You can find here a basic metamodel for Database Schema :
    Or you can reuse the KDM model which contains a Data Package defining classes such as RelationalSchema, RelationalTable, UniqueKey, ReferenceKey, etc). This model is defined by the OMG and an implementation is provided by MoDisco (

    2) Develop a discoverer to create a model from existing source code. If you want to create a model from the schema definition, you have two possibilities: parse the schema definition (using xText for example ?) or use JDBC to get the schema definition directly from an instance of the database.

    If you develop such a component, it would be great if you could contribute it to MoDisco ;-)

  6. Thanks for the prompt response. I would like to model sql queries and generate pig latin/hadoop code from it.

    I will definitely contribute to modisco if i am successful. Can you refer to a simple example for the two steps you mentioned in this blog.

  7. So you need a IJavaProject, do you need any way to simulate that and execute it outside Eclipse?

  8. Hi Fred, could you help me by providing me the java metamodel because i need it to create a model for a java application that i have its source code.

  9. Really Nice Information,Thank You Very Much For Sharing.
    Web Designing Company