How to JvmModelInferrer method body from XExpression and append boilerplate code

好久不见. 提交于 2021-01-29 09:50:37

问题


In a JvmModelInferrer, when constructing the body of a method or constructor, how do you insert both an XExpression from the grammar

body = op.body

and additional "boilerplate" code, for example

body = [
    append(
    '''
        System.out.println("BOILERPLATE");
    '''
    )
]

I can achieve either but not both.

For a minimal working example, consider the following canonical Xbase grammar,

grammar org.example.xbase.entities.Entities with org.eclipse.xtext.xbase.Xbase

generate entities "http://www.example.org/xbase/entities/Entities"

Model:
    importSection=XImportSection?
    entities+=Entity*;

Entity:
    'entity' name=ID ('extends' superType=JvmParameterizedTypeReference)? '{'
        attributes += Attribute*
        operations += Operation*
    '}';

Attribute:
    'attr' (type=JvmTypeReference)? name=ID ('=' initexpression=XExpression)? ';';

Operation:
    'op' (type=JvmTypeReference)? name=ID 
    '(' (params+=FullJvmFormalParameter (',' params+=FullJvmFormalParameter)*)? ')' 
        body=XBlockExpression;

and JvmModelInferrer,

package org.example.xbase.entities.jvmmodel

import com.google.inject.Inject
import org.eclipse.xtext.xbase.jvmmodel.AbstractModelInferrer
import org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor
import org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder
import org.example.xbase.entities.entities.Entity

class EntitiesJvmModelInferrer extends AbstractModelInferrer {

    @Inject extension JvmTypesBuilder

    def dispatch void infer(Entity entity, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
        acceptor.accept(entity.toClass("entities."+entity.name)) [
            documentation = entity.documentation
            if (entity.superType != null)
                superTypes += entity.superType.cloneWithProxies
            entity.attributes.forEach[
                a | 
                val type = a.type ?: a.initexpression?.inferredType
                members += a.toField(a.name, type) [
                    documentation = a.documentation
                    if (a.initexpression != null)
                        initializer = a.initexpression
                ]
                members += a.toGetter(a.name, type)
                members += a.toSetter(a.name, type)
            ]
            entity.operations.forEach[
                op |
                members += op.toMethod(op.name, op.type ?: inferredType) [
                    documentation = op.documentation
                    for (p : op.params) {
                        parameters += p.toParameter(p.name, p.parameterType)
                    }
//              body = [
//                  append(
//                  '''
//                      System.out.println("BOILERPLATE");
//                  '''
//                  )
//              ]
                body = op.body
                ]
            ]
        ]
    }
}

As the comments suggest, I would like to insert "boilerplate" code into the body of the method, before the XExpression itself. While I can insert the boilerplate, or the expression, I cannot work out how to do both.


回答1:


this does not work. the only thing you can do is to infer two methods

methodWithBoilerplate() {
    //pre
    methodwithoutboilerplate
    //post
}
methodwithoutboilerplate() {
    usercode goes here
}
  • for the first use body = '''code here'''
  • for the second use body = exp.body


来源:https://stackoverflow.com/questions/52735755/how-to-jvmmodelinferrer-method-body-from-xexpression-and-append-boilerplate-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!