【问题标题】:Using Inject in an Xtend based Xtext code generator在基于 Xtend 的 Xtext 代码生成器中使用 Inject
【发布时间】:2018-07-20 16:48:59
【问题描述】:

我是 Xtext 和 Xtend 的新手,我正在尝试使用 Xtext 文档中的 Xtext 教程来学习 Xtext。我在带有 Xtext 2.14 的 Java 10 下的 Eclipse Photon 上运行。我正在开始扩展教程,并且很早就遇到了问题。这是我尝试使用代码生成器的代码:

/*
 * generated by Xtext 2.14.0
 */
package net.wiseoldbird.tutorial.domainmodel.generator

import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.AbstractGenerator
import org.eclipse.xtext.generator.IFileSystemAccess2
import org.eclipse.xtext.generator.IGeneratorContext
import net.wiseoldbird.tutorial.domainmodel.domainmodel.Entity
import com.google.inject.Inject

@Inject extension IQualifiedNameProvider;

class DomainmodelGenerator extends AbstractGenerator {

    override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
        for (e: resource.allContents.toIterable.filter(Entity)) {
        fsa.generateFile(e.fullyQualifiedName.toString("/") + ".java", e.compile)

        }
    }
}

这是我的语法文件:

grammar net.wiseoldbird.tutorial.domainmodel.Domainmodel
        with org.eclipse.xtext.common.Terminals

generate domainmodel "http://www.wiseoldbird.net/tutorial/domainmodel/Domainmodel"

Domainmodel :
    (elements+=AbstractElement)*;

PackageDeclaration:
    'package' name=QualifiedName '{'
        (elements+=AbstractElement)*
    '}';

AbstractElement:
    PackageDeclaration | Type | Import;

QualifiedName:
    ID ('.' ID)*;

Import:
    'import' importedNamespace=QualifiedNameWithWildcard;

QualifiedNameWithWildcard:
    QualifiedName '.*'?;

Type:
    DataType | Entity;

DataType:
    'datatype' name=ID;

Entity:
    'entity' name=ID ('extends' superType=[Entity|QualifiedName])? '{'
        (features+=Feature)*
    '}';

Feature:
    (many?='many')? name=ID ':' type=[Type|QualifiedName];

我的问题是 Eclipse 说 @Inject 注释有问题。它说Inject cannot be resolved to an annotation type。这是根据教程中的说明生成的 Eclipse Xtext 项目。

我该如何从这里开始?

【问题讨论】:

标签: xtext


【解决方案1】:

您只能注入字段和初始化方法/构造函数参数

import com.google.inject.Inject
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.example.domainmodel.domainmodel.Entity
import org.eclipse.xtext.generator.AbstractGenerator
import org.eclipse.xtext.generator.IFileSystemAccess2
import org.eclipse.xtext.generator.IGeneratorContext
import org.eclipse.xtext.naming.IQualifiedNameProvider

class DomainmodelGenerator extends AbstractGenerator {

    @Inject extension IQualifiedNameProvider

    override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
        for (e : resource.allContents.toIterable.filter(Entity)) {
            fsa.generateFile(e.fullyQualifiedName.toString("/") + ".java", e.compile)

        }
    }

    def compile(Entity e) '''
        package «e.eContainer.fullyQualifiedName»;

        public class «e.name» {
        }
    '''
}

【讨论】:

  • 为什么快速修复不建议导入?该 jar 在类路径中可用,这是在新的 Eclipse 工作区中进行的干净设置。我想知道 Inject 的意图是什么。
  • 问题是:导入时没有注释。这就是问题
  • 在这种情况下,教程是否应该没有警告或在教程示例中包含导入?毕竟教程应该迎合完整的 Xtext 新手。在使用 Python 15 年后,我正处于学习曲线的中间,这主要是因为 Xtend。
  • 我不明白你的意思:如果你写 class DomainmodelGenerator extends AbstractGenerator { @Inject extension IQualifiedNameProvider } 那么你会得到所有缺失导入的快速修复
  • 我刚刚从教程中添加了下一行,它给出了两个错误,说明字段fullyQualifiedName 和compile 对Entity 是未定义的。我的项目环境似乎有问题,但我不知道它们可能是什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-30
  • 2018-11-15
  • 1970-01-01
相关资源
最近更新 更多