Search in sources :

Example 16 with IResolvedTypes

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

the class ErrorTest method testErrorModel_006.

@Test
public void testErrorModel_006() throws Exception {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("@Data class A {}");
    _builder.newLine();
    _builder.append("@");
    _builder.newLine();
    final XtendFile file = this.processWithoutException(_builder);
    final XtendTypeDeclaration typeDeclaration = IterableExtensions.<XtendTypeDeclaration>last(file.getXtendTypes());
    final EList<XAnnotation> annotations = typeDeclaration.getAnnotations();
    final IResolvedTypes resolvedTypes = this.typeResolver.resolveTypes(IterableExtensions.<XAnnotation>head(annotations));
    Assert.assertNotNull(resolvedTypes.getActualType(IterableExtensions.<XAnnotation>head(annotations)));
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) IResolvedTypes(org.eclipse.xtext.xbase.typesystem.IResolvedTypes) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) XAnnotation(org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotation) Test(org.junit.Test)

Example 17 with IResolvedTypes

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

the class ErrorTest method testErrorModel_012.

@Test
public void testErrorModel_012() throws Exception {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class Y {");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("static def <T> IExpectationSetters<T> expect(T value) {");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("}");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("HeaderAccess<?> unboundedMockHeaderAccess");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("def test() {");
    _builder.newLine();
    _builder.append("   ");
    _builder.append("val Object header = unboundedMockHeaderAccess.header");
    _builder.newLine();
    _builder.append("   ");
    _builder.append("val IExpectationSettersObject> exp1 = expect(header)");
    _builder.newLine();
    _builder.append("   ");
    _builder.append("val IExpectationSetters<Object> exp2 = expect(unboundedMockHeaderAccess.getHeader())");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.append("interface HeaderAccess<T> {");
    _builder.newLine();
    _builder.append("   ");
    _builder.append("def T getHeader();");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.append("interface IExpectationSetters<T> {}");
    _builder.newLine();
    final XtendFile file = this.processWithoutException(_builder);
    final XtendTypeDeclaration y = IterableExtensions.<XtendTypeDeclaration>head(file.getXtendTypes());
    XtendMember _get = y.getMembers().get(3);
    final XtendField exp1 = ((XtendField) _get);
    final XExpression initializer = exp1.getInitialValue();
    final IResolvedTypes resolvedTypes = this.typeResolver.resolveTypes(initializer);
    Assert.assertNotNull(resolvedTypes.getActualType(initializer));
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) XtendMember(org.eclipse.xtend.core.xtend.XtendMember) 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)

Example 18 with IResolvedTypes

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

the class CacheMethodCompileStrategy method apply.

@Override
public void apply(ITreeAppendable appendable) {
    JvmOperation cacheMethod = (JvmOperation) logicalContainerProvider.getLogicalContainer(createExtensionInfo.getCreateExpression());
    JvmDeclaredType containerType = cacheMethod.getDeclaringType();
    IResolvedTypes resolvedTypes = typeResolver.resolveTypes(containerType);
    final ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, containerType);
    LightweightTypeReference listType = owner.newReferenceTo(ArrayList.class, new TypeReferenceInitializer<ParameterizedTypeReference>() {

        @Override
        public LightweightTypeReference enhance(ParameterizedTypeReference reference) {
            reference.addTypeArgument(owner.newWildcardTypeReference());
            return reference;
        }
    });
    String cacheVarName = cacheField.getSimpleName();
    String cacheKeyVarName = appendable.declareSyntheticVariable("CacheKey", "_cacheKey");
    appendable.append("final ").append(listType).append(" ").append(cacheKeyVarName).append(" = ").append(CollectionLiterals.class).append(".newArrayList(");
    List<JvmFormalParameter> list = cacheMethod.getParameters();
    for (Iterator<JvmFormalParameter> iterator = list.iterator(); iterator.hasNext(); ) {
        JvmFormalParameter jvmFormalParameter = iterator.next();
        appendable.append(getVarName(jvmFormalParameter));
        if (iterator.hasNext()) {
            appendable.append(", ");
        }
    }
    appendable.append(");");
    // declare result variable
    LightweightTypeReference returnType = resolvedTypes.getActualType(initializerMethod.getParameters().get(0));
    if (returnType != null) {
        appendable.newLine().append("final ").append(returnType);
    } else {
        appendable.newLine().append("final Object");
    }
    String resultVarName = "_result";
    appendable.append(" ").append(resultVarName).append(";");
    // open synchronize block
    appendable.newLine().append("synchronized (").append(cacheVarName).append(") {");
    appendable.increaseIndentation();
    // if the cache contains the key return the previously created object.
    appendable.newLine().append("if (").append(cacheVarName).append(".containsKey(").append(cacheKeyVarName).append(")) {");
    appendable.increaseIndentation();
    appendable.newLine().append("return ").append(cacheVarName).append(".get(").append(cacheKeyVarName).append(");");
    appendable.decreaseIndentation().newLine().append("}");
    // execute the creation
    compiler.toJavaStatement(createExtensionInfo.getCreateExpression(), appendable, true);
    appendable.newLine();
    appendable.append(resultVarName).append(" = ");
    compiler.toJavaExpression(createExtensionInfo.getCreateExpression(), appendable);
    appendable.append(";");
    // store the newly created object in the cache
    appendable.newLine().append(cacheVarName).append(".put(").append(cacheKeyVarName).append(", ");
    LightweightTypeReference fieldType = resolvedTypes.getActualType(cacheField);
    LightweightTypeReference declaredResultType = fieldType.getTypeArguments().get(1);
    boolean castRequired = false;
    if (!declaredResultType.isAssignableFrom(returnType)) {
        castRequired = true;
        appendable.append("(").append(declaredResultType).append(")");
    }
    appendable.append(resultVarName).append(");");
    // close synchronize block
    appendable.decreaseIndentation();
    appendable.newLine().append("}");
    appendable.newLine().append(initializerMethod.getSimpleName()).append("(").append(resultVarName);
    for (JvmFormalParameter parameter : cacheMethod.getParameters()) {
        appendable.append(", ").append(parameter.getName());
    }
    appendable.append(");");
    // return the result
    appendable.newLine().append("return ");
    if (castRequired) {
        appendable.append("(").append(declaredResultType).append(")");
    }
    appendable.append(resultVarName).append(";");
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) CollectionLiterals(org.eclipse.xtext.xbase.lib.CollectionLiterals) IResolvedTypes(org.eclipse.xtext.xbase.typesystem.IResolvedTypes) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) ITypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) ParameterizedTypeReference(org.eclipse.xtext.xbase.typesystem.references.ParameterizedTypeReference) StandardTypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)

