Search in sources :

Example 66 with JvmTypeReference

use of org.eclipse.xtext.common.types.JvmTypeReference in project xtext-xtend by eclipse.

the class XtendReentrantTypeResolver method _doPrepare.

/**
 * Initializes the type inference strategy for the cache field for create extensions.
 */
@Override
protected void _doPrepare(ResolvedTypes resolvedTypes, IFeatureScopeSession featureScopeSession, JvmField field, Map<JvmIdentifiableElement, ResolvedTypes> resolvedTypesByContext) {
    JvmTypeReference knownType = field.getType();
    if (InferredTypeIndicator.isInferred(knownType)) {
        XComputedTypeReference castedKnownType = (XComputedTypeReference) knownType;
        EObject sourceElement = associations.getPrimarySourceElement(field);
        if (sourceElement instanceof XtendFunction) {
            XtendFunction function = (XtendFunction) sourceElement;
            if (function.getCreateExtensionInfo() != null) {
                JvmOperation operation = associations.getDirectlyInferredOperation(function);
                if (operation != null) {
                    declareTypeParameters(resolvedTypes, field, resolvedTypesByContext);
                    XComputedTypeReference fieldType = getServices().getXtypeFactory().createXComputedTypeReference();
                    fieldType.setTypeProvider(new CreateCacheFieldTypeReferenceProvider(operation, resolvedTypes, featureScopeSession));
                    castedKnownType.setEquivalent(fieldType);
                    return;
                }
            }
        }
    }
    super._doPrepare(resolvedTypes, featureScopeSession, field, resolvedTypesByContext);
    doPrepareLocalTypes(resolvedTypesByContext.get(field), featureScopeSession, field, resolvedTypesByContext);
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) EObject(org.eclipse.emf.ecore.EObject) XComputedTypeReference(org.eclipse.xtext.xtype.XComputedTypeReference)

Example 67 with JvmTypeReference

use of org.eclipse.xtext.common.types.JvmTypeReference in project xtext-xtend by eclipse.

the class XtendValidator method checkSuperTypes.

@Check
public void checkSuperTypes(AnonymousClass anonymousClass) {
    JvmGenericType inferredType = associations.getInferredType(anonymousClass);
    if (inferredType != null) {
        JvmTypeReference superTypeRef = Iterables.getLast(inferredType.getSuperTypes());
        JvmType superType = superTypeRef.getType();
        if (superType instanceof JvmGenericType && ((JvmGenericType) superType).isFinal())
            error("Attempt to override final class", anonymousClass.getConstructorCall(), XCONSTRUCTOR_CALL__CONSTRUCTOR, INSIGNIFICANT_INDEX, OVERRIDDEN_FINAL);
    }
}
Also used : JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) JvmType(org.eclipse.xtext.common.types.JvmType) Check(org.eclipse.xtext.validation.Check)

Example 68 with JvmTypeReference

use of org.eclipse.xtext.common.types.JvmTypeReference in project xtext-xtend by eclipse.

the class XtendValidator method contributesToConflict.

/**
 * Determine whether the given type contributes to the conflict caused by the given default interface implementation.
 */
private boolean contributesToConflict(JvmGenericType rootType, ConflictingDefaultOperation conflictingDefaultOperation) {
    Set<JvmDeclaredType> involvedInterfaces = Sets.newHashSet();
    involvedInterfaces.add(conflictingDefaultOperation.getDeclaration().getDeclaringType());
    for (IResolvedOperation conflictingOperation : conflictingDefaultOperation.getConflictingOperations()) {
        involvedInterfaces.add(conflictingOperation.getDeclaration().getDeclaringType());
    }
    RecursionGuard<JvmDeclaredType> recursionGuard = new RecursionGuard<JvmDeclaredType>();
    if (rootType.isInterface()) {
        int contributingCount = 0;
        for (JvmTypeReference typeRef : rootType.getExtendedInterfaces()) {
            JvmType rawType = typeRef.getType();
            if (rawType instanceof JvmDeclaredType && contributesToConflict((JvmDeclaredType) rawType, involvedInterfaces, recursionGuard)) {
                contributingCount++;
            }
        }
        return contributingCount >= 2;
    } else {
        return contributesToConflict(rootType, involvedInterfaces, recursionGuard);
    }
}
Also used : JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) RecursionGuard(org.eclipse.xtext.xbase.typesystem.util.RecursionGuard) JvmType(org.eclipse.xtext.common.types.JvmType) IResolvedOperation(org.eclipse.xtext.xbase.typesystem.override.IResolvedOperation)

Example 69 with JvmTypeReference

use of org.eclipse.xtext.common.types.JvmTypeReference in project xtext-xtend by eclipse.

the class XtendValidator method checkSuperTypes.

