我在使用 Xtext、Xbase 和 Java 模型推断器从内部类引用容器类时遇到问题。为了突出我的问题,让我首先演示一个工作示例(源自Bettini使用 Xtext 和 Xtend 实现特定领域的语言),我称之为松散语法。从某种意义上说,实体可以继承任何 Java 类(而不是只有实体)并且属性由任何 Java 类(而不仅仅是实体)类型化,这是松散的。
这个问题只涉及将这个语法收紧到实体。
感谢对带有 Xtext 和 XBase和Xbase 的语法限制 JVMModelInferrer 继承的答案:未识别生成的内部类中的字段,作为自己的文件中的类没有问题,我可以完美地实现这一点,除非在内部类引用包含类的情况下。这个问题只是关于内部阶级的情况。
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.xbase.Xbase
generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
ModelDeclaration:
importSection=XImportSection?
packageDeclaration=PackageDeclaration;
PackageDeclaration:
'package' name=QualifiedName ';'?
model=Model;
Model:
'model' name=ID '{'
(
entities+=Entity
)+
'}';
Entity:
'entity' name=ID ('extends' superType=JvmParameterizedTypeReference)? '{'
(attributes+=Attribute
|
entities+=Entity
)*
'}';
Attribute:
'attr' (type=JvmTypeReference) name=ID ';';
下面的 JvmModelInferrer 完美地为模型伪造了一个类,实体作为内部类,它们本身可能包含内部类。所有生成的内部类都是静态的。
package org.xtext.example.mydsl.jvmmodel
import com.google.inject.Inject
import org.eclipse.xtext.common.types.JvmGenericType
import org.eclipse.xtext.naming.IQualifiedNameProvider
import org.eclipse.xtext.xbase.jvmmodel.AbstractModelInferrer
import org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor
import org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder
import org.xtext.example.mydsl.myDsl.Entity
import org.xtext.example.mydsl.myDsl.Model
class MyDslJvmModelInferrer extends AbstractModelInferrer {
@Inject extension JvmTypesBuilder
@Inject extension IQualifiedNameProvider
def dispatch void infer(Model model, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
acceptor.accept(
model.toClass(model.fullyQualifiedName)
) [
model.entities.forEach [ entity |
members += entity.toClass(entity.fullyQualifiedName) [ jEntity |
forgeEntities(model, jEntity, entity)
]
]
]
}
protected def void forgeEntities(Model model, JvmGenericType it, Entity entity) {
static=true
documentation = entity.documentation
if (entity.superType !== null) {
superTypes += entity.superType
}
entity.attributes.forEach [ a |
val type = a.type
members += a.toField(a.name, type) [
documentation = a.documentation
]
members += a.toGetter(a.name, type)
members += a.toSetter(a.name, type)
]
entity.entities.forEach [ innerEntity |
members += innerEntity.toClass(innerEntity.fullyQualifiedName) [ jInnerEntity |
forgeEntities(model, jInnerEntity, innerEntity)
]
]
}
}
例如,实例
package test
model Test {
entity A {}
entity B {
attr Test.A myA;
}
entity C extends test.Test.A {}
entity D {
attr Test.A myA;
entity I {}
entity J extends test.Test.D {}
entity K {
attr Test.D myD;
}
}
}
正确推断
package test;
@SuppressWarnings("all")
public class Test {
public static class A {
}
public static class B {
private Test.A myA;
public Test.A getMyA() {
return this.myA;
}
public void setMyA(final Test.A myA) {
this.myA = myA;
}
}
public static class C extends Test.A {
}
public static class D {
private Test.A myA;
public Test.A getMyA() {
return this.myA;
}
public void setMyA(final Test.A myA) {
this.myA = myA;
}
public static class I {
}
public static class J extends Test.D {
}
public static class K {
private Test.D myD;
public Test.D getMyD() {
return this.myD;
}
public void setMyD(final Test.D myD) {
this.myD = myD;
}
}
}
}
现在考虑下面的语法紧缩。
...
Entity:
'entity' name=ID ('extends' superType=[Entity|QualifiedName])? '{'
(attributes+=Attribute
|
entities+=Entity
)*
'}';
Attribute:
'attr' (type=[Entity|QualifiedName]) name=ID ';';
我添加的模型推断器基于Xbase:生成的内部类中的字段无法识别,作为自己文件中的类没有问题,辅助方法
def String getJavaTypeName(Entity entity) {
val parent = entity.eContainer
if (parent instanceof Model) {
return (parent.fullyQualifiedName.toString+"$"+entity.name)
}
if (parent instanceof Entity) {
return getJavaTypeName(parent)+"$"+entity.name
}
throw new RuntimeException("Impossible")
}
def JvmTypeReference getJavaTypeRef(Entity entity) {
getJavaTypeName(entity).typeRef
}
并将 forgeEntities 方法更改为
protected def void forgeEntities(Model model, JvmGenericType it, Entity entity) {
static=true
documentation = entity.documentation
println("### FORGING [" + entity.name + "]")
if (entity.superType !== null) {
superTypes += entity.superType.javaTypeRef
}
entity.attributes.forEach [ a |
val type = a.type.javaTypeRef
members += a.toField(a.name, type) [
documentation = a.documentation
]
members += a.toGetter(a.name, type)
members += a.toSetter(a.name, type)
]
entity.entities.forEach [ innerEntity |
members += innerEntity.toClass(innerEntity.fullyQualifiedName) [ jInnerEntity |
forgeEntities(model, jInnerEntity, innerEntity)
]
]
}
对于同一个模型(虽然现在可以收紧参考)
package test
model Test {
entity A {}
entity B {
attr A myA;
}
entity C extends A {}
entity D {
attr A myA;
entity I {}
entity J extends D {}
entity K {
attr D myD;
}
}
}
生成以下内容。
package test;
@SuppressWarnings("all")
public class Test {
public static class A {
}
public static class B {
private Test.A myA;
public Test.A getMyA() {
return this.myA;
}
public void setMyA(final Test.A myA) {
this.myA = myA;
}
}
public static class C extends Test.A {
}
public static class D {
private Test.A myA;
public Test.A getMyA() {
return this.myA;
}
public void setMyA(final Test.A myA) {
this.myA = myA;
}
public static class I {
}
public static class J implements test.Test$D {
}
public static class K {
private test.Test$D myD;
public test.Test$D getMyD() {
return this.myD;
}
public void setMyD(final test.Test$D myD) {
this.myD = myD;
}
}
}
}
虽然 A、B 和 C 的一切都按预期工作,表明“$”正在工作,但 D 类失败。不同之处在于内部类是指它的包含类。这是过程失败的时候。D 对 J 的扩展和从 myD 到 K 中的 D 的引用都不起作用。
虽然我可以解决这个问题,但使用命名约定显式外部化内部类并更改名称/范围?提供者,我想知道是否有一个简单的解决方案。