Search in sources :

Example 1 with XAnnotationElementValuePair

use of org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotationElementValuePair in project xtext-eclipse by eclipse.

the class XbaseWithAnnotationsProposalProvider method completeXAnnotationElementValuePair_Element.

@Override
public void completeXAnnotationElementValuePair_Element(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
    XAnnotation annotationReference = null;
    if (model instanceof XAnnotationElementValuePair) {
        annotationReference = (XAnnotation) model.eContainer();
    } else if (model instanceof XAnnotation) {
        annotationReference = (XAnnotation) model;
    }
    if (annotationReference != null) {
        JvmType annotationType = annotationReference.getAnnotationType();
        if (annotationType != null && !annotationType.eIsProxy() && annotationType instanceof JvmAnnotationType) {
            // do not propose features like #toString, #hashCode etc
            JvmAnnotationType casted = (JvmAnnotationType) annotationType;
            final Set<JvmOperation> operations = Sets.newHashSet(casted.getDeclaredOperations());
            Predicate<IEObjectDescription> predicate = Predicates.and(new Predicate<IEObjectDescription>() {

                @Override
                public boolean apply(IEObjectDescription in) {
                    return operations.contains(in.getEObjectOrProxy());
                }
            }, getFeatureDescriptionPredicate(context));
            lookupCrossReference(((CrossReference) assignment.getTerminal()), context, acceptor, predicate);
        }
    }
}
Also used : JvmAnnotationType(org.eclipse.xtext.common.types.JvmAnnotationType) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) XAnnotationElementValuePair(org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotationElementValuePair) XAnnotation(org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotation) CrossReference(org.eclipse.xtext.CrossReference) JvmType(org.eclipse.xtext.common.types.JvmType) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 2 with XAnnotationElementValuePair

use of org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotationElementValuePair in project xtext-eclipse by eclipse.

the class JvmAnnotationReferencePrinter method _internalToString.

protected String _internalToString(final XAnnotation reference) {
    final StringBuilder buffer = new StringBuilder();
    buffer.append("@");
    final URI uri = EcoreUtil.getURI(reference.getAnnotationType());
    buffer.append(this.createLinkWithLabel(XtextElementLinks.XTEXTDOC_SCHEME, uri, reference.getAnnotationType().getSimpleName()));
    XExpression _value = reference.getValue();
    boolean _tripleNotEquals = (_value != null);
    if (_tripleNotEquals) {
        buffer.append("(");
        buffer.append(reference.getValue());
        buffer.append(")");
    } else {
        boolean _isEmpty = reference.getElementValuePairs().isEmpty();
        boolean _not = (!_isEmpty);
        if (_not) {
            buffer.append("(");
            final Function1<XAnnotationElementValuePair, String> _function = (XAnnotationElementValuePair it) -> {
                final StringBuilder builder = new StringBuilder();
                JvmOperation _elvis = null;
                JvmOperation _element = it.getElement();
                if (_element != null) {
                    _elvis = _element;
                } else {
                    JvmType _annotationType = reference.getAnnotationType();
                    final Function1<JvmOperation, Boolean> _function_1 = (JvmOperation it_1) -> {
                        String _simpleName = it_1.getSimpleName();
                        return Boolean.valueOf(Objects.equal(_simpleName, "value"));
                    };
                    JvmOperation _findFirst = IterableExtensions.<JvmOperation>findFirst(((JvmAnnotationType) _annotationType).getDeclaredOperations(), _function_1);
                    _elvis = _findFirst;
                }
                final JvmOperation operation = _elvis;
                final URI operationUri = EcoreUtil.getURI(operation);
                builder.append(this.createLinkWithLabel(XtextElementLinks.XTEXTDOC_SCHEME, operationUri, operation.getSimpleName()));
                builder.append("=");
                builder.append(this.internalToString(it.getValue()));
                return builder.toString();
            };
            buffer.append(IterableExtensions.join(ListExtensions.<XAnnotationElementValuePair, String>map(reference.getElementValuePairs(), _function), ", "));
            buffer.append(")");
        }
    }
    return buffer.toString();
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) JvmAnnotationType(org.eclipse.xtext.common.types.JvmAnnotationType) XAnnotationElementValuePair(org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotationElementValuePair) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) XExpression(org.eclipse.xtext.xbase.XExpression) JvmType(org.eclipse.xtext.common.types.JvmType) URI(org.eclipse.emf.common.util.URI)

Example 3 with XAnnotationElementValuePair

use of org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotationElementValuePair in project xtext-xtend by eclipse.

the class CompilationUnitImpl method translateAnnotation.

