Search in sources :

Example 51 with JvmDeclaredType

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

the class XtendValidator method doCheckOverriddenMethods.

protected void doCheckOverriddenMethods(XtendTypeDeclaration xtendType, JvmGenericType inferredType, ResolvedFeatures resolvedFeatures, Set<EObject> flaggedOperations) {
    List<IResolvedOperation> operationsMissingImplementation = null;
    boolean doCheckAbstract = !inferredType.isAbstract();
    if (doCheckAbstract) {
        operationsMissingImplementation = Lists.newArrayList();
    }
    IVisibilityHelper visibilityHelper = new ContextualVisibilityHelper(this.visibilityHelper, resolvedFeatures.getType());
    boolean flaggedType = false;
    for (IResolvedOperation operation : resolvedFeatures.getAllOperations()) {
        JvmDeclaredType operationDeclaringType = operation.getDeclaration().getDeclaringType();
        if (operationDeclaringType != inferredType) {
            if (operationsMissingImplementation != null && operation.getDeclaration().isAbstract()) {
                operationsMissingImplementation.add(operation);
            }
            if (visibilityHelper.isVisible(operation.getDeclaration())) {
                String erasureSignature = operation.getResolvedErasureSignature();
                List<IResolvedOperation> declaredOperationsWithSameErasure = resolvedFeatures.getDeclaredOperations(erasureSignature);
                for (IResolvedOperation localOperation : declaredOperationsWithSameErasure) {
                    if (!localOperation.isOverridingOrImplementing(operation.getDeclaration()).isOverridingOrImplementing()) {
                        EObject source = findPrimarySourceElement(localOperation);
                        if (operation.getDeclaration().isStatic() && !localOperation.getDeclaration().isStatic()) {
                            if (!isInterface(operationDeclaringType)) {
                                if (flaggedOperations.add(source)) {
                                    error("The instance method " + localOperation.getSimpleSignature() + " cannot override the static method " + operation.getSimpleSignature() + " of type " + getDeclaratorName(operation.getDeclaration()) + ".", source, nameFeature(source), DUPLICATE_METHOD);
                                }
                            }
                        } else if (!operation.getDeclaration().isStatic() && localOperation.getDeclaration().isStatic()) {
                            if (flaggedOperations.add(source)) {
                                error("The static method " + localOperation.getSimpleSignature() + " cannot hide the instance method " + operation.getSimpleSignature() + " of type " + getDeclaratorName(operation.getDeclaration()) + ".", source, nameFeature(source), DUPLICATE_METHOD);
                            }
                        } else if (flaggedOperations.add(source)) {
                            error("Name clash: The method " + localOperation.getSimpleSignature() + " of type " + inferredType.getSimpleName() + " has the same erasure as " + operation.getSimpleSignature() + " of type " + getDeclaratorName(operation.getDeclaration()) + " but does not override it.", source, nameFeature(source), DUPLICATE_METHOD);
                        }
                    }
                }
                if (operation instanceof ConflictingDefaultOperation && contributesToConflict(inferredType, (ConflictingDefaultOperation) operation) && !flaggedType) {
                    IResolvedOperation conflictingOperation = ((ConflictingDefaultOperation) operation).getConflictingOperations().get(0);
                    // Include the declaring class in the issue code in order to give better quick fixes
                    String[] uris = new String[] { getDeclaratorName(operation.getDeclaration()) + "|" + EcoreUtil.getURI(operation.getDeclaration()).toString(), getDeclaratorName(conflictingOperation.getDeclaration()) + "|" + EcoreUtil.getURI(conflictingOperation.getDeclaration()).toString() };
                    if (!operation.getDeclaration().isAbstract() && !conflictingOperation.getDeclaration().isAbstract()) {
                        error("The type " + inferredType.getSimpleName() + " inherits multiple implementations of the method " + conflictingOperation.getSimpleSignature() + " from " + getDeclaratorName(conflictingOperation.getDeclaration()) + " and " + getDeclaratorName(operation.getDeclaration()) + ".", xtendType, XtendPackage.Literals.XTEND_TYPE_DECLARATION__NAME, CONFLICTING_DEFAULT_METHODS, uris);
                    } else {
                        // At least one of the operations is non-abstract
                        IResolvedOperation abstractOp, nonabstractOp;
                        if (operation.getDeclaration().isAbstract()) {
                            abstractOp = operation;
                            nonabstractOp = conflictingOperation;
                        } else {
                            abstractOp = conflictingOperation;
                            nonabstractOp = operation;
                        }
                        error("The non-abstract method " + nonabstractOp.getSimpleSignature() + " inherited from " + getDeclaratorName(nonabstractOp.getDeclaration()) + " conflicts with the method " + abstractOp.getSimpleSignature() + " inherited from " + getDeclaratorName(abstractOp.getDeclaration()) + ".", xtendType, XtendPackage.Literals.XTEND_TYPE_DECLARATION__NAME, CONFLICTING_DEFAULT_METHODS, uris);
                    }
                    flaggedType = true;
                }
            }
        }
    }
    if (operationsMissingImplementation != null && !operationsMissingImplementation.isEmpty() && !flaggedType) {
        reportMissingImplementations(xtendType, inferredType, operationsMissingImplementation);
    }
}
Also used : ConflictingDefaultOperation(org.eclipse.xtext.xbase.typesystem.override.ConflictingDefaultOperation) ContextualVisibilityHelper(org.eclipse.xtext.xbase.typesystem.util.ContextualVisibilityHelper) EObject(org.eclipse.emf.ecore.EObject) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) IVisibilityHelper(org.eclipse.xtext.xbase.typesystem.util.IVisibilityHelper) RichString(org.eclipse.xtend.core.xtend.RichString) IResolvedOperation(org.eclipse.xtext.xbase.typesystem.override.IResolvedOperation)

