简体   繁体   中英

How to add custom import statements in the generated java file in xtext using jvmmodelInferrer?

I have written a grammar for my domain specific language in xtext and I am using jvmmodelInferrer to generate java code. I can generate fields and custom methods but how can I add custom import statements like 'import java.util.*' in the generated java file without the user having to explicitly write the import statement?

you dont generate import strings. you just use rich strings in a proper way and everything happens automatically

def dispatch void infer(Model element, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
    for (greeting : element.greetings) {
        acceptor.accept(greeting.toClass("demo." + greeting.name)) [
            members += greeting.toMethod("demo", Void.TYPE.typeRef) [
                body = '''
                «JFrame» f = null;
                «"java.util.List".typeRef("java.lang.String".typeRef)» l = null;
                return;
                '''
            ]
        ]
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM