Java Code Examples for org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotation#getElementValuePairs()

The following examples show how to use org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotation#getElementValuePairs() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void _toJavaExpression(final XAnnotation annotation, final ITreeAppendable b) {
	b.append("@");
	b.append(annotation.getAnnotationType());
	XExpression value = annotation.getValue();
	if (value != null) {
		b.append("(");
		internalToJavaExpression(value, b);
		b.append(")");
	} else {
		EList<XAnnotationElementValuePair> valuePairs = annotation.getElementValuePairs();
		if (valuePairs.isEmpty())
			return;
		b.append("(");
		for (int i = 0; i < valuePairs.size(); i++) {
			XAnnotationElementValuePair pair = valuePairs.get(i);
			b.append(pair.getElement().getSimpleName());
			b.append(" = ");
			internalToJavaExpression(pair.getValue(), b);
			if (i < valuePairs.size()-1) {
				b.append(", ");
			}
		}
		b.append(")");
	}
}
 
Example 2
Source File: JvmTypesBuilderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testStringAnnotationWithNullExpression() {
  try {
    final XAnnotationsFactory f = XAnnotationsFactory.eINSTANCE;
    final XExpression context = this.expression("\'Foo\'");
    final XAnnotation anno = f.createXAnnotation();
    JvmType _findDeclaredType = this.references.findDeclaredType(Inject.class, context);
    anno.setAnnotationType(((JvmAnnotationType) _findDeclaredType));
    final XAnnotationElementValuePair pair = f.createXAnnotationElementValuePair();
    EList<XAnnotationElementValuePair> _elementValuePairs = anno.getElementValuePairs();
    this._jvmTypesBuilder.<XAnnotationElementValuePair>operator_add(_elementValuePairs, pair);
    final JvmGenericType type = this.typesFactory.createJvmGenericType();
    this._jvmTypesBuilder.addAnnotation(type, anno);
    Assert.assertEquals(anno.getAnnotationType(), IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getAnnotation());
    Assert.assertTrue(IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getExplicitValues().isEmpty());
    Assert.assertFalse(IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getValues().isEmpty());
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example 3
Source File: JvmTypesBuilderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testIntegerAnnotation() {
  try {
    final XAnnotationsFactory f = XAnnotationsFactory.eINSTANCE;
    final XExpression e = this.expression("\'Foo\'");
    final XAnnotation anno = f.createXAnnotation();
    JvmType _findDeclaredType = this.references.findDeclaredType(TestAnnotation3.class, e);
    final JvmAnnotationType annotatiomType = ((JvmAnnotationType) _findDeclaredType);
    anno.setAnnotationType(annotatiomType);
    final XAnnotationElementValuePair pair = f.createXAnnotationElementValuePair();
    pair.setElement(IterableExtensions.<JvmOperation>head(annotatiomType.getDeclaredOperations()));
    pair.setValue(this.expression("10"));
    EList<XAnnotationElementValuePair> _elementValuePairs = anno.getElementValuePairs();
    this._jvmTypesBuilder.<XAnnotationElementValuePair>operator_add(_elementValuePairs, pair);
    final JvmGenericType type = this.typesFactory.createJvmGenericType();
    this._jvmTypesBuilder.addAnnotation(type, anno);
    Assert.assertEquals(anno.getAnnotationType(), IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getAnnotation());
    Assert.assertEquals(1, IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getValues().size());
    JvmAnnotationValue _head = IterableExtensions.<JvmAnnotationValue>head(IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getValues());
    final JvmCustomAnnotationValue value = ((JvmCustomAnnotationValue) _head);
    EObject _head_1 = IterableExtensions.<EObject>head(value.getValues());
    Assert.assertTrue((_head_1 instanceof XNumberLiteral));
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example 4
Source File: Utils.java    From sarl with Apache License 2.0 6 votes vote down vote up
private static void addAnnotationToSignature(StringBuilder textRepresentation, SARLGrammarKeywordAccess elements,
		ISerializer serializer, ImportManager importManager, XAnnotation annotation) {
	textRepresentation.append(elements.getCommercialAtKeyword());
	textRepresentation.append(getSignatureType(annotation.getAnnotationType(), importManager));
	final XExpression value = annotation.getValue();
	if (value != null) {
		textRepresentation.append(elements.getLeftParenthesisKeyword());
		textRepresentation.append(serializer.serialize(value).trim());
		textRepresentation.append(elements.getRightParenthesisKeyword());
	} else if (!annotation.getElementValuePairs().isEmpty()) {
		textRepresentation.append(elements.getLeftParenthesisKeyword());
		boolean addComa = false;
		for (final XAnnotationElementValuePair pair : annotation.getElementValuePairs()) {
			if (addComa) {
				textRepresentation.append(elements.getCommaKeyword());
			} else {
				addComa = true;
			}
			textRepresentation.append(elements.getEqualsSignKeyword());
			textRepresentation.append(serializer.serialize(pair.getValue()).trim());
		}
		textRepresentation.append(elements.getRightParenthesisKeyword());
	}
}
 
Example 5
Source File: JvmTypesBuilder.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Translates a single {@link XAnnotation} to {@link JvmAnnotationReference} that can be added to a {@link JvmAnnotationTarget}.
 * 
 * @param anno the source annotation
 * 
 * @return a {@link JvmAnnotationReference} that can be attached to some {@link JvmAnnotationTarget}
 */
/* @Nullable */ 
public JvmAnnotationReference getJvmAnnotationReference(/* @Nullable */ XAnnotation anno) {
	if(anno == null)
		return null;
	JvmAnnotationReference reference = typesFactory.createJvmAnnotationReference();
	final JvmType annotation = (JvmType) anno.eGet(
			XAnnotationsPackage.Literals.XANNOTATION__ANNOTATION_TYPE, false);
	if (annotation.eIsProxy()) {
		JvmAnnotationType copiedProxy = TypesFactory.eINSTANCE.createJvmAnnotationType();
		((InternalEObject)copiedProxy).eSetProxyURI(EcoreUtil.getURI(annotation));
		reference.setAnnotation(copiedProxy);
	} else if (annotation instanceof JvmAnnotationType){
		reference.setAnnotation((JvmAnnotationType) annotation);
	}
	for (XAnnotationElementValuePair val : anno.getElementValuePairs()) {
		XExpression valueExpression = val.getValue();
		JvmAnnotationValue annotationValue = toJvmAnnotationValue(valueExpression);
		if (annotationValue != null) {
			JvmOperation op = (JvmOperation) val.eGet(
					XAnnotationsPackage.Literals.XANNOTATION_ELEMENT_VALUE_PAIR__ELEMENT, false);
			annotationValue.setOperation(op);
			reference.getExplicitValues().add(annotationValue);
		}
	}
	if (anno.getValue() != null) {
		JvmAnnotationValue value = toJvmAnnotationValue(anno.getValue());
		if (value != null) {
			reference.getExplicitValues().add(value);
		}
	}
	associate(anno, reference);
	return reference;
}
 
Example 6
Source File: XbaseWithAnnotationsTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void computeChildTypesForUnknownAnnotation(XAnnotation object, ITypeComputationState state) {
	XExpression expression = object.getValue();
	if (expression != null)
		state.withNonVoidExpectation().computeTypes(expression);
	else {
		List<XAnnotationElementValuePair> valuePairs = object.getElementValuePairs();
		for(XAnnotationElementValuePair pair: valuePairs) {
			computeTypes(object, pair.getElement(), pair.getValue(), state);
		}
	}
}
 
Example 7
Source File: XAnnotationUtil.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public XExpression findValue(XAnnotation annotation, JvmOperation jvmOperation) {
	if (jvmOperation.getSimpleName().equals("value") && annotation.getValue() != null) {
		return annotation.getValue();
	}
	for (XAnnotationElementValuePair pair : annotation.getElementValuePairs()) {
		if (pair.getElement() == jvmOperation)
			return pair.getValue();
	}
	return null;
}