0

How can I implement this method with Xtend?

private void myListenerMethod(@Observes(notifyObserver=IF_EXISTS) @MyAnnotation Boolean value) 
{
    ... Do somehting ...
}

I have here an example for a normal method implementation:

    var method_myListenerMethod = toMethod("myListenerMethod", newTypeRef(void), [
    '''
        ... Do something ...
    '''
    ])
    method_myListenerMethod.parameters += toParameter("value",newTypeRef('''javax.enterprise.event.Observes'''))
    members += method_doSaveOperation
nik the lion
  • 458
  • 1
  • 9
  • 22

1 Answers1

0

I'm not sure what your methods toMethod and toParameter mean, but I would do generation of your method declaration as follows:

private def addMethod(MutableFieldDeclaration field, extension TransformationContext context) {
    field.declaringType.addMethod("myListenerMethod") [ methodDecl |
        methodDecl.returnType = newTypeReference(typeof(void))
        val param = methodDecl.addParameter("value", newTypeReference(typeof(Boolean)))
        val observes = param.addAnnotation(context.findTypeGlobally("javax.enterprise.event.Observes"))
        val reception = context.findTypeGlobally("javax.enterprise.event.Reception") as EnumerationTypeDeclaration
        observes.setEnumValue("notifyObserver", reception.findDeclaredValue("IF_EXISTS"))
        param.addAnnotation(context.findTypeGlobally("MyAnnotation"))
    ]
}

This requires at least Xtend 2.5.0 (see bug #403789)

ZhekaKozlov
  • 36,558
  • 20
  • 126
  • 155