Example 52 with JvmDeclaredType

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

the class XtendImportedNamespaceScopeProvider method getSuperTypeOfLocalTypeNonResolving.

protected JvmDeclaredType getSuperTypeOfLocalTypeNonResolving(JvmDeclaredType declarator) {
    AnonymousClass anonymousClass = associations.getAnonymousClass(declarator);
    if (anonymousClass != null) {
        IScope typeScope = getScope(anonymousClass, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE);
        JvmDeclaredType superType = anonymousClassUtil.getSuperTypeNonResolving(anonymousClass, typeScope);
        if (superType != null) {
            return superType;
        }
    }
    return null;
}
Also used : AnonymousClass(org.eclipse.xtend.core.xtend.AnonymousClass) IScope(org.eclipse.xtext.scoping.IScope) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType)

Example 53 with JvmDeclaredType

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

the class XtendImportedNamespaceScopeProvider method getScope.

@Override
public IScope getScope(final EObject context, final EReference reference) {
    EClass referenceType = reference.getEReferenceType();
    if (TypesPackage.Literals.JVM_TYPE.isSuperTypeOf(referenceType)) {
        if (context instanceof XImportDeclaration) {
            Resource resource = context.eResource();
            IJvmTypeProvider typeProvider = typeScopeProvider.getTypeProvider(resource.getResourceSet());
            AbstractTypeScope typeScope = typeScopeProvider.createTypeScope(typeProvider, null);
            IResourceDescriptions descriptions = resourceDescriptionsProvider.getResourceDescriptions(context.eResource().getResourceSet());
            IResourceDescription resourceDescription = descriptions.getResourceDescription(resource.getURI());
            if (resourceDescription != null) {
                typeScope = new LocalResourceFilteringTypeScope(typeScope, resourceDescription);
            }
            RecordingTypeScope recordingTypeScope = new RecordingTypeScope(typeScope, getImportedNamesSet(resource), getQualifiedNameConverter());
            // TODO this scope doesn't support binary syntax for inner types. It should be a KnownTypes scope which doesn't allow simple names
            // Unfortunately I cannot use a RecordingTypeScope as a parent as it is not compatible...
            IScope scope = SelectableBasedScope.createScope(recordingTypeScope, getAllDescriptions(resource), reference.getEReferenceType(), false);
            return scope;
        }
        final XtendFile xtendFile = getXtendFile(context);
        final Resource resource = xtendFile.eResource();
        AbstractScope result = resourceScopeCache.get("type.scope", xtendFile.eResource(), new Provider<AbstractScope>() {

            @Override
            public AbstractScope get() {
                IJvmTypeProvider typeProvider = typeScopeProvider.getTypeProvider(resource.getResourceSet());
                AbstractTypeScope typeScope = typeScopeProvider.createTypeScope(typeProvider, null);
                IResourceDescriptions descriptions = resourceDescriptionsProvider.getResourceDescriptions(context.eResource().getResourceSet());
                IResourceDescription resourceDescription = descriptions.getResourceDescription(resource.getURI());
                if (resourceDescription != null) {
                    typeScope = new LocalResourceFilteringTypeScope(typeScope, resourceDescription);
                }
                RecordingTypeScope recordingTypeScope = new RecordingTypeScope(typeScope, getImportedNamesSet(resource), getQualifiedNameConverter());
                AbstractScope rootTypeScope = getRootTypeScope(xtendFile, recordingTypeScope);
                AbstractScope importScope = getImportScope(xtendFile.getImportSection(), rootTypeScope, recordingTypeScope);
                AbstractScope localTypes = getResourceTypeScope(xtendFile.eResource(), xtendFile.getPackage(), importScope);
                AbstractScope primitiveAware = new PrimitiveAwareScope(localTypes, typeScope);
                AbstractScope caching = new CachingTypeScope(primitiveAware);
                return caching;
            }
        });
        if (context instanceof AnonymousClass) {
            // necessary for the super type of an anonymous class expression
            JvmDeclaredType inferredAnonymousType = associations.getInferredType((AnonymousClass) context);
            if (inferredAnonymousType != null)
                result = new LocalTypeScope(singletonList(inferredAnonymousType), result);
        }
        XtendMember syntacticContainer = EcoreUtil2.getContainerOfType(context, XtendMember.class);
        if (syntacticContainer != null) {
            result = getContainerScope(syntacticContainer, result);
        }
        EObject logicalContainer = logicalContainerProvider.getNearestLogicalContainer(context);
        if (logicalContainer != null) {
            List<List<JvmTypeParameter>> typeParameters = new ArrayList<List<JvmTypeParameter>>();
            while (logicalContainer instanceof JvmTypeParameterDeclarator) {
                JvmTypeParameterDeclarator typeParamProvider = (JvmTypeParameterDeclarator) logicalContainer;
                if (!typeParamProvider.getTypeParameters().isEmpty()) {
                    typeParameters.add(typeParamProvider.getTypeParameters());
                }
                logicalContainer = logicalContainer.eContainer();
            }
            if (!typeParameters.isEmpty())
                result = new TypeParameterScope(typeParameters, result);
        }
        return result;
    } else if (TypesPackage.Literals.JVM_CONSTRUCTOR.isSuperTypeOf(referenceType)) {
        IScope typeScope = getScope(context, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE);
        // this is not called from the type resolution where we want to allow constructors to link to interfaces
        // in order to improve the error message, therefore we use a strict wrapper here
        IScope result = new ConstructorTypeScopeWrapper(context, IVisibilityHelper.ALL, typeScope, true);
        return result;
    } else {
        throw new IllegalArgumentException("Unexpected global request for " + reference);
    }
}
Also used : IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) XtendMember(org.eclipse.xtend.core.xtend.XtendMember) JvmTypeParameterDeclarator(org.eclipse.xtext.common.types.JvmTypeParameterDeclarator) AbstractTypeScope(org.eclipse.xtext.common.types.xtext.AbstractTypeScope) ArrayList(java.util.ArrayList) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) IJvmTypeProvider(org.eclipse.xtext.common.types.access.IJvmTypeProvider) EClass(org.eclipse.emf.ecore.EClass) AnonymousClass(org.eclipse.xtend.core.xtend.AnonymousClass) EObject(org.eclipse.emf.ecore.EObject) IScope(org.eclipse.xtext.scoping.IScope) List(java.util.List) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) XtendFile(org.eclipse.xtend.core.xtend.XtendFile) JvmTypeParameter(org.eclipse.xtext.common.types.JvmTypeParameter) Resource(org.eclipse.emf.ecore.resource.Resource) XImportDeclaration(org.eclipse.xtext.xtype.XImportDeclaration) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) ConstructorTypeScopeWrapper(org.eclipse.xtext.xbase.scoping.batch.ConstructorTypeScopeWrapper)

