I am using Active Annotations to generate fields in my classes and am having difficulty discovering how to expand field initializer expressions. Without initializers the code looks like this
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)''']
]
}
}
This works fine when the field has no initializer. Ocassionally, I would like the field to have an initializer and that works fine for int
and bool
literals by just dumping the initializer as a string into the expression
val theInitializer = field.initializer //this is not null
clazz.addField("myField2")[
type = theClass.newTypeReference
initializer = [
'''new <<toJavaCode(theClass.newTypeReference()>>(this, <<theInitializer.toString>>)'''
]
]
When the initializer is a list literal, say #[1,2,3]
for example, then clearly the naive initializer.toString
technique doesn't work since it creates java code such as
val myField = new List<Integer>(#[1,2,3]);
So how to I get to evaluate/expand the field.initializer
(which is of type org.eclipse.xtend.lib.macro.expression
) in order to java-ise it for my initializers?