Example 19 with IResolvedTypes

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

the class AnonymousClassTypeTest method testAnonymousWithAdditionalMember.

@Test
public void testAnonymousWithAdditionalMember() {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("def foo() {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("val foo = new Runnable() {");
        _builder.newLine();
        _builder.append("\t\t");
        _builder.append("int bar");
        _builder.newLine();
        _builder.append("\t\t");
        _builder.append("override run() {}");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("}");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("foo");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        final XtendFunction function = this.function(_builder.toString());
        final JvmOperation operation = this._iXtendJvmAssociations.getDirectlyInferredOperation(function);
        final IResolvedTypes resolvedTypes = this._iBatchTypeResolver.resolveTypes(operation.eResource());
        Assert.assertEquals("Runnable", resolvedTypes.getActualType(operation).toString());
        XExpression _expression = function.getExpression();
        final XExpression variable = IterableExtensions.<XExpression>last(((XBlockExpression) _expression).getExpressions());
        final LightweightTypeReference variableType = resolvedTypes.getActualType(variable);
        Assert.assertEquals("__Foo_1", variableType.toString());
        Assert.assertTrue(variableType.isSubtypeOf(Runnable.class));
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) IResolvedTypes(org.eclipse.xtext.xbase.typesystem.IResolvedTypes) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XExpression(org.eclipse.xtext.xbase.XExpression) Test(org.junit.Test)

Example 20 with IResolvedTypes

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

the class AnonymousClassTypeTest method testPlainAnonymous.

@Test
public void testPlainAnonymous() {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("def foo() {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("new Runnable() {");
        _builder.newLine();
        _builder.append("\t\t");
        _builder.append("override run() {}");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("}");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        final JvmOperation operation = this._iXtendJvmAssociations.getDirectlyInferredOperation(this.function(_builder.toString()));
        final IResolvedTypes resolvedTypes = this._iBatchTypeResolver.resolveTypes(operation.eResource());
        Assert.assertEquals("Runnable", resolvedTypes.getActualType(operation).toString());
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) IResolvedTypes(org.eclipse.xtext.xbase.typesystem.IResolvedTypes) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Test(org.junit.Test)

Aggregations

IResolvedTypes (org.eclipse.xtext.xbase.typesystem.IResolvedTypes)32 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)25 Test (org.junit.Test)25 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)21 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)19 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)14 XExpression (org.eclipse.xtext.xbase.XExpression)14 XAnnotation (org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotation)11 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)9 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)7 EObject (org.eclipse.emf.ecore.EObject)6 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)6 JvmFormalParameter (org.eclipse.xtext.common.types.JvmFormalParameter)5 XtendField (org.eclipse.xtend.core.xtend.XtendField)4 AnonymousClass (org.eclipse.xtend.core.xtend.AnonymousClass)3 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)3 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)3 JvmIdentifiableElement (org.eclipse.xtext.common.types.JvmIdentifiableElement)3 XClosure (org.eclipse.xtext.xbase.XClosure)3 XMemberFeatureCall (org.eclipse.xtext.xbase.XMemberFeatureCall)3