[英]Example for xtend toEnumerateType / generate Java enum via xtend?
I need to generate Java enums via DSL, using xtend 2.5. 我需要使用xtend 2.5通过DSL生成Java枚举。 My grammar looks something like this:
我的语法看起来像这样:
grammar de.mytest.xtext.enumdsl.EnumDsl with org.eclipse.xtext.xbase.annotations.XbaseWithAnnotations
import "http://www.eclipse.org/emf/2002/Ecore" as ecore
import "http://www.eclipse.org/xtext/xbase/Xbase" as xbase
EnumModel:
pckg=EnumPackage;
EnumPackage:
{EnumPackage}
importSection=XImportSection?
'package' name=QualifiedName '{'
(enums+=MyEnum)*
'}';
MyEnum:
'enum' name=ValidID '{' (items+=ValidID)* '}';
With this grammar the DSL could look like this: 通过这种语法,DSL可能如下所示:
package de.mytest.enumpackage {
enum MyEnumTest {
YES
NO
MAYBE
}
}
The resulting Java enum expected would be: 预期的Java枚举结果将是:
package de.mytest.enumpackage;
public enum MyEnumTest {
YES,
NO,
MAYBE
}
I tried to use implement the xtend method infer() using toEnumerateType(), but without success. 我试图使用toEnumerateType()实现xtend方法infer(),但没有成功。 Also I couldn't find any examples for that !
我也找不到任何例子 ! My google-fu doesn't seem to be good enough!
我的Google Fu似乎还不够好!
Using toEnumerateLiteral(), which also works inside toClass(), but it generates a class obviously... 使用toEnumerateLiteral(),它也可以在toClass()内部使用,但显然会生成一个类...
Do you have any ideas? 你有什么想法?
Thanks in advance, Michael 预先感谢迈克尔
This should do the trick for you: 这应该为您解决问题:
myEnum.toEnumerationType(myEnum.name) [
myEnum.items.forEach [ literal |
myEnum.toEnumerationLiteral(literal)
]
]
Thank you very much for hint, Sebastian. 塞巴斯蒂安,非常感谢您的提示。 I had to modify it a little.
我不得不对其进行一些修改。 Here is the method, that worked for me finally :-)
这是终于对我有用的方法:-)
class EnumDslJvmModelInferrer extends AbstractModelInferrer {
@Inject extension JvmTypesBuilder
def dispatch void infer(EnumModel element, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
element.pckg.enums.forEach[ myEnum |
acceptor.accept(myEnum.toEnumerationType(element.pckg.name+"."+myEnum.name)[]).initializeLater[
myEnum.items.forEach [ literal |
it.members += myEnum.toEnumerationLiteral(literal)
]
]
]
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.