Tuesday, October 26, 2010

How to run Java class from Xpand Template

1 install eclipse (helios) software :
  • mwe2
  • xpand

2 create a new Xpand project (use wizzard)
Then, add dependencies :
  •  org.eclipse.emf.mwe2.launch,
  •  org.eclipse.xtext

3 get an emf model
       See Vogel Lars tutorial to do your own model

4 create an mwe2 workflow (use wizzard)
Add components
  •  (EMF) reader
  •  (Xpand2) generator

I create a workflow called "engine.mwe2" in frk.generator package module frk.generator.engine

1 import org.eclipse.emf.mwe.utils.*
2 var targetDir = "src-gen"
3 Workflow {
4      component = Reader {
5               modelSlot ="mymodel"
6               uri="src/MyFirst.app"
7               firstElementOnly = true
8       }
9
10       component = org.eclipse.xpand2.Generator {
11               metaModel = org.eclipse.xtend.typesystem.emf.EmfMetaModel
12              {
13                      metaModelPackage="app.AppPackage"
14              }
15              expand = "frk::template::Root::root FOR mymodel"
16              outlet = {
17                      path = "${targetDir}"
18                      overwrite = true
19              }
20              fileEncoding = "ISO-8859-1"
21      }
22}


4 : EMF reader component
10 : Generator calling Xpand

5 create a Java Class
package frk.xtend;

public class CreatePluginProject {

       public static void createPluginProjectMeth(String name) {
               System.out.println("project created "  + name);
       }

       public static String createPluginProjectMethString(
String name)
{
               System.out.println("project " + name + " created from
method ");
               return "a string";
       }
}

6 create an Xtend extension

createPluginProjectMeth(String name): 
JAVA
frk.xtend.CreatePluginProject.createPluginProjectMeth(java.lang.String);
String createPluginProjectMethString(String name): 
JAVA
frk.xtend.CreatePluginProject.createPluginProjectMethString(java.lang.String);
7 create an Xpand template
In the package frk.template I create Root.xpt



1<IMPORT app>
2<EXTENSION frk::xtend::createProject>
3
4<DEFINE root FOR app::App>
5<createPluginProjectMethString("AAA")>
6<ENDDEFINE>

2 : Add the xtend declaration
5 : call the Java method via xtend


Finally, run the workflow and it should work.

No comments:

Post a Comment