Search in sources :

Example 6 with XExpression

use of org.eclipse.xtext.xbase.XExpression in project xtext-xtend by eclipse.

the class ConstantExpressionsInterpreter method evaluate.

public Object evaluate(final XExpression expression, final JvmTypeReference expectedType) {
    final ClassLoader classLoader = this.classLoaderProvider.getClassLoader(expression);
    final Map<String, JvmIdentifiableElement> visibleFeatures = this.findVisibleFeatures(expression);
    JvmTypeReference _xifexpression = null;
    if ((expectedType instanceof XComputedTypeReference)) {
        _xifexpression = null;
    } else {
        _xifexpression = expectedType;
    }
    ClassFinder _classFinder = new ClassFinder(classLoader);
    LinkedHashSet<XExpression> _newLinkedHashSet = CollectionLiterals.<XExpression>newLinkedHashSet();
    Context _context = new Context(_xifexpression, _classFinder, visibleFeatures, _newLinkedHashSet);
    final Object result = this.evaluate(expression, _context);
    return result;
}
Also used : Context(org.eclipse.xtext.xbase.interpreter.Context) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) XComputedTypeReference(org.eclipse.xtext.xtype.XComputedTypeReference) ClassFinder(org.eclipse.xtext.common.types.access.impl.ClassFinder) XExpression(org.eclipse.xtext.xbase.XExpression) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject)

Example 7 with XExpression

use of org.eclipse.xtext.xbase.XExpression in project xtext-xtend by eclipse.

the class CompilationUnitImpl method translateAnnotationValue.

