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

我尝试使用 Papyrus 中已包含的 Alf 解析器 (org.eclipse.papyrus.uml.alf.*)。所以我尝试按所写实例化解析器 这里:

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 插件(关联)):

有帮助吗?

解决方案

正如你所看到的,该方法 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,从而避免异常。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top