0

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

我尝试使用已经包含在 Papyrus (org.eclipse.papyrus.uml.alf.*) 中的 Alf-parser。所以我试图实例化解析器,如下所示

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 插件(链接)):

4

1 回答 1

1

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

如果您查看此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 并避免异常。

于 2014-07-30T10:36:58.897 回答