我在使用Xtext,Xbase和Java模型推断器从内部类引用容器类时遇到问题。为了强调我的问题,让我首先演示一个有效的示例(源自Bettini的使用Xtext和Xtend 实现域特定语言),我将其称为宽松语法。从实体可以继承任何Java类(而不是仅实体)和属性由任何Java类(而不仅仅是实体)键入的意义上讲,这是宽松的。
此问题仅涉及将语法仅适用于实体。
由于对Grammatically restricted JVMModelInferrer inheritence with Xtext and XBase和Xbase: Fields in generated inner class not recognized, no problems as class in own file的回答,我可以完美地实现这一点,除非内部类引用包含类。这个问题只是关于内部阶级的情况。
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: Fields in generated inner class not recognized, no problems as class in own file向模型推断者添加了辅助方法
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的引用均无效。
虽然我可以解决此问题,但使用命名约定显式外部化内部类并更改名称/作用域?提供者,我想知道是否有一个简单的解决方案。