Example 54 with JvmDeclaredType

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

the class XtendImportedNamespaceScopeProvider method getContainerScope.

private AbstractScope getContainerScope(XtendMember syntacticContainer, AbstractScope result) {
    JvmDeclaredType innermost = null;
    List<List<JvmTypeParameter>> typeParameters = null;
    XtendMember workWithMe = syntacticContainer;
    while (workWithMe != null) {
        EObject derivedJvmElement = getAssociations().getPrimaryJvmElement(workWithMe);
        // scope for JvmTypeParameterDeclarator
        if (derivedJvmElement instanceof JvmTypeParameterDeclarator) {
            JvmTypeParameterDeclarator parameterDeclarator = (JvmTypeParameterDeclarator) derivedJvmElement;
            List<JvmTypeParameter> current = parameterDeclarator.getTypeParameters();
            if (!current.isEmpty()) {
                if (typeParameters == null) {
                    typeParameters = Lists.newArrayListWithCapacity(3);
                }
                typeParameters.add(current);
            }
        }
        if (innermost == null && derivedJvmElement instanceof JvmDeclaredType) {
            if (syntacticContainer != workWithMe) {
                // prevent stackoverflow / cyclic resolution
                innermost = (JvmDeclaredType) derivedJvmElement;
            }
        }
        EObject container = workWithMe;
        do {
            container = container.eContainer();
            if (container == null) {
                if (typeParameters == null) {
                    if (innermost == null)
                        return result;
                    return new NestedTypesScope(innermost, this, result);
                }
                TypeParameterScope typeParameterScope = new TypeParameterScope(typeParameters, result);
                if (innermost == null) {
                    return typeParameterScope;
                }
                return new NestedTypesScope(innermost, this, typeParameterScope);
            }
        } while (!(container instanceof XtendMember));
        workWithMe = (XtendMember) container;
    }
    return result;
}
Also used : XtendMember(org.eclipse.xtend.core.xtend.XtendMember) JvmTypeParameterDeclarator(org.eclipse.xtext.common.types.JvmTypeParameterDeclarator) EObject(org.eclipse.emf.ecore.EObject) JvmTypeParameter(org.eclipse.xtext.common.types.JvmTypeParameter) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) List(java.util.List) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList)

