Question

I am trying to write a plugin for Papyrus that converts Alf code.

I tried to use the Alf-parser that is already included in Papyrus (org.eclipse.papyrus.uml.alf.*). So I tried to instantiate the parser as written here:

public class Activator extends Plugin {

    // default Activator code here ...

    public String ConvertAlfToSpecSharp(String alf)
    {
        new org.eclipse.emf.mwe.utils.StandaloneSetup().setPlatformUri("../");
        Injector injector = new AlfStandaloneSetup().createInjectorAndDoEMFRegistration();
        XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
        resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
        // ...
    }
}

But the first line (new org.eclipse.emf.mwe.utils.StandaloneSetup().setPlatformUri("../");) throws the following exception:

com.google.inject.CreationException: Guice creation errors:

1) Error injecting method, java.lang.IllegalStateException: No EPackages were registered for the validator org.eclipse.papyrus.uml.alf.validation.CommonJavaValidator please override and implement getEPackages().
at org.eclipse.xtext.validation.AbstractInjectableValidator.register(Unknown Source)
at org.eclipse.xtext.service.MethodBasedModule.configure(MethodBasedModule.java:55)
while locating org.eclipse.papyrus.uml.alf.validation.CommonJavaValidator

1 error
    at com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:435)
    at com.google.inject.internal.InternalInjectorCreator.injectDynamically(InternalInjectorCreator.java:183)
    at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:109)
    at com.google.inject.Guice.createInjector(Guice.java:95)
    at com.google.inject.Guice.createInjector(Guice.java:72)
    at com.google.inject.Guice.createInjector(Guice.java:62)
    at org.eclipse.papyrus.uml.alf.CommonStandaloneSetupGenerated.createInjector(CommonStandaloneSetupGenerated.java:28)
    at org.eclipse.papyrus.uml.alf.CommonStandaloneSetupGenerated.createInjectorAndDoEMFRegistration(CommonStandaloneSetupGenerated.java:22)
at org.eclipse.papyrus.uml.alf.CommonStandaloneSetup.doSetup(CommonStandaloneSetup.java:23)
    at org.eclipse.papyrus.uml.alf.AlfStandaloneSetupGenerated.createInjectorAndDoEMFRegistration(AlfStandaloneSetupGenerated.java:20)
    at <packagenamehere>.Activator.ConvertAlfToSpecSharp(Activator.java:113)

I have no idea how to solve this, especially since I find it very hard to debug eclipse applications...

Update: Here are links to some relevant classes (all from the org.eclipse.papyrus.uml.alf.common plugin of Papyrus plugins (link)):

Was it helpful?

Solution

As you can see, the method getEPackages() of AbstractCommonJavaValidator.java returns an empty list.

If you look at this AbstractAlfJavaValidator implementation, there is an EPackage which is added to the list.

As a solution, i think you should edit CommonJavaValidator.java and override getEPackages() in order to add an EPackage:

@Override
protected List<EPackage> getEPackages() {
    List<EPackage> result = super.getEPackages();
    // result.add(org.eclipse.papyrus.uml.alf.alf.AlfPackage.eINSTANCE);
    // Edit
    result.add(org.eclipse.emf.ecore.EcorePackage.eINSTANCE);
    return result;
}

Edit

If you can't edit papyrus plugins, it think you should add the following before your code:

if (!EPackage.Registry.INSTANCE.containsKey("http://www.eclipse.org/papyrus/alf/Alf")) { EPackage.Registry.INSTANCE.put("http://www.eclipse.org/papyrus/alf/Alf", org.eclipse.papyrus.uml.alf.alf.AlfPackage.eINSTANCE); }

It will add an EPackage before the guice creation and the exception will avoided.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top