Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Repeatable active annotations(How to use repeatabel annotations in a field processor)
Repeatable active annotations [message #1806092] Thu, 02 May 2019 09:46 Go to next message
Eclipse UserFriend
I created an active annotation like this:

@Target(ElementType.FIELD)
@Repeatable(MyAnnos)
@Active(MyAnnoProcessor)
annotation MyAnno {
	int group = 0
}

@Target(ElementType.FIELD)
annotation MyAnnos{
	MyAnno [] value
}


So I can write this in code:

@MyAnno(group=0)
@MyAnno(group=1)
String myField


In my MyAnnoProcessor I've overridden the method doTransform(MutableFieldDeclaration field, extension TransformationContext context) of the class AbstractFieldProcessor.

For each field I extract all annotations of type MyAnno and create for each annotation a new method.

When I look in the generated code then the methods are duplicated i.e. it looks like for 1 field and 2 annotations the doTransform-Method is called twice.

How can I use repeatable annotations in Xtend's active annotations correctly?



Re: Repeatable active annotations [message #1806094 is a reply to message #1806092] Thu, 02 May 2019 10:09 Go to previous messageGo to next message
Eclipse UserFriend
I'm quite sure that this is an implementation gap. I'm not aware of a scenario where AA's were used repeatably. Please file an issue to https://github.com/eclipse/xtext/issues

Could you provide a minimal example or better unit test? And even best, a patch? I think we won't have this on our short list. So if you are interested in that feature, please provide it. We would help you through the process & review.
Re: Repeatable active annotations [message #1806095 is a reply to message #1806094] Thu, 02 May 2019 10:13 Go to previous messageGo to next message
Eclipse UserFriend
Where do I start to implement it myself? Is there a readme on how to setup the sources and the build of Xtend?

First I'll try to prepare a minimal example project.
Re: Repeatable active annotations [message #1806096 is a reply to message #1806095] Thu, 02 May 2019 10:15 Go to previous messageGo to next message
Eclipse UserFriend
We have an Oomph setup that does all the setup automatically. Just use the Eclipse Installer, switch to Advanced mode and select all "Xtext" projects.

Read:
https://github.com/eclipse/xtext/blob/master/CONTRIBUTING.md
Re: Repeatable active annotations [message #1806097 is a reply to message #1806096] Thu, 02 May 2019 10:25 Go to previous messageGo to next message
Eclipse UserFriend
@udo maybe you can customize

public void doTransform(List<? extends MutableFieldDeclaration> annotatedfields, @Extension TransformationContext context) {
instead
and filter the duplicates youself

(dont know if this will be called n-times too
Re: Repeatable active annotations [message #1806098 is a reply to message #1806096] Thu, 02 May 2019 10:45 Go to previous messageGo to next message
Eclipse UserFriend
Minimal example based on projects xtend-annotations-examples and xtend-annotations-examples-client created with project wizard "Xtend > Examples > Xtend Active Annotation Examples":

1. Add a new package called "repeatable" to both projects.
2. Add this Xtend class to first project in package repeatable:

package repeatable

import java.lang.annotation.Target
import java.lang.annotation.ElementType
import java.lang.annotation.Repeatable
import org.eclipse.xtend.lib.macro.Active
import org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration
import org.eclipse.xtend.lib.macro.AbstractFieldProcessor
import org.eclipse.xtend.lib.macro.TransformationContext
import org.eclipse.xtend.lib.macro.declaration.AnnotationTarget
import org.eclipse.xtend.lib.macro.declaration.AnnotationReference
import org.eclipse.xtend.lib.annotations.Accessors

@Target(ElementType.FIELD)
@Repeatable(MyAnnos)
@Active(MyAnnoProcessor)
annotation MyAnno {
	int group = 0
}

@Target(ElementType.FIELD)
annotation MyAnnos {
	MyAnno[] value
}

class MyAnnoProcessor extends AbstractFieldProcessor {

	extension AnnotationExtensions = new AnnotationExtensions

	override doTransform(MutableFieldDeclaration field, extension TransformationContext context) {

		var annos = field.getAnnotations(context)
		for (anno : annos) {
			doTransformSingleAnnotation(anno, field, context)
		}
	}

	def doTransformSingleAnnotation(AnnotationReference annoRef, MutableFieldDeclaration field,
		extension TransformationContext context) {
		val group = annoRef.getValue("group") as Integer

		field.declaringType.addMethod("method" + group) [
			returnType = context.primitiveVoid
			body = ''''''
		]
	}

	private def getAnnotations(MutableFieldDeclaration field, extension TransformationContext context) {
		findAnnotations(
			new AnnotationSearch => [
				it.type = field
				it.context = context
				it.annotationClass = MyAnno
			],
			context
		)
	}
}

class AnnotationExtensions {

	def findAnnotations(extension AnnotationSearch annotationSearch, extension TransformationContext context) {
		type.annotations.filter [
			annotationTypeDeclaration.qualifiedName == annotationClass.newTypeReference.type.qualifiedName
		]
	}
}

class AnnotationSearch {
	@Accessors AnnotationTarget type
	@Accessors TransformationContext context
	@Accessors Class<?> annotationClass
}


3. Add this code to package repeatable in second project (client):

package repeatable

class RepeatableExample {
	
	
	@MyAnno(group=0)
	@MyAnno(group=1)
	@MyAnno(group=2)
	String field
}



To see the active annotation in client-project you have to extend the MANIFEST.MF of the first project with the package repeatable as exported.

And following code is generated:

package repeatable;

import repeatable.MyAnno;

@SuppressWarnings("all")
public class RepeatableExample {
  @MyAnno(group = 0)
  @MyAnno(group = 1)
  @MyAnno(group = 2)
  private String field;
  
  public void method0() {
  }
  public void method1() {
  }
  public void method2() {
  }
  public void method0() {
  }
  public void method1() {
  }
  public void method2() {
  }
  public void method0() {
  }
  public void method1() {
  }
  public void method2() {
  }
}


So the field generation is called too often.
Re: Repeatable active annotations [message #1806099 is a reply to message #1806098] Thu, 02 May 2019 10:56 Go to previous messageGo to next message
Eclipse UserFriend
Added bug https://github.com/eclipse/xtext/issues/1441
Re: Repeatable active annotations [message #1806123 is a reply to message #1806097] Fri, 03 May 2019 02:36 Go to previous message
Eclipse UserFriend
I did it as you suggested by overriding the method which gets the list of fields and removing all duplicated fields.

That works.
Previous Topic:indir in grammer
Next Topic:Decision can match input such as "RULE_ID" using multiple alternatives
Goto Forum:
  


Current Time: Sat Jul 05 04:15:49 EDT 2025

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

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

Back to the top