Search in sources :

Example 11 with ITypeReferenceOwner

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

the class DeferredTypeParameterHintCollectorTest method in.

public Map<JvmTypeParameter, LightweightMergedBoundTypeArgument> in(final String typeParameters, final String expectedType, final String actualType) {
    final JvmOperation operation = this.operation(typeParameters, expectedType, actualType);
    ITypeReferenceOwner _owner = this.getOwner();
    final DeferredTypeParameterHintCollector collector = new DeferredTypeParameterHintCollector(_owner);
    final DefaultReentrantTypeResolver resolver = this.resolverProvider.get();
    resolver.initializeFrom(EcoreUtil.getRootContainer(operation));
    ITypeReferenceOwner _owner_1 = this.getOwner();
    PublicResolvedTypes _publicResolvedTypes = new PublicResolvedTypes(resolver);
    final MockTypeParameterSubstitutor substitutor = new MockTypeParameterSubstitutor(_owner_1, _publicResolvedTypes);
    final LightweightTypeReference hasUnbounds = substitutor.substitute(this.toLightweightTypeReference(IterableExtensions.<JvmFormalParameter>head(operation.getParameters()).getParameterType()));
    final LightweightTypeReference isActual = this.toLightweightTypeReference(IterableExtensions.<JvmFormalParameter>last(operation.getParameters()).getParameterType());
    collector.processPairedReferences(hasUnbounds, isActual);
    return substitutor.getTypeParameterMapping();
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) DeferredTypeParameterHintCollector(org.eclipse.xtext.xbase.typesystem.util.DeferredTypeParameterHintCollector) ITypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner) DefaultReentrantTypeResolver(org.eclipse.xtext.xbase.typesystem.internal.DefaultReentrantTypeResolver)

Example 12 with ITypeReferenceOwner

use of org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner 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 13 with ITypeReferenceOwner

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

the class ReplacingAppendableTest method insertListField.

protected XtextDocument insertListField(final String model, final String fieldName) throws Exception {
    final int cursorPosition = model.indexOf('|');
    String actualModel = model.replace("|", " ");
    final XtendFile file = testHelper.xtendFile("Foo", actualModel);
    final XtextDocument document = documentProvider.get();
    document.set(actualModel);
    XtextResource xtextResource = (XtextResource) file.eResource();
    document.setInput(xtextResource);
    final EObject context = eObjectAtOffsetHelper.resolveElementAt(xtextResource, cursorPosition);
    document.modify(new IUnitOfWork.Void<XtextResource>() {

        @Override
        public void process(XtextResource state) throws Exception {
            ReplacingAppendable a = appendableFactory.create(document, state, cursorPosition, 1);
            ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, context);
            LightweightTypeReference typeRef = owner.toLightweightTypeReference(services.getTypeReferences().getTypeForName(List.class, context, typesFactory.createJvmWildcardTypeReference()));
            a.append(typeRef);
            a.append(" ").append(fieldName);
            a.commitChanges();
        }
    });
    return document;
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) XtextDocument(org.eclipse.xtext.ui.editor.model.XtextDocument) XtextResource(org.eclipse.xtext.resource.XtextResource) ReplacingAppendable(org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable) ITypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner) IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) EObject(org.eclipse.emf.ecore.EObject) StandardTypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)

Example 14 with ITypeReferenceOwner

use of org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner 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 15 with ITypeReferenceOwner

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

the class XtendValidator method checkExceptions.

private void checkExceptions(EObject context, List<JvmTypeReference> exceptions, EReference reference) {
    Set<String> declaredExceptionNames = Sets.newHashSet();
    JvmTypeReference throwableType = getServices().getTypeReferences().getTypeForName(Throwable.class, context);
    if (throwableType == null) {
        return;
    }
    ITypeReferenceOwner owner = new StandardTypeReferenceOwner(getServices(), context);
    LightweightTypeReference throwableReference = owner.toLightweightTypeReference(throwableType);
    for (int i = 0; i < exceptions.size(); i++) {
        JvmTypeReference exception = exceptions.get(i);
        // throwables may not carry generics thus the raw comparison is sufficient
        if (exception.getType() != null && !exception.getType().eIsProxy()) {
            if (!throwableReference.isAssignableFrom(exception.getType()))
                error("No exception of type " + exception.getSimpleName() + " can be thrown; an exception type must be a subclass of Throwable", reference, i, EXCEPTION_NOT_THROWABLE);
            if (!declaredExceptionNames.add(exception.getQualifiedName()))
                error("Exception " + exception.getSimpleName() + " is declared twice", reference, i, EXCEPTION_DECLARED_TWICE);
        }
    }
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) RichString(org.eclipse.xtend.core.xtend.RichString) ITypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner) StandardTypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)

Aggregations

ITypeReferenceOwner (org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner)16 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)11 StandardTypeReferenceOwner (org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)11 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)6 EObject (org.eclipse.emf.ecore.EObject)4 JvmTypeParameter (org.eclipse.xtext.common.types.JvmTypeParameter)4 List (java.util.List)3 EList (org.eclipse.emf.common.util.EList)3 WrappedException (org.eclipse.emf.common.util.WrappedException)3 BadLocationException (org.eclipse.jface.text.BadLocationException)3 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)3 JvmFormalParameter (org.eclipse.xtext.common.types.JvmFormalParameter)3 JvmType (org.eclipse.xtext.common.types.JvmType)3 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)3 IModificationContext (org.eclipse.xtext.ui.editor.model.edit.IModificationContext)3 ISemanticModification (org.eclipse.xtext.ui.editor.model.edit.ISemanticModification)3 Fix (org.eclipse.xtext.ui.editor.quickfix.Fix)3 ParameterizedTypeReference (org.eclipse.xtext.xbase.typesystem.references.ParameterizedTypeReference)3 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)2 XAbstractFeatureCall (org.eclipse.xtext.xbase.XAbstractFeatureCall)2