0

all I was migrating my project from xtend 2.4.3 to 2.7.3

I got some problems. Here is the code works on 2.4.3

val AttrType = findTypeGlobally(typeof(Attr));
val fld = Cls.addField(Pkt::getMemberName(m)) 
[
    val annot = addAnnotation(AttrType )
    annot.set("value", GenHelper::getAnnot(m))
    visibility = Visibility::PUBLIC
]
setFieldType(m, fld, context)

On 2.7.3, addAnnotation returns AnnotationReference.

There is no way to set value into the AnnotationReference. How to fix it?

Thanks.

Chi Shin Hsu
  • 261
  • 1
  • 2
  • 12

1 Answers1

1

Use the addAnnotation method that takes a lambda to initialize the annotation reference. In that lambda, you have access to an AnnotationReferenceBuildContext to set values.

val AttrType = findTypeGlobally(typeof(Attr))
val fld = Cls.addField(Pkt::getMemberName(m)) 
[
    val annot = addAnnotation(AttrType) [
        set("value", GenHelper::getAnnot(m))
    ]
    visibility = Visibility::PUBLIC
]
...

Also note:

  1. typeOfhas become obsolete. You can use Attr directly to refer to the class.
  2. :: for static member access can be replaced by a simple . now.
  3. If the class Attris on the classpath of both, the active annotation project and the client project, you don't have to use `findTypeGlobally.
  4. static (extension) imports could help you write even more concise code, e.g.

So your code could become

import static extension <whereever>.Pkt.*
import static extension <whereever>.GenHelper.*
import static org.xtend.lib.macro.declaration.Visibility.*
...

Cls.addField(m.memberName) [
    Attr.addAnnotation [
        'value'.set(m.annot)
    ]
    visibility = PUBLIC
    type = m.fieldType(context) // might need further refactoring
]

...

Jan Koehnlein
  • 211
  • 1
  • 2