public Object translateAnnotationValue(final JvmAnnotationValue value, final boolean isArray) {
    Pair<List<?>, Class<?>> _switchResult = null;
    boolean _matched = false;
    if (value instanceof JvmCustomAnnotationValue) {
        if ((((JvmCustomAnnotationValue) value).getValues().isEmpty() && isArray)) {
            _matched = true;
            List<Object> _emptyList = CollectionLiterals.<Object>emptyList();
            Class<?> _arrayComponentType = this.toArrayComponentType(this.findExpectedType(value));
            _switchResult = Pair.<List<?>, Class<?>>of(_emptyList, _arrayComponentType);
        }
    }
    if (!_matched) {
        if (value instanceof JvmCustomAnnotationValue) {
            _matched = true;
            final JvmTypeReference expectedType = this.findExpectedType(value);
            final Function1<XExpression, Object> _function = (XExpression it) -> {
                return this.evaluate(it, expectedType);
            };
            final Object result = IterableExtensions.<Object>head(IterableExtensions.<XExpression, Object>map(Iterables.<XExpression>filter(((JvmCustomAnnotationValue) value).getValues(), XExpression.class), _function));
            return this.translateAnnotationValue(result, expectedType, isArray);
        }
    }
    if (!_matched) {
        if (value instanceof JvmTypeAnnotationValue) {
            _matched = true;
            final Function1<JvmTypeReference, TypeReference> _function = (JvmTypeReference it) -> {
                return this.toTypeReference(it);
            };
            List<TypeReference> _map = ListExtensions.<JvmTypeReference, TypeReference>map(((JvmTypeAnnotationValue) value).getValues(), _function);
            _switchResult = Pair.<List<?>, Class<?>>of(_map, TypeReference.class);
        }
    }
    if (!_matched) {
        if (value instanceof JvmAnnotationAnnotationValue) {
            _matched = true;
            final Function1<JvmAnnotationReference, AnnotationReference> _function = (JvmAnnotationReference it) -> {
                return this.toAnnotationReference(it);
            };
            List<AnnotationReference> _map = ListExtensions.<JvmAnnotationReference, AnnotationReference>map(((JvmAnnotationAnnotationValue) value).getValues(), _function);
            _switchResult = Pair.<List<?>, Class<?>>of(_map, AnnotationReference.class);
        }
    }
    if (!_matched) {
        if (value instanceof JvmStringAnnotationValue) {
            _matched = true;
            EList<String> _values = ((JvmStringAnnotationValue) value).getValues();
            _switchResult = Pair.<List<?>, Class<?>>of(_values, String.class);
        }
    }
    if (!_matched) {
        if (value instanceof JvmBooleanAnnotationValue) {
            _matched = true;
            EList<Boolean> _values = ((JvmBooleanAnnotationValue) value).getValues();
            _switchResult = Pair.<List<?>, Class<?>>of(_values, boolean.class);
        }
    }
    if (!_matched) {
        if (value instanceof JvmIntAnnotationValue) {
            _matched = true;
            EList<Integer> _values = ((JvmIntAnnotationValue) value).getValues();
            _switchResult = Pair.<List<?>, Class<?>>of(_values, int.class);
        }
    }
    if (!_matched) {
        if (value instanceof JvmByteAnnotationValue) {
            _matched = true;
            EList<Byte> _values = ((JvmByteAnnotationValue) value).getValues();
            _switchResult = Pair.<List<?>, Class<?>>of(_values, byte.class);
        }
    }
    if (!_matched) {
        if (value instanceof JvmCharAnnotationValue) {
            _matched = true;
            EList<Character> _values = ((JvmCharAnnotationValue) value).getValues();
            _switchResult = Pair.<List<?>, Class<?>>of(_values, char.class);
        }
    }
    if (!_matched) {
        if (value instanceof JvmDoubleAnnotationValue) {
            _matched = true;
            EList<Double> _values = ((JvmDoubleAnnotationValue) value).getValues();
            _switchResult = Pair.<List<?>, Class<?>>of(_values, double.class);
        }
    }
    if (!_matched) {
        if (value instanceof JvmEnumAnnotationValue) {
            _matched = true;
            final Function1<JvmEnumerationLiteral, NamedElement> _function = (JvmEnumerationLiteral it) -> {
                return this.toNamedElement(it);
            };
            List<NamedElement> _map = ListExtensions.<JvmEnumerationLiteral, NamedElement>map(((JvmEnumAnnotationValue) value).getValues(), _function);
            _switchResult = Pair.<List<?>, Class<?>>of(_map, EnumerationValueDeclaration.class);
        }
    }
    if (!_matched) {
        if (value instanceof JvmFloatAnnotationValue) {
            _matched = true;
            EList<Float> _values = ((JvmFloatAnnotationValue) value).getValues();
            _switchResult = Pair.<List<?>, Class<?>>of(_values, float.class);
        }
    }
    if (!_matched) {
        if (value instanceof JvmLongAnnotationValue) {
            _matched = true;
            EList<Long> _values = ((JvmLongAnnotationValue) value).getValues();
            _switchResult = Pair.<List<?>, Class<?>>of(_values, long.class);
        }
    }
    if (!_matched) {
        if (value instanceof JvmShortAnnotationValue) {
            _matched = true;
            EList<Short> _values = ((JvmShortAnnotationValue) value).getValues();
            _switchResult = Pair.<List<?>, Class<?>>of(_values, short.class);
        }
    }
    if (!_matched) {
        List<Object> _emptyList = CollectionLiterals.<Object>emptyList();
        _switchResult = Pair.<List<?>, Class<?>>of(_emptyList, Object.class);
    }
    final Pair<List<?>, Class<?>> result = _switchResult;
    if (isArray) {
        return this.toArrayOfType(result.getKey(), result.getValue());
    } else {
        return IterableExtensions.head(result.getKey());
    }
}
Also used : JvmEnumAnnotationValue(org.eclipse.xtext.common.types.JvmEnumAnnotationValue) JvmByteAnnotationValue(org.eclipse.xtext.common.types.JvmByteAnnotationValue) JvmCustomAnnotationValue(org.eclipse.xtext.common.types.JvmCustomAnnotationValue) JvmLongAnnotationValue(org.eclipse.xtext.common.types.JvmLongAnnotationValue) ArrayList(java.util.ArrayList) List(java.util.List) EList(org.eclipse.emf.common.util.EList) JvmStringAnnotationValue(org.eclipse.xtext.common.types.JvmStringAnnotationValue) JvmFloatAnnotationValue(org.eclipse.xtext.common.types.JvmFloatAnnotationValue) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) EObject(org.eclipse.emf.ecore.EObject) NamedElement(org.eclipse.xtend.lib.macro.declaration.NamedElement) EnumerationValueDeclaration(org.eclipse.xtend.lib.macro.declaration.EnumerationValueDeclaration) JvmTypeAnnotationValue(org.eclipse.xtext.common.types.JvmTypeAnnotationValue) JvmBooleanAnnotationValue(org.eclipse.xtext.common.types.JvmBooleanAnnotationValue) JvmIntAnnotationValue(org.eclipse.xtext.common.types.JvmIntAnnotationValue) JvmCharAnnotationValue(org.eclipse.xtext.common.types.JvmCharAnnotationValue) JvmAnnotationReference(org.eclipse.xtext.common.types.JvmAnnotationReference) AnnotationReference(org.eclipse.xtend.lib.macro.declaration.AnnotationReference) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) JvmShortAnnotationValue(org.eclipse.xtext.common.types.JvmShortAnnotationValue) JvmDoubleAnnotationValue(org.eclipse.xtext.common.types.JvmDoubleAnnotationValue) JvmAnnotationReference(org.eclipse.xtext.common.types.JvmAnnotationReference) JvmEnumerationLiteral(org.eclipse.xtext.common.types.JvmEnumerationLiteral) XExpression(org.eclipse.xtext.xbase.XExpression) JvmAnnotationAnnotationValue(org.eclipse.xtext.common.types.JvmAnnotationAnnotationValue)

