Search code examples
javaxtextxtend

Xtext and Xtend - Add annotations before parameters in a method


I am trying to generate a Java code like the following.

public void create(@RequestBody final Resource resource) {
    return;
}

The Inferrer code snippet which I added for providing the @RequestBody is given below.

members += event.toMethod(event.action.name, typeRef(void)) [
    var dataType = map.get(method.action.name)
    parameters += event.toParameter(method.type.parameter.name, dataType.javaType)
    annotations += annotationRef("org.springframework.web.bind.annotation.RequestBody");
    body = '''
        return;
    '''

The resulting generated code is like following.

@RequestBody
public void create(final Resource resource) {
    return;
}

What changes should I make in the xtend code in order to get the annotation before the parameter of method? Like @RequestBody final Resource resource


Solution

  • you have to add the Annotation to the Parameter itself. use a variable or the with clause

    members += entity.toMethod("dummy", Void.TYPE.typeRef) [
    parameters +=  entity.toParameter("someParam", String.typeRef) => [
        annotations += annotationRef("java.lang.Deprecated");
    ]
    body = ''''''
    ]