How create Java code from Ecore metamodel without EMF jar dependences?

3.3k views Asked by At

Is possible to generate Java code from an Ecore metamodel, without the dependencies of EMF? Generate "clean Code" as if it were a generation from a UML model.

2

There are 2 answers

3
Tonny Madsen On BEST ANSWER

The easy way is to set the GenModel option "Suppress EMF Types" in the "Model Feature Defaults" section of the top-level node... There are are hole set of relevant options. Refer to the EMF book (2nd edition), for the complete details.

You can also use JET to generate you own Java code from the ECore model. Have a look at "Metamodeling with EMF: Generating concrete, reusable Java snippets" for a rather detailed description.

1
wmorrison365 On

Not everything can be removed using the EMF model editor. The #eSet, #eGet, etc methods have to be removed in the underlying template. The eInvoke method can be removed through setting "Operation Reflection" = false. The util package can be removed setting package level config "Adapter Factory" = false. There are other changes that can be managed from the genmodel config - see the EMF Modeling Framework book, section 12.3.1 for more details.

Note that "Suppress EMF Types" removes the EMF type from EOperation and EReference getters and setters only but not from the declared ivar or the constructor used in your getter (if list is null). These have to be changed in the template - Class.javajet to be specific. For details on how to replace the core EMF templates, see using-dynamic-templates-in-emf.

For the ivar, use Feature#getImportedType, not Feature#getImportedInternalType:

protected <%=genFeature.getImportedType(genClass)%> <%=genFeature.getSafeName()%>;

This line follows the comment section containing the following that is within the isListType test:

<%} else if (genFeature.isListType() || genFeature.isReferenceType()) {%>
  <%if (genClass.isField(genFeature)) {%>
/**
 * The cached value of the '{@link #<%=genFeature.getGetAccessor()%>() <em><%=genFeature.getFormattedName()%></em>}' <%=genFeature.getFeatureKind()%>.
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->

For the accessor, use something like the following to replace The EMF EList impl with an ArrayList:

<%=genFeature.getSafeName()%> = new <%=genModel.getImportedName("java.util.ArrayList")%><<%=genFeature.getListItemTy‌​pe()%>>();

instead of:
<%=genFeature.getSafeName()%> = new <%=genClass.getListConstructor(genFeature)%>;