Example 8 with XExpression

use of org.eclipse.xtext.xbase.XExpression in project xtext-xtend by eclipse.

the class XtendJvmModelInferrer method initialize.

protected void initialize(XtendAnnotationType source, JvmAnnotationType inferredJvmType) {
    inferredJvmType.setVisibility(source.getVisibility());
    inferredJvmType.setStatic(source.isStatic() && !isTopLevel(source));
    inferredJvmType.setAbstract(true);
    translateAnnotationsTo(source.getAnnotations(), inferredJvmType);
    jvmTypesBuilder.copyDocumentationTo(source, inferredJvmType);
    for (XtendMember member : source.getMembers()) {
        if (member instanceof XtendField) {
            XtendField field = (XtendField) member;
            if (!Strings.isEmpty(field.getName())) {
                JvmOperation operation = typesFactory.createJvmOperation();
                associator.associatePrimary(member, operation);
                operation.setSimpleName(field.getName());
                JvmTypeReference returnType = null;
                XExpression initialValue = field.getInitialValue();
                if (field.getType() != null) {
                    returnType = jvmTypesBuilder.cloneWithProxies(field.getType());
                } else if (initialValue != null) {
                    returnType = jvmTypesBuilder.inferredType(initialValue);
                }
                operation.setReturnType(returnType);
                if (initialValue != null) {
                    JvmAnnotationValue jvmAnnotationValue = jvmTypesBuilder.toJvmAnnotationValue(initialValue);
                    if (jvmAnnotationValue != null) {
                        operation.setDefaultValue(jvmAnnotationValue);
                        jvmAnnotationValue.setOperation(operation);
                    }
                    jvmTypesBuilder.setBody(operation, initialValue);
                }
                operation.setVisibility(JvmVisibility.PUBLIC);
                translateAnnotationsTo(member.getAnnotations(), operation);
                jvmTypesBuilder.copyDocumentationTo(member, operation);
                inferredJvmType.getMembers().add(operation);
            }
        }
    }
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) XtendMember(org.eclipse.xtend.core.xtend.XtendMember) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) XExpression(org.eclipse.xtext.xbase.XExpression) XtendField(org.eclipse.xtend.core.xtend.XtendField) JvmAnnotationValue(org.eclipse.xtext.common.types.JvmAnnotationValue)

Example 9 with XExpression

use of org.eclipse.xtext.xbase.XExpression in project xtext-xtend by eclipse.

the class InitialTemplateIndentationComputer method caseRichString.