@Check
public void checkSuperTypes(XtendClass xtendClass) {
    JvmTypeReference superClass = xtendClass.getExtends();
    if (superClass != null && superClass.getType() != null) {
        if (!(superClass.getType() instanceof JvmGenericType) || ((JvmGenericType) superClass.getType()).isInterface()) {
            error("Superclass must be a class", XTEND_CLASS__EXTENDS, CLASS_EXPECTED);
        } else {
            if (((JvmGenericType) superClass.getType()).isFinal()) {
                error("Attempt to override final class", XTEND_CLASS__EXTENDS, OVERRIDDEN_FINAL);
            }
            checkWildcardSupertype(xtendClass, superClass, XTEND_CLASS__EXTENDS, INSIGNIFICANT_INDEX);
        }
    }
    for (int i = 0; i < xtendClass.getImplements().size(); ++i) {
        JvmTypeReference implementedType = xtendClass.getImplements().get(i);
        if (!isInterface(implementedType.getType())) {
            error("Implemented interface must be an interface", XTEND_CLASS__IMPLEMENTS, i, INTERFACE_EXPECTED);
        }
        checkWildcardSupertype(xtendClass, implementedType, XTEND_CLASS__IMPLEMENTS, i);
    }
    JvmGenericType inferredType = associations.getInferredType(xtendClass);
    if (inferredType != null && hasCycleInHierarchy(inferredType, Sets.<JvmGenericType>newHashSet())) {
        error("The inheritance hierarchy of " + notNull(xtendClass.getName()) + " contains cycles", XTEND_TYPE_DECLARATION__NAME, CYCLIC_INHERITANCE);
    }
}
Also used : JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) Check(org.eclipse.xtext.validation.Check)

Example 70 with JvmTypeReference

use of org.eclipse.xtext.common.types.JvmTypeReference in project xtext-xtend by eclipse.

the class XtendValidator method reportMissingImplementations.

protected void reportMissingImplementations(XtendTypeDeclaration xtendClass, JvmGenericType inferredType, List<IResolvedOperation> operationsMissingImplementation) {
    StringBuilder errorMsg = new StringBuilder();
    String name = xtendClass.getName();
    boolean needsNewLine = operationsMissingImplementation.size() > 1;
    if (xtendClass.isAnonymous()) {
        JvmTypeReference superType = Iterables.getLast(inferredType.getSuperTypes());
        errorMsg.append("The anonymous subclass of ").append(superType.getSimpleName());
        errorMsg.append(" does not implement ");
    } else {
        errorMsg.append("The class ").append(name);
        errorMsg.append(" must be defined abstract because it does not implement ");
    }
    if (needsNewLine) {
        errorMsg.append("its inherited abstract methods ");
    }
    IResolvedOperation operation;
    for (int i = 0; i < operationsMissingImplementation.size() && i < 3; ++i) {
        operation = operationsMissingImplementation.get(i);
        if (needsNewLine)
            errorMsg.append("\n- ");
        errorMsg.append(operation.getSimpleSignature());
    }
    int numUnshownOperations = operationsMissingImplementation.size() - 3;
    if (numUnshownOperations > 0)
        errorMsg.append("\nand " + numUnshownOperations + " more.");
    List<String> uris = transform(operationsMissingImplementation, new Function<IResolvedOperation, String>() {

        @Override
        public String apply(IResolvedOperation from) {
            return EcoreUtil.getURI(from.getDeclaration()).toString();
        }
    });
    if (xtendClass.isAnonymous()) {
        error(errorMsg.toString(), xtendClass, ANONYMOUS_CLASS__CONSTRUCTOR_CALL, ANONYMOUS_CLASS_MISSING_MEMBERS, toArray(uris, String.class));
    } else {
        error(errorMsg.toString(), xtendClass, XTEND_TYPE_DECLARATION__NAME, CLASS_MUST_BE_ABSTRACT, toArray(uris, String.class));
    }
}
Also used : ToStringBuilder(org.eclipse.xtext.xbase.lib.util.ToStringBuilder) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) RichString(org.eclipse.xtend.core.xtend.RichString) IResolvedOperation(org.eclipse.xtext.xbase.typesystem.override.IResolvedOperation)

Aggregations

JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)130 Test (org.junit.Test)58 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)46 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)38 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)36 JvmType (org.eclipse.xtext.common.types.JvmType)28 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)23 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)18 XExpression (org.eclipse.xtext.xbase.XExpression)18 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)17 EObject (org.eclipse.emf.ecore.EObject)15 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)12 JvmTypeParameter (org.eclipse.xtext.common.types.JvmTypeParameter)12 XBlockExpression (org.eclipse.xtext.xbase.XBlockExpression)12 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)12 AnonymousClass (org.eclipse.xtend.core.xtend.AnonymousClass)8 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)8 TypeReference (org.eclipse.xtend.lib.macro.declaration.TypeReference)8 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)8 JvmParameterizedTypeReference (org.eclipse.xtext.common.types.JvmParameterizedTypeReference)8