Search in sources :

Example 56 with XtendField

use of org.eclipse.xtend.core.xtend.XtendField 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 57 with XtendField

use of org.eclipse.xtend.core.xtend.XtendField 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 58 with XtendField

use of org.eclipse.xtend.core.xtend.XtendField in project xtext-xtend by eclipse.

the class ConstantExpressionsInterpreterTest method assertEvaluatesTo.

protected void assertEvaluatesTo(final Object expectation, final Pair<String, String> left, final Pair<String, String> right, final String op) {
    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("static val result = A ");
        _builder.append(op, "\t");
        _builder.append(" B");
        _builder.newLineIfNotEmpty();
        _builder.append("}");
        _builder.newLine();
        _builder.append("class MyConstants {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("static val ");
        String _value = left.getValue();
        _builder.append(_value, "\t");
        _builder.append(" A = ");
        String _key = left.getKey();
        _builder.append(_key, "\t");
        _builder.append(" as ");
        String _value_1 = left.getValue();
        _builder.append(_value_1, "\t");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append("static val ");
        String _value_2 = right.getValue();
        _builder.append(_value_2, "\t");
        _builder.append(" B = ");
        String _key_1 = right.getKey();
        _builder.append(_key_1, "\t");
        _builder.append(" as ");
        String _value_3 = right.getValue();
        _builder.append(_value_3, "\t");
        _builder.newLineIfNotEmpty();
        _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));
        Assert.assertEquals(expectation, this.interpreter.evaluate(stringField.getInitialValue(), null));
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XtendField(org.eclipse.xtend.core.xtend.XtendField)

Example 59 with XtendField

use of org.eclipse.xtend.core.xtend.XtendField in project xtext-xtend by eclipse.

the class ErrorTest method testErrorModel_017.

@Test
public void testErrorModel_017() throws Exception {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("import static extension Throwables.*");
    _builder.newLine();
    _builder.append("import java.net.URI");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class Throwables {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def static <T> T propagate(()=>T proc, (Exception)=>Exception handler) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("try {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("proc.apply");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("} catch(Exception e) {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("throw handler.apply(e)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.append("class Client {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("valuri = [| new URI(\'\')].propagate [ new IllegalArgumentException(it) ]");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final XtendFile file = this.processWithoutException(_builder);
    final XtendTypeDeclaration client = IterableExtensions.<XtendTypeDeclaration>last(file.getXtendTypes());
    XtendMember _head = IterableExtensions.<XtendMember>head(client.getMembers());
    final XtendField field = ((XtendField) _head);
    XExpression _initialValue = field.getInitialValue();
    final XMemberFeatureCall initializer = ((XMemberFeatureCall) _initialValue);
    XExpression _head_1 = IterableExtensions.<XExpression>head(initializer.getMemberCallArguments());
    final XClosure closure = ((XClosure) _head_1);
    final JvmFormalParameter implicit = IterableExtensions.<JvmFormalParameter>head(closure.getImplicitFormalParameters());
    Assert.assertNotNull(implicit);
    final IResolvedTypes resolvedTypes = this.typeResolver.resolveTypes(implicit);
    Assert.assertNotNull(resolvedTypes.getActualType(implicit));
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) XtendMember(org.eclipse.xtend.core.xtend.XtendMember) XClosure(org.eclipse.xtext.xbase.XClosure) IResolvedTypes(org.eclipse.xtext.xbase.typesystem.IResolvedTypes) XMemberFeatureCall(org.eclipse.xtext.xbase.XMemberFeatureCall) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) XExpression(org.eclipse.xtext.xbase.XExpression) XtendField(org.eclipse.xtend.core.xtend.XtendField) Test(org.junit.Test)

Example 60 with XtendField

use of org.eclipse.xtend.core.xtend.XtendField in project xtext-xtend by eclipse.

the class ErrorTest method testErrorModel_019.

@Test
public void testErrorModel_019() throws Exception {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package x");
    _builder.newLine();
    _builder.append("import bug396879.*");
    _builder.newLine();
    _builder.append("class Z {");
    _builder.newLine();
    _builder.append("  \t");
    _builder.append("def addListeners(BooleanProperty prop) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val ChangeListenerBoolean> listener = [ p, oldValue, newValue | ]\t\tprop.addListener(listener)");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final XtendFile file = this.processWithoutException(_builder);
    final XtendTypeDeclaration z = IterableExtensions.<XtendTypeDeclaration>head(file.getXtendTypes());
    XtendMember _get = z.getMembers().get(1);
    final XtendField field = ((XtendField) _get);
    XExpression _initialValue = field.getInitialValue();
    final XClosure closure = ((XClosure) _initialValue);
    final JvmFormalParameter param = IterableExtensions.<JvmFormalParameter>head(closure.getDeclaredFormalParameters());
    final IResolvedTypes resolvedTypes = this.typeResolver.resolveTypes(param);
    Assert.assertNotNull(resolvedTypes.getActualType(param));
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) XtendMember(org.eclipse.xtend.core.xtend.XtendMember) XClosure(org.eclipse.xtext.xbase.XClosure) IResolvedTypes(org.eclipse.xtext.xbase.typesystem.IResolvedTypes) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) XExpression(org.eclipse.xtext.xbase.XExpression) XtendField(org.eclipse.xtend.core.xtend.XtendField) Test(org.junit.Test)

Aggregations

XtendField (org.eclipse.xtend.core.xtend.XtendField)71 Test (org.junit.Test)50 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)38 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)33 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)29 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)23 XExpression (org.eclipse.xtext.xbase.XExpression)23 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)14 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)12 EObject (org.eclipse.emf.ecore.EObject)8 RichString (org.eclipse.xtend.core.xtend.RichString)7 XtendConstructor (org.eclipse.xtend.core.xtend.XtendConstructor)6 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)6 XClosure (org.eclipse.xtext.xbase.XClosure)6 JvmIdentifiableElement (org.eclipse.xtext.common.types.JvmIdentifiableElement)5 XBlockExpression (org.eclipse.xtext.xbase.XBlockExpression)5 XtendAnnotationType (org.eclipse.xtend.core.xtend.XtendAnnotationType)4 JvmFormalParameter (org.eclipse.xtext.common.types.JvmFormalParameter)4 IResolvedTypes (org.eclipse.xtext.xbase.typesystem.IResolvedTypes)4 AnonymousClass (org.eclipse.xtend.core.xtend.AnonymousClass)3