Skip to main content


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 13:16 Go to next message
Oren Mishali is currently offline Oren MishaliFriend
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 15:00 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 760
Registered: July 2009
Location: Hamburg
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


---
Get professional support from the Xtext committers at www.typefox.io
Re: How to infer an annotation with a key-value pair [message #1080806 is a reply to message #1064302] Tue, 06 August 2013 11:48 Go to previous messageGo to next message
Thomas Goossens is currently offline Thomas GoossensFriend
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 11:49 Go to previous messageGo to next message
Thomas Goossens is currently offline Thomas GoossensFriend
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 18:48 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
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;
	}


}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to infer an annotation with a key-value pair [message #1827858 is a reply to message #1081053] Mon, 25 May 2020 18:10 Go to previous messageGo to next message
Thomas Kohler is currently offline Thomas KohlerFriend
Messages: 15
Registered: November 2016
Junior Member
Hello,

I have similar problems generating Annotations out of the Inferrrer.

I found Christians solution, but I get back an instance of the class
JvmAnnotationReferenceImplCustom 
as result in
JvmAnnotationReference result = typesFactory.createJvmAnnotationReference();
which returns an unmodifiable List
@Override
	public EList<JvmAnnotationValue> getValues() {
		EList<JvmAnnotationValue> explicitValues = getExplicitValues();
		List<JvmOperation> operations = Lists.newArrayList(getAnnotation().getDeclaredOperations());
		if (operations.size() <= explicitValues.size()) {
			return ECollections.unmodifiableEList(explicitValues);
		}
		Set<JvmOperation> seenOperations = Sets.newHashSetWithExpectedSize(operations.size());
		BasicEList<JvmAnnotationValue> result = new BasicEList<JvmAnnotationValue>(operations.size());
		for(JvmAnnotationValue value: explicitValues) {
			seenOperations.add(value.getOperation());
			result.add(value);
		}
		for(JvmOperation operation: operations) {
			if (seenOperations.add(operation)) {
				JvmAnnotationValue defaultValue = operation.getDefaultValue();
				if (defaultValue != null) {
					result.add(defaultValue);
				}
			}
		}
		return ECollections.unmodifiableEList(result);
	}

The only possible values to set are those collected for all operations without default value, but I also need to set those with a default value to an other value. How can I set all possible Enumeration operations?

Additionally I want to set a String value using a String constant instead of the String-Literal itself. Other Annotation values are Enumeration literals; here a small example what I mean:
public enum MyColors {
   red,
   green,
   blue;
}

public interface MyFeatures {
   public static final String MY_COLOR_FEATURE = "myColorFeature";
}

public class Something {
   @MyAnnotation(feature=Test.MY_COLOR_FEATURE, color=MyColors.green)
   public void someMethod() {}
}

I have an already inferred JvmField for the String constant and a JvmTypeReference to the already existing Enumeration. Can you show me how to infer an Annotation as shown in the Example?

Thanks,
Tom
Re: How to infer an annotation with a key-value pair [message #1827868 is a reply to message #1827858] Tue, 26 May 2020 05:35 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
I am not sure if this is possible https://www.eclipse.org/forums/index.php/t/1085224/

https://bugs.eclipse.org/bugs/show_bug.cgi?id=460798

Maybe you can find a sample somewhere
Or find a solution by reading the code


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Tue, 26 May 2020 05:35]

Report message to a moderator

Re: How to infer an annotation with a key-value pair [message #1827889 is a reply to message #1827868] Tue, 26 May 2020 12:14 Go to previous messageGo to next message
Thomas Kohler is currently offline Thomas KohlerFriend
Messages: 15
Registered: November 2016
Junior Member
Hello Christian,

Thank you for your answer, I checked the Links, they refer to similar Problems but I did not found a solution for me.

Do you know what was the reason the (new) getValues() Method return only those Enumeration values with no default?

I looks like the whole concept has changed since you posted your solution...

Thank you,
Tom
Re: How to infer an annotation with a key-value pair [message #1827913 is a reply to message #1827889] Tue, 26 May 2020 19:03 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
i dont get this question about the default. please elaborate

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to infer an annotation with a key-value pair [message #1827960 is a reply to message #1827913] Wed, 27 May 2020 16:45 Go to previous messageGo to next message
Thomas Kohler is currently offline Thomas KohlerFriend
Messages: 15
Registered: November 2016
Junior Member
Hello Christian,

You can see what I mean with "default" in the following snippet out of the Xtext original method
JvmAnnotationReferenceImplCustom>>getValues()

		(...)
		for(JvmOperation operation: operations) {
			if (seenOperations.add(operation)) {
				JvmAnnotationValue defaultValue = operation.getDefaultValue();
				if (defaultValue != null) {
					result.add(defaultValue);
				}
			}
		}
		return ECollections.unmodifiableEList(result);


Only the operations with no defaults defined in the Annotation will be in the (unmodifiable!) result list, so it is neither possible to set an alternate value to a JvmAnnotationValue witch has an default defined because it is not in the list, nor you can add a JvmAnnotationValue because of the "Unmodifiable".

Is there an other way to set annotation values (especially those who have an default)?

Regards, Tom
Re: How to infer an annotation with a key-value pair [message #1827964 is a reply to message #1827960] Wed, 27 May 2020 18:53 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
are you talking about "special type values" or simply ones like string, int.
or some that call to stuff like yours above https://www.eclipse.org/forums/index.php?t=msg&th=1067387&goto=1698455&#msg_1698455




Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Parsing XML
Next Topic:[solved] Indentation of elements in EList using formatting2 API
Goto Forum:
  


Current Time: Tue Apr 16 17:25:41 GMT 2024

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

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

Back to the top