protected AnnotationReference translateAnnotation(final XAnnotation annotation) {
    AnnotationReference _xblockexpression = null;
    {
        AnnotationReferenceBuildContextImpl _annotationReferenceBuildContextImpl = new AnnotationReferenceBuildContextImpl();
        final Procedure1<AnnotationReferenceBuildContextImpl> _function = (AnnotationReferenceBuildContextImpl it) -> {
            it.setCompilationUnit(this);
            JvmAnnotationReference _createJvmAnnotationReference = this.typesFactory.createJvmAnnotationReference();
            final Procedure1<JvmAnnotationReference> _function_1 = (JvmAnnotationReference reference) -> {
                JvmType _annotationType = annotation.getAnnotationType();
                reference.setAnnotation(((JvmAnnotationType) _annotationType));
            };
            JvmAnnotationReference _doubleArrow = ObjectExtensions.<JvmAnnotationReference>operator_doubleArrow(_createJvmAnnotationReference, _function_1);
            it.setDelegate(_doubleArrow);
        };
        final AnnotationReferenceBuildContextImpl buildContext = ObjectExtensions.<AnnotationReferenceBuildContextImpl>operator_doubleArrow(_annotationReferenceBuildContextImpl, _function);
        EList<XAnnotationElementValuePair> _elementValuePairs = annotation.getElementValuePairs();
        for (final XAnnotationElementValuePair valuePair : _elementValuePairs) {
            {
                final XExpression value = valuePair.getValue();
                if ((value != null)) {
                    final JvmOperation operation = valuePair.getElement();
                    final Object annotationValue = this.translateAnnotationValue(value, operation.getReturnType());
                    buildContext.set(operation.getSimpleName(), annotationValue);
                }
            }
        }
        XExpression _value = annotation.getValue();
        boolean _tripleNotEquals = (_value != null);
        if (_tripleNotEquals) {
            final Object annotationValue = this.translateAnnotationValue(annotation.getValue(), null);
            buildContext.set("value", annotationValue);
        }
        _xblockexpression = this.toAnnotationReference(buildContext.getDelegate());
    }
    return _xblockexpression;
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) XAnnotationElementValuePair(org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotationElementValuePair) EList(org.eclipse.emf.common.util.EList) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) AnnotationReference(org.eclipse.xtend.lib.macro.declaration.AnnotationReference) JvmAnnotationReference(org.eclipse.xtext.common.types.JvmAnnotationReference) XExpression(org.eclipse.xtext.xbase.XExpression) EObject(org.eclipse.emf.ecore.EObject) JvmType(org.eclipse.xtext.common.types.JvmType) JvmAnnotationReference(org.eclipse.xtext.common.types.JvmAnnotationReference)

Example 4 with XAnnotationElementValuePair

use of org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotationElementValuePair in project xtext-xtend by eclipse.

the class ConstantExpressionsInterpreterTest method testAnnotationValues.

@Test
public void testAnnotationValues() {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("@test.Annotation(annotation2Value=@test.Annotation2(\'foo\'))");
        _builder.newLine();
        _builder.append("class C { ");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        final XtendFile file = this.file(_builder.toString());
        final XAnnotationElementValuePair pair = IterableExtensions.<XAnnotationElementValuePair>head(IterableExtensions.<XAnnotation>head(IterableExtensions.<XtendTypeDeclaration>head(file.getXtendTypes()).getAnnotations()).getElementValuePairs());
        Object _evaluate = this.interpreter.evaluate(pair.getValue(), pair.getElement().getReturnType());
        final XAnnotation anno = ((XAnnotation) _evaluate);
        Assert.assertEquals("test.Annotation2", anno.getAnnotationType().getIdentifier());
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) XAnnotationElementValuePair(org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotationElementValuePair) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XAnnotation(org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotation) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) Test(org.junit.Test)

Aggregations

XAnnotationElementValuePair (org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotationElementValuePair)4 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)3 JvmType (org.eclipse.xtext.common.types.JvmType)3 JvmAnnotationType (org.eclipse.xtext.common.types.JvmAnnotationType)2 XExpression (org.eclipse.xtext.xbase.XExpression)2 XAnnotation (org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotation)2 EList (org.eclipse.emf.common.util.EList)1 URI (org.eclipse.emf.common.util.URI)1 EObject (org.eclipse.emf.ecore.EObject)1 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)1 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)1 AnnotationReference (org.eclipse.xtend.lib.macro.declaration.AnnotationReference)1 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)1 CrossReference (org.eclipse.xtext.CrossReference)1 JvmAnnotationReference (org.eclipse.xtext.common.types.JvmAnnotationReference)1 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)1 Function1 (org.eclipse.xtext.xbase.lib.Functions.Function1)1 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)1 Test (org.junit.Test)1