如何使用Xtend实现此方法?
private void myListenerMethod(@Observes(notifyObserver=IF_EXISTS) @MyAnnotation Boolean value)
{
... Do somehting ...
}
我这里有一个正常方法实现的例子:
var method_myListenerMethod = toMethod("myListenerMethod", newTypeRef(void), [
'''
... Do something ...
'''
])
method_myListenerMethod.parameters += toParameter("value",newTypeRef('''javax.enterprise.event.Observes'''))
members += method_doSaveOperation
答案 0 :(得分:0)
我不确定您的方法toMethod
和toParameter
是什么意思,但我会按如下方式生成您的方法声明:
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"))
]
}
这至少需要Xtend 2.5.0(参见bug #403789)