这是用于创建特定于域的语言(DSL)的类项目。没什么好考虑的。对于我自己来说,我把这个项目作为学习所涉及的步骤的一个练习。
所涉及的文件(附所附代码文件):
( 1) Xtext语法(entities.xtext)
grammar org.example.xbase.entities.Entities with org.eclipse.xtext.xbase.Xbase
generate entities "http://www.example.org/xbase/entities/Entities"
Model:
entities+=Entity*;
Entity:
'entity' name=ID ('extends'
superType=JvmParameterizedTypeReference)?
'{'
attributes+=Attribute*
operations+=Operation*
'}';
Attribute:
'attr'(type=JvmTypeReference)? name=ID
('=' initexpression=XExpression)? ';';
Operation:
'op'(type=JvmTypeReference)? name=ID
'(' (params+=FullJvmFormalParameter (','
params+=FullJvmFormalParameter)*)? ')'
body=XBlockExpression;2) JvmModelInferrer (entitiesJvmModelInferrer.xtend)
package org.example.xbase.entities.jvmmodel
import com.google.inject.Inject
import org.eclipse.xtext.xbase.jvmmodel.AbstractModelInferrer
import org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor
import org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder
import org.eclipse.xtext.naming.IQualifiedNameProvider
import org.example.xbase.entities.entities.Entity
class EntitiesJvmModelInferrer extends AbstractModelInferrer {
@Inject extension JvmTypesBuilder
@Inject extension IQualifiedNameProvider
def dispatch void infer(Entity entity, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
acceptor.accept(entity.toClass("Animals."+entity.name)).
initializeLater[
documentation= entity.documentation
if(entity.superType !=null)
superTypes += entity.superType.cloneWithProxies
//Create fields for each attribute in the entity
entity.attributes.forEach[
a | val type = a.type ?: a.initexpression?.inferredType
members += a.toField(a.name,type) [
documentation=a.documentation
if(a.initexpression !=null)
initializer = a.initexpression
]
//create getter method
members +=a.toGetter(a.name,type)
//create setter method
members +=a.toSetter(a.name,type)
]
//Create method for each operation in the entity
entity.operations.forEach[
op|
members+= op.toMethod(op.name,op.type?:inferredType)[
documentation=op.documentation
for (p:op.params){
parameters +=p.toParameter(p.name,p.parameterType)
}
//create a main method in one of the classes, when called. Make sure it is also static!!!
if(op.name.equals('main')){
static=true
}
body=op.body
]
]
]
}
}3)新语言源文件(Animal.xentities和Main.xentities)。.xentities扩展名是特定于此DSL项目的文件的。- Main.xentities
entity Main{
//define an instance of Animal
attr me=new Animal;
op main(){
println('main-func')
}
}-generated Main.java
package Animals;
import Animals.Animal;
import org.eclipse.xtext.xbase.lib.Functions.Function0;
import org.eclipse.xtext.xbase.lib.InputOutput;
@SuppressWarnings("all")
public class Main {
private Animal me = new Function0<Animal>() {
public Animal apply() {
Animal _animal = new Animal();
return _animal;
}
}.apply();
public Animal getMe() {
return this.me;
}
public void setMe(final Animal me) {
this.me = me;
}
public static String main() {
//This is the main function I created to
//have an entry point for program execution
String _println = InputOutput.<String>println("main-func");
return _println;
}
}-Animal.xentities
entity Animal{
//define an equivalent class in my domain-specific language
attr int nlegs;
op printAnimalSound(){
println('I grunt and sniff')
}
}-Generated Animal.java码
package Animals;
import org.eclipse.xtext.xbase.lib.InputOutput;
@SuppressWarnings("all")
public class Animal {
private int nlegs;
public int getNlegs() {
return this.nlegs;
}
public void setNlegs(final int nlegs) {
this.nlegs = nlegs;
}
public String printAnimalSound() {
String _println = InputOutput.<String>println("I grunt and sniff");
return _println;
}
}我的目标是:我想开发一个基本的工作流基础设施,这样我就可以定义具有相关功能和属性的类。那么我想要能够执行他们。
我的问题是:不断收到这样一条消息:‘选择不包含主类型’‘。
我的尝试:在实体(类)‘Main.xEntites’中,我创建了一个'op main()‘函数。在生成的.java代码中,这将显示为一个公共静态字符串main()函数。我以为这样能行。不管怎么说。我不知道缺了什么。欢迎任何帮助。
发布于 2013-11-03 22:38:33
当然,这是行不通的!主方法必须返回void并接受String[] args类型的参数;或
public static void main(String[] args){ ...}为了实现这一点,我需要修改JvmModelInferrer文件。
https://stackoverflow.com/questions/19758058
复制相似问题