|
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 |
Jan Koehnlein 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 #1081053 is a reply to message #1080807] |
Tue, 06 August 2013 18:48 |
|
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
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03388 seconds