在运行时配置Xtext生成器

问题描述 投票:0回答:2

我已经使用Xtext实现了DSL,我正在尝试找到一种方法来动态配置mydsl.ui Eclipse插件中的代码生成。

我引入了一个首选项参数来配置生成器。

我向MyDslRuntimeModule注入了一个自定义的GeneratorConfiguration对象

然后我在自定义BuilderParticipant(在plugin.xml中配置)的“build”方法中设置此对象中的首选项参数。

// In mydsl plugin    
class MyDslRuntimeModule extends AbstractMyDslRuntimeModule {

    def Class<? extends IGeneratorConfiguration> bindIGeneratorConfiguration() {
        return GeneratorConfiguration;
   }

}

// In mydsl.ui plugin
package mydsl.ui;

public class MyBuildPartecipant extends BuilderParticipant {

@Inject IGeneratorConfiguration generatorConfiguration;

@Override
public void build(IBuildContext context, IProgressMonitor monitor) throws CoreException {

    ScopedPreferenceStore scopedPreferenceStore = new ScopedPreferenceStore(InstanceScope.INSTANCE, "ID");

    generatorConfiguration.setGeneratorProperty(scopedPreferenceStore.getInt("myDslProperty"));

    super.build(context, monitor);
}

// In mydsl plugin
class MyDslGenerator extends AbstractGenerator {

@Inject IGeneratorConfiguration generatorConfiguration;

    override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {

        println("Compiling with " + generatorConfiguration.generatorProperty)  

结果是通过mydsl.ui插件(eclipse ui)的类MyBuildPartecipant中的@Inject装饰器获得的GeneratorConfiguration对象与mydsl插件(Xtext生成器插件)的类MyDslGenerator中获得的不同。

如何将eclipse ui插件中的参数传递给Xtext生成器插件(非ui插件)以动态配置代码生成?

谢谢保罗

eclipse generator xtext inject preference
2个回答
0
投票

我解决了:

// In mydsl plugin    
class MyDslRuntimeModule extends AbstractMyDslRuntimeModule {

    def IGeneratorConfiguration bindIGeneratorConfiguration() {
        return new GeneratorConfiguration();
   }

0
投票

你应该用GeneratorConfiguration标记@Singleton类。

或使用configure方法添加单例绑定

def void configureIGeneratorConfiguration(Binder binder) {
    binder.bind(IGeneratorConfiguration).to(GeneratorConfiguration).in(Scopes.SINGLETON)
} 

或使用@SingletonBinding注释类绑定

@SingletonBinding
def Class<? extends IGeneratorConfiguration> bindIGeneratorConfiguration() {
    GeneratorConfiguration
}

如果将内容注入Generator Configuration类,则执行此操作的方式将不起作用

© www.soinside.com 2019 - 2024. All rights reserved.