Question

I'd like to generate a List field into my class generated from my DSL and initialize it like this:

private List<MyObject> myObjects= Lists.newArrayList();

The only way I know for this, is to append some text to the initializer:

members += appRule.toField("myObjects", appRule.newTypeRef(List, it.newTypeRef(MyObject))) [
     initializer = [append('''Lists.newArrayList()''')]
]

However, using this approach the JvmModelInferrer won't import the Guava Strings library, thus will raise compilation issues. Is there any way to overcome this obstacle?

Was it helpful?

Solution

If I understand your issue (as you are referring to the Guava Strings library that is not used in the code :) ), your problem is, that the class reference Lists is not imported.

For such constructs, we have a helper method in EMF-IncQuery that serializes a type reference the same way parameters are serialized. This functionality relies on the injectable TypeReferenceSerializer class.

def referClass(ITreeAppendable appendable, EObject ctx, Class<?> clazz, JvmTypeReference... typeArgs) {
    val ref = ctx.newTypeRef(clazz, typeArgs)
    if (ref != null) {
        appendable.serialize(ref, ctx)
    } else {
        //Class resolution error - error handling required here
        //A fallback to writing out the fqn of the class
        appendable.append(clazz.canonicalName)
    }
}

def serialize(ITreeAppendable appendable, JvmTypeReference ref, EObject ctx) {
    typeReferenceSerializer.serialize(ref, ctx, appendable)     
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top