Search in sources :

Example 1 with ConstantExpressionEvaluationException

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

the class ConstantExpressionsInterpreter method _internalEvaluate.

protected Object _internalEvaluate(final XMemberFeatureCall it, final Context ctx) {
    Object _eGet = it.eGet(XbasePackage.Literals.XABSTRACT_FEATURE_CALL__FEATURE, this.isResolveProxies(it));
    final EObject feature = ((EObject) _eGet);
    boolean _eIsProxy = feature.eIsProxy();
    boolean _not = (!_eIsProxy);
    if (_not) {
        Object _switchResult = null;
        boolean _matched = false;
        if (feature instanceof JvmEnumerationLiteral) {
            _matched = true;
            _switchResult = feature;
        }
        if (!_matched) {
            if (feature instanceof JvmField) {
                _matched = true;
                _switchResult = this.evaluateField(it, ((JvmField) feature), ctx);
            }
        }
        if (!_matched) {
            if (feature instanceof JvmType) {
                _matched = true;
                _switchResult = this.toTypeReference(((JvmType) feature), 0);
            }
        }
        return _switchResult;
    }
    final String featureName = it.getConcreteSyntaxFeatureName();
    try {
        final Object receiver = this.evaluate(it.getMemberCallTarget(), ctx);
        boolean _matched_1 = false;
        if (receiver instanceof JvmTypeReference) {
            _matched_1 = true;
            JvmType _type = ((JvmTypeReference) receiver).getType();
            final JvmType type = _type;
            boolean _matched_2 = false;
            if (type instanceof JvmEnumerationType) {
                _matched_2 = true;
                final Function1<JvmEnumerationLiteral, Boolean> _function = (JvmEnumerationLiteral it_1) -> {
                    String _simpleName = it_1.getSimpleName();
                    return Boolean.valueOf(Objects.equal(_simpleName, featureName));
                };
                final JvmEnumerationLiteral enumValue = IterableExtensions.<JvmEnumerationLiteral>findFirst(((JvmEnumerationType) type).getLiterals(), _function);
                if ((enumValue == null)) {
                    String _simpleName = ((JvmTypeReference) receiver).getSimpleName();
                    String _plus = ((("Couldn\'t find enum value " + featureName) + " on enum ") + _simpleName);
                    throw new ConstantExpressionEvaluationException(_plus, it);
                }
                this.resolveFeature(it, enumValue);
                return enumValue;
            }
            if (!_matched_2) {
                if (type instanceof JvmGenericType) {
                    _matched_2 = true;
                    final Function1<JvmField, Boolean> _function = (JvmField it_1) -> {
                        String _simpleName = it_1.getSimpleName();
                        return Boolean.valueOf(Objects.equal(_simpleName, featureName));
                    };
                    final JvmField field = IterableExtensions.<JvmField>findFirst(Iterables.<JvmField>filter(((JvmGenericType) type).getAllFeatures(), JvmField.class), _function);
                    if ((field == null)) {
                        String _simpleName = ((JvmTypeReference) receiver).getSimpleName();
                        String _plus = ((("Couldn\'t find field " + featureName) + " on type ") + _simpleName);
                        throw new ConstantExpressionEvaluationException(_plus, it);
                    }
                    this.resolveFeature(it, field);
                    return this.evaluateField(it, field, ctx);
                }
            }
        }
        throw new UnresolvableFeatureException(((("Unresolvable feature " + featureName) + " on ") + receiver), it);
    } catch (final Throwable _t) {
        if (_t instanceof UnresolvableFeatureException) {
            final String typeName = this.getFullName(it);
            final JvmType type = this.findTypeByName(it, typeName);
            if ((type != null)) {
                this.resolveType(it, type);
                return this.toTypeReference(type, 0);
            } else {
                throw new UnresolvableFeatureException(("Unresolvable type " + typeName), it);
            }
        } else {
            throw Exceptions.sneakyThrow(_t);
        }
    }
}
Also used : JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) UnresolvableFeatureException(org.eclipse.xtext.xbase.interpreter.UnresolvableFeatureException) JvmType(org.eclipse.xtext.common.types.JvmType) ConstantExpressionEvaluationException(org.eclipse.xtext.xbase.interpreter.ConstantExpressionEvaluationException) StackedConstantExpressionEvaluationException(org.eclipse.xtext.xbase.interpreter.StackedConstantExpressionEvaluationException) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) JvmEnumerationLiteral(org.eclipse.xtext.common.types.JvmEnumerationLiteral) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) JvmField(org.eclipse.xtext.common.types.JvmField) JvmEnumerationType(org.eclipse.xtext.common.types.JvmEnumerationType)

