Search code examples
javareflectionemfecore

ECore reflection and cross references


To have a context, I am currently working on an ecore to java model transformation. Practically, I am reading some ecore file and generate a string which happens to be a valid java interface source code.

As a example, here is my code generation workflow.

projectA.ecore:

Defines an EClass 'A'

package projectA : projectA = 'http://www.example.org/projectA'
{
    class A;
}

projectB.ecore:

Defines an EClass 'B' which inherit from 'A' using cross-reference to a.ecore to get access to it.

import projectA : '../../projectA/model/projectA.ecore#/';

package projectB : projectB = 'http://www.example.org/projectB'
{
    class B extends projectA::A;
}

From those ecore I first generate an interface for projectA.ecore:

package projecta;

interface ProjectA<A> {
  // ...
}

And now I have want to do the same thing for projectB.ecore and obtains the following interface:

package projectb;

import projecta.ProjectA;

interface ProjectB<A,B> extends ProjectA<A> {
  // ...
}

To do so I need to detect that A is and EClass accessed using cross reference and do some analysis in projectA.ecore in order to generate a valid interface extension, packages imports...

I looked around in the ecore reflection API without finding a clean and obvious way to do so. Is this possible? It yes, how?

EDIT: Technical details

I'm loading the ecore using this kind of code :

final ResourceSetImpl resourceSet = new ResourceSetImpl();
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("ecore", new XMIResourceFactoryImpl());
final Resource resource = resourceSet.getResource(uri, true);
final EPackage ePackage = (EPackage) resource.getContents().get(0);
final String fileContent = new GenerateAlgebra().process(ePackage);

GenerateAlgebra is the class dedicated to the .ecore to String transformation. Technically it is developed using Xtend (https://github.com/manuelleduc/ecore-oa/blob/master/fr.inria.diverse.ecorealgebragenerator/src/fr/inria/diverse/objectalgebragenerator/popup/actions/GenerateAlgebra.xtend).


Solution

  • If you just want to get the file path to the ecore file in which the EClass is defined use the resource URI

    try:

    ePackage.eResource().getURI() which gives you the actual URI to the ecore file in which the package and all its EClasses are defined. something like: file:/Users/../../yourPath/projectA.ecore You can also use the getNsURI(), getName() to identify the two ecore files.

    If you use a Resourceset, like you did, and you have cross references between several ecore files, then the set tries to load all of the other Resources aswell. Which means, by calling resourceSet.getResource(uri, true) the resourceset should contain both resources.

    try iterating through resourceSet.getResources()