Example 55 with JvmDeclaredType

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

the class XtendImportedNamespaceScopeProvider method getImportScope.

private AbstractScope getImportScope(XImportSection importSection, AbstractScope parent, RecordingTypeScope typeScope) {
    if (importSection == null)
        return parent;
    List<XImportDeclaration> importDeclarations = importSection.getImportDeclarations();
    if (importDeclarations.isEmpty()) {
        return parent;
    }
    List<ImportNormalizer> wildcardImports = null;
    List<JvmType> concreteImports = null;
    List<QualifiedName> importedNames = null;
    boolean hasLegacyImport = false;
    for (XImportDeclaration importDeclaration : importDeclarations) {
        if (!importDeclaration.isStatic()) {
            if (importDeclaration.isWildcard()) {
                if (wildcardImports == null) {
                    wildcardImports = Lists.newArrayListWithCapacity(4);
                }
                String importedNamespace = importDeclaration.getImportedNamespace();
                importedNamespace = importedNamespace.substring(0, importedNamespace.length() - 2);
                QualifiedName qualifiedImportedNamespace = qualifiedNameConverter.toQualifiedName(importedNamespace);
                wildcardImports.add(AbstractNestedTypeAwareImportNormalizer.createNestedTypeAwareImportNormalizer(qualifiedImportedNamespace, true, false));
            } else {
                JvmDeclaredType importedType = importDeclaration.getImportedType();
                if (importedType != null && !importedType.eIsProxy()) {
                    if (concreteImports == null || importedNames == null) /* to make JDT happy */
                    {
                        concreteImports = Lists.newArrayListWithCapacity(10);
                        importedNames = Lists.newArrayListWithCapacity(10);
                    }
                    concreteImports.add(importedType);
                    if (importedType.eContainer() instanceof JvmDeclaredType) {
                        String importSyntax = getImportsConfiguration().getLegacyImportSyntax(importDeclaration);
                        if (importSyntax != null) {
                            hasLegacyImport = true;
                            importedNames.add(getQualifiedNameConverter().toQualifiedName(importSyntax));
                        } else
                            importedNames.add(null);
                    } else {
                        importedNames.add(null);
                    }
                }
            }
        }
    }
    return getImportScope(wildcardImports, concreteImports, hasLegacyImport ? importedNames : null, parent, typeScope);
}
Also used : AbstractNestedTypeAwareImportNormalizer(org.eclipse.xtext.xbase.scoping.AbstractNestedTypeAwareImportNormalizer) ImportNormalizer(org.eclipse.xtext.scoping.impl.ImportNormalizer) QualifiedName(org.eclipse.xtext.naming.QualifiedName) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) JvmType(org.eclipse.xtext.common.types.JvmType) XImportDeclaration(org.eclipse.xtext.xtype.XImportDeclaration)

Aggregations

JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)138 Test (org.junit.Test)41 EObject (org.eclipse.emf.ecore.EObject)26 JvmType (org.eclipse.xtext.common.types.JvmType)24 JvmMember (org.eclipse.xtext.common.types.JvmMember)21 JvmConstructor (org.eclipse.xtext.common.types.JvmConstructor)18 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)18 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)16 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)16 JvmAnnotationReference (org.eclipse.xtext.common.types.JvmAnnotationReference)15 JvmAnnotationType (org.eclipse.xtext.common.types.JvmAnnotationType)13 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)11 JvmAnnotationTarget (org.eclipse.xtext.common.types.JvmAnnotationTarget)10 TestAnnotation (org.eclipse.xtext.common.types.testSetups.TestAnnotation)10 Resource (org.eclipse.emf.ecore.resource.Resource)8 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)8 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)7 JvmArrayType (org.eclipse.xtext.common.types.JvmArrayType)7 JvmParameterizedTypeReference (org.eclipse.xtext.common.types.JvmParameterizedTypeReference)7 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)6