Example 2 with ConstantExpressionEvaluationException

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

the class ConstantExpressionsInterpreterTest method evaluatesWithException.

protected void evaluatesWithException(final String expression, final Procedure1<? super ConstantExpressionEvaluationException> exceptionAssertions) {
    try {
        final XtendFunction function = this.function((("def void testFoo() { " + expression) + " }"));
        XExpression _expression = function.getExpression();
        final XExpression expr = IterableExtensions.<XExpression>head(((XBlockExpression) _expression).getExpressions());
        try {
            this.interpreter.evaluate(expr, null);
            Assert.fail("exception expected");
        } catch (final Throwable _t) {
            if (_t instanceof ConstantExpressionEvaluationException) {
                final ConstantExpressionEvaluationException e = (ConstantExpressionEvaluationException) _t;
                exceptionAssertions.apply(e);
            } else {
                throw Exceptions.sneakyThrow(_t);
            }
        }
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) ConstantExpressionEvaluationException(org.eclipse.xtext.xbase.interpreter.ConstantExpressionEvaluationException) XExpression(org.eclipse.xtext.xbase.XExpression)

Example 3 with ConstantExpressionEvaluationException

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

the class ConstantExpressionsInterpreterTest method testNonConstant.

@Test
public void testNonConstant() {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("class C { ");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("public final static Class<?> REF = D.testFoo;");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        _builder.newLine();
        _builder.append("class D {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("public final static Class<?> testFoo = Object");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        final XtendFile file = this.file(_builder.toString());
        Resource _eResource = file.eResource();
        ((BatchLinkableResource) _eResource).resolveLazyCrossReferences(null);
        final XtendField field = IterableExtensions.<XtendField>head(Iterables.<XtendField>filter(IterableExtensions.<XtendTypeDeclaration>head(file.getXtendTypes()).getMembers(), XtendField.class));
        try {
            this.interpreter.evaluate(field.getInitialValue(), field.getType());
            Assert.fail("exception expected");
        } catch (final Throwable _t) {
            if (_t instanceof ConstantExpressionEvaluationException) {
            } else {
                throw Exceptions.sneakyThrow(_t);
            }
        }
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) BatchLinkableResource(org.eclipse.xtext.xbase.resource.BatchLinkableResource) ConstantExpressionEvaluationException(org.eclipse.xtext.xbase.interpreter.ConstantExpressionEvaluationException) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Resource(org.eclipse.emf.ecore.resource.Resource) BatchLinkableResource(org.eclipse.xtext.xbase.resource.BatchLinkableResource) XtendField(org.eclipse.xtend.core.xtend.XtendField) Test(org.junit.Test)

Example 4 with ConstantExpressionEvaluationException

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

the class ConstantExpressionsInterpreterTest method testConstants_RecursionFails.

@Test
public void testConstants_RecursionFails() {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("import static MyConstants.*");
        _builder.newLine();
        _builder.newLine();
        _builder.append("class C { ");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("val someString = MY_CONST");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        _builder.append("class MyConstants {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("static val MY_CONST = STRING_CONSTANT");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("static val STRING_CONSTANT = MY_CONST");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        final XtendFile file = this.file(_builder.toString());
        final XtendField stringField = IterableExtensions.<XtendField>head(Iterables.<XtendField>filter(IterableExtensions.<XtendTypeDeclaration>head(file.getXtendTypes()).getMembers(), XtendField.class));
        try {
            InputOutput.<Object>println(this.interpreter.evaluate(stringField.getInitialValue(), null));
            Assert.fail("Exception expected");
        } catch (final Throwable _t) {
            if (_t instanceof ConstantExpressionEvaluationException) {
            } else {
                throw Exceptions.sneakyThrow(_t);
            }
        }
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) ConstantExpressionEvaluationException(org.eclipse.xtext.xbase.interpreter.ConstantExpressionEvaluationException) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XtendField(org.eclipse.xtend.core.xtend.XtendField) Test(org.junit.Test)

Example 5 with ConstantExpressionEvaluationException

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

the class ConstantExpressionsInterpreter method evaluateField.

protected Object evaluateField(final XAbstractFeatureCall call, final JvmField field, final Context context) {
    if ((field.isSetConstant() || (field.eResource() instanceof TypeResource))) {
        boolean _isConstant = field.isConstant();
        if (_isConstant) {
            return field.getConstantValue();
        } else {
            String _simpleName = field.getDeclaringType().getSimpleName();
            String _plus = ("Field " + _simpleName);
            String _plus_1 = (_plus + ".");
            String _simpleName_1 = field.getSimpleName();
            String _plus_2 = (_plus_1 + _simpleName_1);
            String _plus_3 = (_plus_2 + " is not a constant");
            throw new ConstantExpressionEvaluationException(_plus_3);
        }
    }
    final XExpression expression = this.containerProvider.getAssociatedExpression(field);
    boolean _contains = context.getAlreadyEvaluating().contains(expression);
    if (_contains) {
        throw new ConstantExpressionEvaluationException("Endless recursive evaluation detected.");
    }
    try {
        final Map<String, JvmIdentifiableElement> visibleFeatures = this.findVisibleFeatures(expression);
        JvmTypeReference _type = field.getType();
        ClassFinder _classFinder = context.getClassFinder();
        Set<XExpression> _alreadyEvaluating = context.getAlreadyEvaluating();
        final Context ctx = new Context(_type, _classFinder, visibleFeatures, _alreadyEvaluating);
        return this.evaluate(expression, ctx);
    } catch (final Throwable _t) {
        if (_t instanceof ConstantExpressionEvaluationException) {
            final ConstantExpressionEvaluationException e = (ConstantExpressionEvaluationException) _t;
            throw new StackedConstantExpressionEvaluationException(call, field, e);
        } else {
            throw Exceptions.sneakyThrow(_t);
        }
    }
}
Also used : Context(org.eclipse.xtext.xbase.interpreter.Context) TypeResource(org.eclipse.xtext.common.types.access.TypeResource) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) ConstantExpressionEvaluationException(org.eclipse.xtext.xbase.interpreter.ConstantExpressionEvaluationException) StackedConstantExpressionEvaluationException(org.eclipse.xtext.xbase.interpreter.StackedConstantExpressionEvaluationException) StackedConstantExpressionEvaluationException(org.eclipse.xtext.xbase.interpreter.StackedConstantExpressionEvaluationException) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) ClassFinder(org.eclipse.xtext.common.types.access.impl.ClassFinder) XExpression(org.eclipse.xtext.xbase.XExpression)

Aggregations

ConstantExpressionEvaluationException (org.eclipse.xtext.xbase.interpreter.ConstantExpressionEvaluationException)6 XtendField (org.eclipse.xtend.core.xtend.XtendField)2 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)2 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)2 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)2 XExpression (org.eclipse.xtext.xbase.XExpression)2 StackedConstantExpressionEvaluationException (org.eclipse.xtext.xbase.interpreter.StackedConstantExpressionEvaluationException)2 Test (org.junit.Test)2 EObject (org.eclipse.emf.ecore.EObject)1 InternalEObject (org.eclipse.emf.ecore.InternalEObject)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)1 JvmAnnotationValue (org.eclipse.xtext.common.types.JvmAnnotationValue)1 JvmEnumerationLiteral (org.eclipse.xtext.common.types.JvmEnumerationLiteral)1 JvmEnumerationType (org.eclipse.xtext.common.types.JvmEnumerationType)1 JvmField (org.eclipse.xtext.common.types.JvmField)1 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)1 JvmIdentifiableElement (org.eclipse.xtext.common.types.JvmIdentifiableElement)1 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)1 JvmType (org.eclipse.xtext.common.types.JvmType)1