JaMoPP Basic Tutorial2

Ken Webb 2010-04-16T00:43:08Z

I discovered the JaMoPP website and related projects. I then put together part 1 of this basic tutorial on JaMoPP to describe how to transform a normal Java class or set of classes into one or more EMF models. JaMoPP can also transform the EMF model(s) back into the original Java source code. This tutorial will describe the steps to do this.

I ran the following code, as part of a separate executable.

protected static void model2java() {
    setUp();
    Resource xmiResource = rs.getResource(URI.createFileURI("src/test/Elephant.java.xmi"), true);
    Resource javaResource = rs.getResource(URI.createFileURI("Elephant.java"), true);
    javaResource.getContents().addAll(xmiResource.getContents());
    try {
        // why does it generate Elephant.java in JamoppTest1/Elephant/test ?
        javaResource.save(null);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

The setup() method is the same one supplied in the JaMoPP class:

protected static void setUp() {
    EPackage.Registry.INSTANCE.put("http://www.emftext.org/java",
            JavaPackage.eINSTANCE);
    Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
            "java", new JavaSourceOrClassFileResourceFactoryImpl());
    Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
            Resource.Factory.Registry.DEFAULT_EXTENSION,
            new XMIResourceFactoryImpl());
    rs.getLoadOptions().put(
            IJavaOptions.RESOURCE_POSTPROCESSOR_PROVIDER, new JavaPostProcessor());
}

The resulting Elephant.java file ended up in an unexpected place: JamoppTest1/Elephant/test

The generated file looks like this, which is the same content as the original, just formatted differently.

package test ; 

/**
 * An Elephant can be used to eat
 * bananas and carry heavy loads.
 */public class Elephant extends null { 
	private boolean tired = false ; 
	public static void main (null [] args ) { 
		// here we go
Elephant dumbo = new Elephant ( ) ; 
		if ( dumbo .isTired ( ) ) { 
			return ; 
		} 
		dumbo .eatBananas ( ) ; 
	} 
	public boolean isTired () { 
		return tired ; 
	} 
	// TODO implement eating
public void eatBananas () { 
	} 
}

return to main page