Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to infer an annotation with a key-value pair
How to infer an annotation with a key-value pair [message #1064262] Tue, 18 June 2013 09:16 Go to next message
Oren Mishali is currently offline Oren Mishali
Messages: 15
Registered: March 2013
Junior Member
Hi all,
I'm using the model inferrer to infer an annotation, e.g., as follows:

...
annotations += model.toAnnotation("org.my.MyAnnotation")
...


I also managed to infer an annotation with a String value:

...
annotations += model.toAnnotation("org.my.MyAnnotation", "MyValue")
...


My question is, how to infer an annotation that has a key-value pair, e.g., @MyAnnotation(id = "MyId").

Thanks,
Oren

Re: How to infer an annotation with a key-value pair [message #1064302 is a reply to message #1064262] Tue, 18 June 2013 11:00 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan Koehnlein
Messages: 655
Registered: July 2009
Senior Member
There is no helper method for that so far. You have to create the
appropriate Jvm<Type>AnnotationValue using the types factory, assign the
value and add it to the annotation as in
org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder.toAnnotation(EObject,
String, Object)

The name of the parameter is indeed a cross reference to a method of the
annotation type. So in addition, you have to find the operation with the
given name in the methods of the annotation type and set that as the
operation of the annotation value.

18.06.13 15:16, schrieb Oren Mishali:
> Hi all,
> I'm using the model inferrer to infer an annotation, e.g., as follows:
>
>
> ..
> annotations += model.toAnnotation("org.my.MyAnnotation")
> ..
>
>
> I also managed to infer an annotation with a String value:
>
>
> ..
> annotations += model.toAnnotation("org.my.MyAnnotation", "MyValue")
> ..
>
>
> My question is, how to infer an annotation that has a key-value pair,
> e.g., @MyAnnotation(id = "MyId").
>
> Thanks,
> Oren
>
>


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: How to infer an annotation with a key-value pair [message #1080806 is a reply to message #1064302] Tue, 06 August 2013 07:48 Go to previous messageGo to next message
Thomas Goossens is currently offline Thomas Goossens
Messages: 32
Registered: August 2013
Member
Have you been succesful with this? I'm having the exact same issue right now and I can't seem to figure it out.
Re: How to infer an annotation with a key-value pair [message #1080807 is a reply to message #1064302] Tue, 06 August 2013 07:49 Go to previous messageGo to next message
Thomas Goossens is currently offline Thomas Goossens
Messages: 32
Registered: August 2013
Member
Have you figured this out? I'm stuck on this myself
Re: How to infer an annotation with a key-value pair [message #1081053 is a reply to message #1080807] Tue, 06 August 2013 14:48 Go to previous message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6130
Registered: July 2009
Senior Member
Hi,

i dont get this point. the tipp was 100% straight forward

public class ExtendedJvmTypesBuilder extends JvmTypesBuilder {
	
	@Inject
	private TypeReferences references;
	
	@Inject
	private TypesFactory typesFactory;
	
	@Nullable
	public JvmAnnotationReference toAnnotationExtended(@Nullable EObject sourceElement, @Nullable String annotationTypeName,
			Pair<String, String> ...  values) {
		JvmAnnotationReference result = typesFactory.createJvmAnnotationReference();
		JvmType jvmType = references.findDeclaredType(annotationTypeName, sourceElement);
		if (jvmType == null) {
			throw new IllegalArgumentException("The type "+annotationTypeName +" is not on the classpath.");
		}
		if (!(jvmType instanceof JvmAnnotationType)) {
			throw new IllegalArgumentException("The given class " + annotationTypeName + " is not an annotation type.");
		}
		JvmAnnotationType annotationType = (JvmAnnotationType) jvmType;
		result.setAnnotation(annotationType);
		
		for (Pair<String, String> value : values) {
			JvmStringAnnotationValue annotationValue = typesFactory.createJvmStringAnnotationValue();
			annotationValue.getValues().add(value.getValue());
			JvmOperation operation = getJvmOperation(annotationType, value.getKey());
			if (operation != null) {				
				annotationValue.setOperation(operation);
			}
			result.getValues().add(annotationValue);
		}
	
		return result;
	}
	
	private JvmOperation getJvmOperation(JvmAnnotationType type, String name) {
		for (JvmOperation op : type.getDeclaredOperations()) {
			if (op.getSimpleName().equals(name)) {
				return op;
			}
		}
		return null;
	}


}
Previous Topic:Case Insensitivity for the 5 min Tutorial
Next Topic:How to set multiple properties of a java annotation
Goto Forum:
  


Current Time: Fri Jul 25 07:24:20 EDT 2014

Powered by FUDForum. Page generated in 0.02238 seconds