Search in sources :

Example 1 with ParameterizedTypeReference

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

the class AbstractOverloadedInstanceMethodTest method linksTo.

protected void linksTo(final String invocation, final String method) {
    try {
        final XtendFile file = this.file(this.inMethodBody(invocation), false);
        XtendTypeDeclaration _head = IterableExtensions.<XtendTypeDeclaration>head(file.getXtendTypes());
        final XtendClass c = ((XtendClass) _head);
        XtendMember _head_1 = IterableExtensions.<XtendMember>head(c.getMembers());
        final XtendFunction m = ((XtendFunction) _head_1);
        XExpression _expression = m.getExpression();
        final XBlockExpression body = ((XBlockExpression) _expression);
        XExpression _last = IterableExtensions.<XExpression>last(body.getExpressions());
        final XAbstractFeatureCall featureCall = ((XAbstractFeatureCall) _last);
        JvmIdentifiableElement _feature = featureCall.getFeature();
        final JvmOperation operation = ((JvmOperation) _feature);
        final StandardTypeReferenceOwner owner = new StandardTypeReferenceOwner(this.services, file);
        final ParameterizedTypeReference declaration = owner.newParameterizedTypeReference(operation.getDeclaringType());
        final BottomResolvedOperation resolved = new BottomResolvedOperation(operation, declaration, this.overrideTester);
        Assert.assertEquals(method, resolved.getSimpleSignature());
        Assert.assertTrue(IterableExtensions.join(file.eResource().getErrors(), "\n"), file.eResource().getErrors().isEmpty());
        Assert.assertNotNull(featureCall.getImplicitReceiver());
        Assert.assertNull(featureCall.getImplicitFirstArgument());
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) XBlockExpression(org.eclipse.xtext.xbase.XBlockExpression) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) XtendMember(org.eclipse.xtend.core.xtend.XtendMember) XAbstractFeatureCall(org.eclipse.xtext.xbase.XAbstractFeatureCall) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) ParameterizedTypeReference(org.eclipse.xtext.xbase.typesystem.references.ParameterizedTypeReference) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) XExpression(org.eclipse.xtext.xbase.XExpression) BottomResolvedOperation(org.eclipse.xtext.xbase.typesystem.override.BottomResolvedOperation) StandardTypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)

Example 2 with ParameterizedTypeReference

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

the class AbstractOverloadedStaticMethodTest method linksTo.

protected void linksTo(final String invocation, final String method) {
    try {
        final XtendFile file = this.file(this.inMethodBody(invocation), false);
        XtendTypeDeclaration _head = IterableExtensions.<XtendTypeDeclaration>head(file.getXtendTypes());
        final XtendClass c = ((XtendClass) _head);
        XtendMember _head_1 = IterableExtensions.<XtendMember>head(c.getMembers());
        final XtendFunction m = ((XtendFunction) _head_1);
        XExpression _expression = m.getExpression();
        final XBlockExpression body = ((XBlockExpression) _expression);
        XExpression _last = IterableExtensions.<XExpression>last(body.getExpressions());
        final XAbstractFeatureCall featureCall = ((XAbstractFeatureCall) _last);
        JvmIdentifiableElement _feature = featureCall.getFeature();
        final JvmOperation operation = ((JvmOperation) _feature);
        final StandardTypeReferenceOwner owner = new StandardTypeReferenceOwner(this.services, file);
        final ParameterizedTypeReference declaration = owner.newParameterizedTypeReference(operation.getDeclaringType());
        final BottomResolvedOperation resolved = new BottomResolvedOperation(operation, declaration, this.overrideTester);
        Assert.assertEquals(method, resolved.getSimpleSignature());
        Assert.assertTrue(IterableExtensions.join(file.eResource().getErrors(), "\n"), file.eResource().getErrors().isEmpty());
        Assert.assertNull(featureCall.getImplicitReceiver());
        Assert.assertNull(featureCall.getImplicitFirstArgument());
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) XBlockExpression(org.eclipse.xtext.xbase.XBlockExpression) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) XtendMember(org.eclipse.xtend.core.xtend.XtendMember) XAbstractFeatureCall(org.eclipse.xtext.xbase.XAbstractFeatureCall) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) ParameterizedTypeReference(org.eclipse.xtext.xbase.typesystem.references.ParameterizedTypeReference) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) XExpression(org.eclipse.xtext.xbase.XExpression) BottomResolvedOperation(org.eclipse.xtext.xbase.typesystem.override.BottomResolvedOperation) StandardTypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)