@Override
public String caseRichString(RichString object) {
    String result = null;
    List<XExpression> elements = object.getExpressions();
    for (int i = 0; i < elements.size(); ) {
        XExpression element = elements.get(i);
        String elementResult = null;
        int nextIndex = i + 1;
        if (element instanceof RichStringLiteral) {
            RichStringLiteral literal = (RichStringLiteral) element;
            if (nextIndex == elements.size()) {
                // last one
                elementResult = getLeadingWhitespace(literal.getValue(), literal);
            } else if (!(elements.get(nextIndex) instanceof RichStringLiteral)) {
                elementResult = getLeadingWhitespace(literal.getValue(), literal);
            } else {
                StringBuilder run = new StringBuilder(Strings.emptyIfNull(literal.getValue()));
                RichStringLiteral next = null;
                do {
                    next = (RichStringLiteral) elements.get(nextIndex);
                    run.append(next.getValue());
                    nextIndex++;
                } while (nextIndex < elements.size() && elements.get(nextIndex) instanceof RichStringLiteral);
                elementResult = getLeadingWhitespace(run.toString(), next);
            }
        }
        if (elementResult == null && i == 0)
            return initial;
        result = getBetterString(result, elementResult);
        if (Strings.isEmpty(result))
            return result;
        i = nextIndex;
    }
    return result;
}
Also used : RichStringLiteral(org.eclipse.xtend.core.xtend.RichStringLiteral) XExpression(org.eclipse.xtext.xbase.XExpression) RichString(org.eclipse.xtend.core.xtend.RichString)

Example 10 with XExpression

use of org.eclipse.xtext.xbase.XExpression in project xtext-xtend by eclipse.

the class InitialTemplateIndentationComputer method getLeadingWhitespace.

private String getLeadingWhitespace(String value, RichStringLiteral lastLiteral) {
    List<TextLine> lines = TextLines.splitString(value);
    // no line breaks or immediately closed string literal => no initial indentation
    if (lines.size() <= 1) {
        return null;
    }
    TextLine firstLine = lines.get(0);
    // first line has content == no initial indentation
    if (!firstLine.containsOnlyWhitespace()) {
        return null;
    }
    String result = null;
    boolean emptyLineSeen = false;
    for (int i = 1; i < lines.size(); i++) {
        TextLine line = lines.get(i);
        CharSequence leadingWS = line.getLeadingWhiteSpace();
        // line is not empty
        if (leadingWS.length() != line.length()) {
            if (leadingWS.length() == 0)
                return "";
            result = getBetterString(result, leadingWS.toString());
        } else {
            // some tools tend to right trim text files by default (e.g. git)
            // that's why we ignore empty lines
            RichString completeString = (RichString) lastLiteral.eContainer();
            List<XExpression> siblings = completeString.getExpressions();
            if (siblings.get(siblings.size() - 1) != lastLiteral) {
                if (leadingWS.length() == 0) {
                    // empty line
                    emptyLineSeen = true;
                } else {
                    result = getBetterString(result, leadingWS.toString());
                }
            }
        }
    }
    if (emptyLineSeen && result == null)
        return "";
    return result;
}
Also used : RichString(org.eclipse.xtend.core.xtend.RichString) XExpression(org.eclipse.xtext.xbase.XExpression) RichString(org.eclipse.xtend.core.xtend.RichString)

Aggregations

XExpression (org.eclipse.xtext.xbase.XExpression)215 Test (org.junit.Test)133 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)96 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)84 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)82 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)82 XBlockExpression (org.eclipse.xtext.xbase.XBlockExpression)80 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)63 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)36 EObject (org.eclipse.emf.ecore.EObject)28 IFeatureCallArguments (org.eclipse.xtext.xbase.typesystem.arguments.IFeatureCallArguments)28 XtendField (org.eclipse.xtend.core.xtend.XtendField)23 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)23 XNumberLiteral (org.eclipse.xtext.xbase.XNumberLiteral)21 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)20 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)18 XAbstractFeatureCall (org.eclipse.xtext.xbase.XAbstractFeatureCall)17 XStringLiteral (org.eclipse.xtext.xbase.XStringLiteral)17 XClosure (org.eclipse.xtext.xbase.XClosure)16 IFeatureCallArgumentSlot (org.eclipse.xtext.xbase.typesystem.arguments.IFeatureCallArgumentSlot)16