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);
}
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
}
Aggregations