简体   繁体   中英

how to use type annotations in xtext?

How can I add a field like the following using the JvmModelInferrer?

public final @IdInstance long id;

What I already have is this:

members += domainId.toField('id', Long.TYPE.typeRef()) [
    visibility = JvmVisibility.PUBLIC
    final = true
]

which produces this java code:

public final long id;

But I cannot figure out how to add the IdInstance annotation to the type.

Note: adding the annotation to the field works, but is not what I want:

members += domainId.toField('id', Long.TYPE.typeRef()) [
    visibility = JvmVisibility.PUBLIC
    final = true
    annotations += 'com.tmtron.dscontrol2.qual.IdInstance'.annotationRef()
]

produces:

@IdInstance
public final long id;

xbase does not support type annotations yet: see xtext#218

But there is an easy way to generate what we want:

JvmTypeReferenceBuilder.typeRef('@IdInstance long')

This will create a JvmUnknownTypeReference which just writes the provided string literally to the output (so make sure to get the imports right or use the fully qualified annotation).

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