Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » annotate a parameter in method(annotations)
annotate a parameter in method [message #1053092] Thu, 02 May 2013 12:07 Go to next message
Dirk Deyne is currently offline Dirk DeyneFriend
Messages: 8
Registered: May 2013
Junior Member
I'm using a AbstractModelInferrer to generate java-code from dsl
I want to insert a method to the generated class like:

  @MyFoo
  public void foo(@MyBar(value="foo") Bar bar){
    this.bar = bar;
  }



But I just cannot get the @MyBar(value="foo") inserted Confused

here is a part of my FooBarInferrer.xtend

class FooBarInferrer extends AbstractModelInferrer {
  @Inject extension JvmTypesBuilder
  ...

  def dispatch void infer(Foo foo, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
  ...
  acceptor.accept(foo.toClass(foo.name)).initializeLater([
     members+= foo.fooMethod
     ...
  ])

  def fooMethod(Foo foo){
       	foo.toMethod(
   	"foo", 
  	foo.newTypeRef(Void::TYPE), 
        [	
	   parameters += toParameter(form.bar.simpleName.toFirstLower,form.bar)
           // how do I add annotation  @MyBar(value="foo") to parameters!?
           body=[append('''this.bar = bar;''')]
	   annotations.add(form.toAnnotation("foo.annotation.MyFoo"))
        ])
  }

}


note that anotation has a parameter too value=bar
Re: annotate a parameter in method [message #1053140 is a reply to message #1053092] Thu, 02 May 2013 16:10 Go to previous messageGo to next message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 6552
Registered: July 2009
Senior Member
val p =toParameter(form.bar.simpleName.toFirstLower,form.bar)
p.annotations += ....
parameters += p
Re: annotate a parameter in method [message #1053141 is a reply to message #1053092] Thu, 02 May 2013 16:15 Go to previous messageGo to next message
Dirk Deyne is currently offline Dirk DeyneFriend
Messages: 8
Registered: May 2013
Junior Member
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 Embarrassed

[Updated on: Thu, 02 May 2013 16:24]

Report message to a moderator

Re: annotate a parameter in method [message #1053142 is a reply to message #1053140] Thu, 02 May 2013 16:18 Go to previous messageGo to next message
Thomas Fritsch is currently offline Thomas FritschFriend
Messages: 20
Registered: April 2013
Location: Germany
Junior Member
or equivalently, but a slightly more concise
parameters += toParameter(form.bar.simpleName.toFirstLower,form.bar) => [
   annotations += ....
]
Re: annotate a parameter in method [message #1053144 is a reply to message #1053142] Thu, 02 May 2013 16:29 Go to previous messageGo to next message
Dirk Deyne is currently offline Dirk DeyneFriend
Messages: 8
Registered: May 2013
Junior Member
Ok Thomas I see...
Need to work on my xtend-skills
Re: annotate a parameter in method [message #1053151 is a reply to message #1053144] Thu, 02 May 2013 18:08 Go to previous messageGo to next message
Dirk Deyne is currently offline Dirk DeyneFriend
Messages: 8
Registered: May 2013
Junior Member
next problem ...
foo.toAnnotation("foo.annotation.MyBar","foo") becomes @MyBar("foo")
needs to be
@MyBar(value="foo")
Re: annotate a parameter in method [message #1053179 is a reply to message #1053151] Thu, 02 May 2013 21:30 Go to previous messageGo to next message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 6552
Registered: July 2009
Senior Member
Hi,

isnt that the same?
Re: annotate a parameter in method [message #1053180 is a reply to message #1053179] Thu, 02 May 2013 21:41 Go to previous messageGo to next message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 6552
Registered: July 2009
Senior Member
Btw you may digg into

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
Re: annotate a parameter in method [message #1053245 is a reply to message #1053180] Fri, 03 May 2013 11:18 Go to previous messageGo to next message
Dirk Deyne is currently offline Dirk DeyneFriend
Messages: 8
Registered: May 2013
Junior Member
found out that adding key-value pairs to an Annotation is not that obvious.
but here is an intresting post that helps a lot: http://www.eclipse.org/forums/index.php/t/300722/
Re: annotate a parameter in method [message #1053250 is a reply to message #1053179] Fri, 03 May 2013 11:42 Go to previous messageGo to next message
Dirk Deyne is currently offline Dirk DeyneFriend
Messages: 8
Registered: May 2013
Junior Member
Christian Dietrich wrote on Thu, 02 May 2013 17:30
Hi,

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 11:43]

Report message to a moderator

Re: annotate a parameter in method [message #1053257 is a reply to message #1053250] Fri, 03 May 2013 12:10 Go to previous messageGo to next message
Thomas Fritsch is currently offline Thomas FritschFriend
Messages: 20
Registered: April 2013
Location: Germany
Junior Member
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 #1053258 is a reply to message #1053257] Fri, 03 May 2013 12:12 Go to previous messageGo to next message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 6552
Registered: July 2009
Senior Member
yes this is what i meant with digging
icon14.gif  Re: annotate a parameter in method [message #1053288 is a reply to message #1053257] Fri, 03 May 2013 15:06 Go to previous messageGo to next message
Dirk Deyne is currently offline Dirk DeyneFriend
Messages: 8
Registered: May 2013
Junior Member
Thomas Fritsch wrote on Fri, 03 May 2013 08:10
Actually, a few weeks ago I digged through the api for solving similar problems, and finally came up with my own utilities.


does the job perfectly !

thx Thomas.
Re: annotate a parameter in method [message #1421357 is a reply to message #1053288] Thu, 11 September 2014 08:58 Go to previous messageGo to next message
Mohamed El-Beltagy is currently offline Mohamed El-BeltagyFriend
Messages: 5
Registered: July 2014
Junior Member
To add an annotation attribute that has a list of String values, the following method will do:
def addListOfStringValues(JvmAnnotationReference annoRef, String name, List<String> newValues) {
	val op = annoRef.findOperationByName(name)
	annoRef.values += createJvmStringAnnotationValue => [
		operation = op
		for(value: newValues) {
			values += value
		}
	]
	annoRef
}
Re: annotate a parameter in method [message #1421429 is a reply to message #1421357] Thu, 11 September 2014 10:58 Go to previous message
Thomas Fritsch is currently offline Thomas FritschFriend
Messages: 20
Registered: April 2013
Location: Germany
Junior Member
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.
Previous Topic:Implicit type conversion for Xbase
Next Topic:[solved] Accessing types and their properties from another file (with same grammar)
Goto Forum:
  


Current Time: Mon Dec 22 10:08:02 GMT 2014

Powered by FUDForum. Page generated in 0.02319 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software