How can I protect my methods bodies (not the attached JavaDoc and Signature) using Acceleo code-generator

StackOverflow https://stackoverflow.com/questions/11036470

Domanda

I use Acceleo in order to generate code with a model I have made. I managed to protect my methods in order to protect them usinig "@generated NOT" in case I need to regenerate my code with Acceleo. The problem is that adding @generated NOT protect all the method content, that is to say the body, the signature and JavaDocs.

The thing is that I only need to keep the method body, or at least the method body and its signature, but I need the doc to be updated. How can I do this ?





Just for information here is an example of a potential generated class :

/*
 * @generated
 */
public class ActeurRefEntrepriseServicesImpl implements ActeurRefEntrepriseServices {

    @Autowired
    HelloWorldService helloWorldService;

    /**
     * Service which say hello
     * 
     * @param name
     *           user name
     * @return print Hello username
     * 
     * @generated NOT
     */
    @Override
    public void sayHello(final String name) {
        helloWorldService.print(name);
    }
}
È stato utile?

Soluzione

Baptiste,

The @generated tags use the standard EMF protection rules : "@generated" means that the body of the block for which it is set will be generated, anything else means no re-generation. If you set something as "@generated" in any of your metamodels' generated code, you will see that there, too, the javadoc is preserved whatever the edits you do.

In short, you cannot tell EMF to re-generate anything other than the code itself.

If you need to have the body protected but not the javadoc, you have to shift from the "@generated" protection to Acceleo's [protected] blocks. i.e, change your template from :

[template generatedMethod(methodName : String)]
    /**
     * Some doc.
     * @param param1
     *           param documentation.
     * @generated
     */
    [generateSignature(methodName)/] {
        [generateBody()/]
    }
[/template]

to something using a protected block :

[template generatedMethod(methodName : String)]
    /**
     * Some doc.
     * @param param1
     *           param documentation.
     */
    [protected (methodName)]
    [generateSignature(methodName)/] {
        [generateBody()/]
    }
    [/protected]
[/template]

With this paradigm, anything that is outside of the protected area will be regenerated, everything else will remain untouched by a regeneration.

See also the full documentation available from the Acceleo website.

If you absolutely need to use the "@generated" protection method for your model, you will need to tamper with the JMerger API from EMF and alter the launcher Acceleo generated for you in order to use your own merging strategy (see the getGenerationStrategy method from that launcher). Note that this is by no means an easy task.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top