尝试将Alf Xtext解析器用作Standalone时出现Guice错误

时间:2014-07-29 12:35:50

标签: eclipse guice xtext papyrus

我正在尝试为Papyrus编写一个转换Alf代码的插件。

我尝试使用已经包含在Papyrus中的Alf-parser(org.eclipse.papyrus.uml.alf。*)。所以我尝试将解析器实例化为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);
        // ...
    }
}

但是第一行(new org.eclipse.emf.mwe.utils.StandaloneSetup().setPlatformUri("../");)抛出以下异常:

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)

我不知道如何解决这个问题,特别是因为我发现调试eclipse应用程序非常困难......

更新: 以下是一些相关类的链接(全部来自Papyrus插件的org.eclipse.papyrus.uml.alf.common插件(link)):

1 个答案:

答案 0 :(得分:1)

如您所见,AbstractCommonJavaValidator.java的方法getEPackages()返回一个空列表。

如果你看一下这个AbstractAlfJavaValidator实现,就会有一个EPackage添加到列表中。

作为一个解决方案,我认为您应该编辑CommonJavaValidator.java并覆盖getEPackages()以添加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;
}

修改

如果您无法编辑纸莎草插件,则认为您应该在代码之前添加以下内容:

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); }

它将在guice创建之前添加一个EPackage,并且将避免异常。