|
|
Re: annotate a parameter in method [message #1053141 is a reply to message #1053092] |
Thu, 02 May 2013 12:15   |
Eclipse User |
|
|
|
solved
def fooMethod(Foo foo){
foo.toMethod(
"foo",
foo.newTypeRef(Void::TYPE),
[
var fooPar = toParameter(foo.bar.simpleName.toFirstLower,form.bar)
foorPar.annotations.add(foo.toAnnotation("foo.annotation.MyBar","foo"))
parameters += fooPar
body=[append('''this.bar = bar;''')]
annotations.add(foo.toAnnotation("foo.annotation.MyFoo"))
])
}
thx Christian
before I tried
var fooPar = toParameter(foo.bar.simpleName.toFirstLower,form.bar)
foorPar.annotations.add(toAnnotation("foo.annotation.MyBar","foo"))
and
var fooPar = toParameter(foo.bar.simpleName.toFirstLower,form.bar)
foorPar.annotations.add(fooPar.toAnnotation("foo.annotation.MyBar","foo"))
still learning
[Updated on: Thu, 02 May 2013 12:24] by Moderator
|
|
|
|
|
|
|
|
|
Re: annotate a parameter in method [message #1053250 is a reply to message #1053179] |
Fri, 03 May 2013 07:42   |
Eclipse User |
|
|
|
Christian Dietrich wrote on Thu, 02 May 2013 17:30Hi,
isnt that the same?
Yes, it is the same... in this simplified example.
I'm learning in small steps
But I will eventualy need a convertion to something like
@Column(name="T_NAME", updatable=false)
and
@RequestMapping(value="/welcome", method = RequestMethod.GET)
I will surely have a look at
Quote:
org.eclipse.xtext.xbase.compiler.JvmModelGenerator.toJava(JvmAnnotationValue, ITreeAppendable, GeneratorConfig)
org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder.toAnnotation(EObject, String, Object)
and create your own toAnnotation
that sets an JvmAnnotationValue.operation
[Updated on: Fri, 03 May 2013 07:43] by Moderator
|
|
|
Re: annotate a parameter in method [message #1053257 is a reply to message #1053250] |
Fri, 03 May 2013 08:10   |
Eclipse User |
|
|
|
Actually, a few weeks ago I digged through the api for solving similar problems, and finally came up with my own utilities.
class Utils {
@Inject extension JvmTypesBuilder
@Inject extension TypesFactory
def addStringValue(JvmAnnotationReference annoRef, String name, String value) {
val op = annoRef.findOperationByName(name)
annoRef.values += createJvmStringAnnotationValue => [
operation = op
values += value
]
annoRef
}
// Similar methods (left to the reader as an exercise ;-) )
// addIntValue(JvmAnnotationReference annoRef, String name, int value)
// addBooleanValue(JvmAnnotationReference annoRef, String name, boolean value)
// addAnnotationValue(JvmAnnotationReference annoRef, String name, JvmAnnotationReference value)
// addEnumValue(JvmAnnotationReference annoRef, EObject context, String name, Enum<?> value)
// addTypeValue(JvmAnnotationReference annoRef, EObject context, String name, Class<?> value)
// ...
def private findOperationByName(JvmAnnotationReference annoRef, String name) {
annoRef.annotation.declaredOperations.findFirst[it.simpleName == name]
}
}
For example, for building the annotation
@NamedQuery(name="Printer.findAll", query="SELECT x FROM Printer AS x")
I can write
@Inject extension JvmTypesBuilder
@Inject extension Utils
val a = e.toAnnotation("javax.persistence.NamedQuery")
.addStringValue("name", "Printer.findAll")
.addStringValue("query", "SELECT x FROM Printer AS x")
|
|
|
|
|
|
Re: annotate a parameter in method [message #1421429 is a reply to message #1421357] |
Thu, 11 September 2014 06:58  |
Eclipse User |
|
|
|
Hi Mohamed,
you can make your method even easier to use with varargs parameter:
def addStringValues(JvmAnnotationReference annoRef, String name, String... newValues) {
val op = annoRef.findOperationByName(name)
annoRef.values += createJvmStringAnnotationValue => [
operation = op
values += newValues
]
annoRef
}
And Xtend's += operator is smart enough to know how to add the array String[] newValues to the List<String> values.
|
|
|
Powered by
FUDForum. Page generated in 0.06615 seconds