Example 3 with ParameterizedTypeReference

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

the class CreateMemberQuickfixes method getReceiverType.

/* @Nullable */
protected LightweightTypeReference getReceiverType(XAbstractFeatureCall featureCall) {
    XExpression actualReceiver = featureCall.getActualReceiver();
    ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, featureCall);
    if (actualReceiver == null) {
        JvmDeclaredType callersType = getCallersType(featureCall);
        if (callersType != null)
            return owner.newParameterizedTypeReference(callersType);
    } else if (actualReceiver instanceof XAbstractFeatureCall && ((XAbstractFeatureCall) actualReceiver).isTypeLiteral()) {
        JvmType type = (JvmType) ((XAbstractFeatureCall) actualReceiver).getFeature();
        ParameterizedTypeReference reference = owner.newParameterizedTypeReference(type);
        return reference;
    } else {
        LightweightTypeReference typeRef = typeResolver.resolveTypes(featureCall).getActualType(actualReceiver);
        if (typeRef != null && typeRef.getType() instanceof JvmDeclaredType)
            return typeRef;
    }
    return null;
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) ParameterizedTypeReference(org.eclipse.xtext.xbase.typesystem.references.ParameterizedTypeReference) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) XExpression(org.eclipse.xtext.xbase.XExpression) JvmType(org.eclipse.xtext.common.types.JvmType) ITypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner) StandardTypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner) XAbstractFeatureCall(org.eclipse.xtext.xbase.XAbstractFeatureCall)

Example 4 with ParameterizedTypeReference

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

the class SuperMemberImplementorTest method getContextType.

protected LightweightTypeReference getContextType() {
    JvmGenericType inferredType = associations.getInferredType(xtendClass);
    ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, inferredType);
    ParameterizedTypeReference contextType = owner.newParameterizedTypeReference(inferredType);
    for (JvmTypeParameter typeParamter : inferredType.getTypeParameters()) {
        contextType.addTypeArgument(owner.newParameterizedTypeReference(typeParamter));
    }
    return contextType;
}
Also used : ParameterizedTypeReference(org.eclipse.xtext.xbase.typesystem.references.ParameterizedTypeReference) JvmTypeParameter(org.eclipse.xtext.common.types.JvmTypeParameter) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) ITypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner) StandardTypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)

Example 5 with ParameterizedTypeReference

use of org.eclipse.xtext.xbase.typesystem.references.ParameterizedTypeReference 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)

Aggregations

ParameterizedTypeReference (org.eclipse.xtext.xbase.typesystem.references.ParameterizedTypeReference)6 StandardTypeReferenceOwner (org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)6 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)3 XAbstractFeatureCall (org.eclipse.xtext.xbase.XAbstractFeatureCall)3 XExpression (org.eclipse.xtext.xbase.XExpression)3 ITypeReferenceOwner (org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner)3 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)2 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)2 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)2 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)2 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)2 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)2 JvmIdentifiableElement (org.eclipse.xtext.common.types.JvmIdentifiableElement)2 XBlockExpression (org.eclipse.xtext.xbase.XBlockExpression)2 BottomResolvedOperation (org.eclipse.xtext.xbase.typesystem.override.BottomResolvedOperation)2 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)2 JvmFormalParameter (org.eclipse.xtext.common.types.JvmFormalParameter)1 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)1 JvmType (org.eclipse.xtext.common.types.JvmType)1 JvmTypeParameter (org.eclipse.xtext.common.types.JvmTypeParameter)1