我正在使用Active Annotations在我的类中生成字段,并且很难发现如何扩展字段初始化表达式。没有初始化程序,代码看起来像这样
class JavaFxPropertyProcessor implements TransformationParticipant<MutableFieldDeclaration>{
def private transform(MutableFieldDeclaration field, extension TransformationContext context){
fields.forEach[transform(context)]
}
def private transform(MutableFieldDeclaration field, extension TransformationContext context){
val clazz = field.declaringType as MutableClassDeclaration
val theClass = SomeJavaClass //any non-parameterised java class here
clazz.addField("myField")[
type = theClass.newTypeReference
initializer = ['''new <<toJavaCode(theClass.newTypeReference()>>(this)''']
]
}
}
当字段没有初始化程序时,这可以正常工作。有时候,我希望该字段有一个初始化程序,只需将初始化程序作为字符串转储到表达式
中,对于int
和bool
文字都可以正常工作
val theInitializer = field.initializer //this is not null
clazz.addField("myField2")[
type = theClass.newTypeReference
initializer = [
'''new <<toJavaCode(theClass.newTypeReference()>>(this, <<theInitializer.toString>>)'''
]
]
当初始值设定项是列表文字时,例如说#[1,2,3]
,那么显然天真的initializer.toString
技术不起作用,因为它创建了诸如
val myField = new List<Integer>(#[1,2,3]);
那么我如何评估/扩展field.initializer
(类型为org.eclipse.xtend.lib.macro.expression
)以便为我的初始化程序创建它?